import java.io.Writer;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.Random;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.GZIPInputStream;
import java.net.URL;

class ReadBackFile
{
    public static void main( String [ ] args )
    {
        ObjectInputStream dr = null;

        try
        {
            URL url = new URL( "http://users.cis.fiu.edu/~weiss/cop3337/day16.txt" );
            InputStream fr = url.openStream( );
            GZIPInputStream gr = new GZIPInputStream( fr );
            dr = new ObjectInputStream( gr );

            ArrayList<Integer> arr = (ArrayList<Integer>) dr.readObject( );

            long sum = 0;
            for( int x : arr )
                sum += x;

            System.out.println( "Sum of items is: " + sum );
        }
        catch( IOException e )
        {
            System.out.println( "Error opening data file" );
        }
        catch( ClassNotFoundException e )
        {
            System.out.println( "Unknown object on the input stream!" );
        }
        finally
        {
            try
            {
                if( dr != null )
                    dr.close( );
            }
            catch( IOException e )
            {
                System.out.println( "Error closing" );
            }
        }
    }
}

public class Day16
{
    public static void main( String [ ] args )
    {
        Random r = new Random( 1 );
        ObjectOutputStream dw = null;
        
        try
        {
            FileOutputStream fw = new FileOutputStream( "day16.txt" );
            GZIPOutputStream gw = new GZIPOutputStream( fw );
            dw = new ObjectOutputStream( gw );
            ArrayList<Integer> arr = new ArrayList<Integer>( );

            for( int i = 0; i < 10000; i++ )
                arr.add( r.nextInt( 1000000000 ) );

            long sum = 0;
            for( int x : arr )
                sum += x;

            System.out.println( "Sum of items is: " + sum );

            dw.writeObject( arr );
        }
        catch( IOException e )
        {
            System.out.println( "Error creating output file" );
        }
        finally  // guaranteed to be entered no matter what!!!
        {
            try
            {
                if( dw != null )
                    dw.close( );
            }
            catch( IOException e )
            {
                System.out.println( "Error closing output file" );
            }
        }

        System.out.println( "Day16 ends" );
    }

}
