Can't read file on server more than once

I have an applet which uses a few external jars after loading.

The first time I load the applet everything works fine. But if I try to load the applet again it breaks because I can’t fully read the jar files. I only manage to make only one IOStream read call and then the next read() returns -1.

Closing and opening the browser doesn’t help.
I can only load the applet again if I replace the jar file on the server with a new copy.

The code I’m using for loading the files:


public byte[] loadFile(URL a_fileLocation)
            throws
            Exception
    {
        byte[] byteArray = null;

        try
        {

            // Create URL Connectioin
            URLConnection urlConnection = a_fileLocation.openConnection();
            // Get the length of the downloaded file
            int length = urlConnection.getContentLength();


            // Create the byte array the for the incoming data
            byteArray = new byte[length];

            System.out.println("file length is " + length + " bytes");

            // open the connection
            InputStream io = urlConnection.getInputStream();

            System.out.println("IoStream available = " + io.available());
            int offset = 0;
            int numRead = 0;

            while (offset < byteArray.length
                   &&
                    (numRead = io.read(byteArray, offset, byteArray.length - offset)) >=
                               0)
            {
                offset += numRead;

                System.out.println("offset = " + offset);

            }

            System.out.println("Total array length is " + byteArray.length);
            System.out.println("Last amount read = " + numRead);
            System.out.println("Total read " + offset);

            // close the connection
            io.close();


        }
        catch (Exception ex)
        {
            throw new Exception(ex.getMessage().concat(
                    "@ ImageLoader: loadImageFile( " +
                    a_fileLocation + " )"));
        }
        return byteArray;
    }

This is what I get when the applet works

network: Cache entry found [url: http://www.smerc.com/decades_latest/ExternalResources/InitialLoadingResources.jar, version: null] network: Connecting http://www.smerc.com/decades_latest/ExternalResources/InitialLoadingResources.jar with proxy=DIRECT network: ResponseCode for http://www.smerc.com/decades_latest/ExternalResources/InitialLoadingResources.jar : 200 network: Encoding for http://www.smerc.com/decades_latest/ExternalResources/InitialLoadingResources.jar : null network: Disconnect connection to http://www.smerc.com/decades_latest/ExternalResources/InitialLoadingResources.jar network: Connecting http://www.smerc.com/decades_latest/ExternalResources/InitialLoadingResources.jar with proxy=DIRECT file length is 348751 bytes IoStream available = 8476 offset = 8476 offset = 74176 offset = 139876 offset = 205576 offset = 271276 offset = 336976 offset = 348751 Total array length is 348751 Last amount read = 11775 Total read 348751 network: Downloading resource: http://www.smerc.com/decades_latest/ExternalResources/InitialLoadingResources.jar Content-Length: 348,751 Content-Encoding: null network: Wrote URL http://www.smerc.com/decades_latest/ExternalResources/InitialLoadingResources.jar to File C:\Documents and Settings\Dan\Application Data\Sun\Java\Deployment\cache\6.0\49\5da74fb1-5bcb2cd8-temp

…and this is what I get with subsequent loads when the applet crashes;

network: Cache entry found [url: http://www.smerc.com/decades_latest/ExternalResources/InitialLoadingResources.jar, version: null] network: Connecting http://www.smerc.com/decades_latest/ExternalResources/InitialLoadingResources.jar with proxy=DIRECT network: ResponseCode for http://www.smerc.com/decades_latest/ExternalResources/InitialLoadingResources.jar : 304 network: Encoding for http://www.smerc.com/decades_latest/ExternalResources/InitialLoadingResources.jar : null network: Disconnect connection to http://www.smerc.com/decades_latest/ExternalResources/InitialLoadingResources.jar file length is 348751 bytes IoStream available = 346674 offset = 346674 Total array length is 348751 Last amount read = -1 Total read 346674

Am I not closing the jar correctly? What’s stopping me from fully reading the file again?

Thanks.

You are getting http status code “304 - Not modified” on the second read. It is a common practice to tweak a http-server to return this code to prevent heavy server load due to buggy IE 6 cache implementation. You could try to call setUseCaches(true) on the connection to get the cache content if this happens. Another option would be to call setIfModifiedSince(0) to force the server to make a check and return the file.

Regarding closing resources - as a rule of thumb always do it in a finally block:


InputStream io = null;
try{
    io = urlConnection.getInputStream();
    // (...)
}
catch(Exception readEx)
{
    // handle exception
}
finally{
    if(io!=null) try { io.close() } catch(Exception closeEx){ /* ignore or log if interested */ }
}