----------------------------------------------------------------
BEFORE YOU POST, search the faq at <http://java.apache.org/faq/>
WHEN YOU POST, include all relevant version numbers, log files,
and configuration files.  Don't make us guess your problem!!!
----------------------------------------------------------------

jon * <[EMAIL PROTECTED]> writes:

> on 1/18/00 1:07 PM, Hutchison, Jeff <[EMAIL PROTECTED]> wrote:
> 
> > I've found a bug in AdaptiveClassLoader.loadResourceFromZipfile. This
> > method returns an InputStream with zipfile.getInputStream from within the
> > main block of a try/catch/finally statement and then calls zipfile.close
> > from within the finally block of the same.  When I commented out the
> > finally block of the try/catch/finally, it works fine.  (This is how it
> > was up through 1.1b3.)
> 
> q: are you saying that it shouldn't close the zipfile? why? I'm wondering
> *why* this is a bug.

Because the zipfile is closed *before* the InputStream is read from.
You can easily convince yourself with this test program.  I copied
loadResourceFromZipfile from AdaptiveClassLoader.  Just zip this
source file into a zip file called test.zip in the same directory.

Without the finally block in loadResourceFromZipfile commented out you
will get:

[jhut@trout junk]$ java -nojit ZipReadTest
java.lang.NullPointerException: 
        at java.util.zip.ZipFile.read(ZipFile.java)
        at java.util.zip.ZipFileInputStream.read(ZipFile.java)
        at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java)
        at java.util.zip.InflaterInputStream.read(InflaterInputStream.java)
        at java.util.zip.InflaterInputStream.read(InflaterInputStream.java)
        at ZipReadTest.main(ZipReadTest.java:37)

when it tries to read from the returned InputStream.  Commented out,
it works fine.

-jh

===============================================================================

import java.io.*;
import java.util.zip.*;

public class ZipReadTest {

    private static InputStream loadResourceFromZipfile(File file, String name) {
        ZipFile zipfile = null;
        try {
            zipfile = new ZipFile(file);
            ZipEntry entry = zipfile.getEntry(name);

            if (entry != null) {
                return zipfile.getInputStream(entry);
            } else {
                return null;
            }
        } catch(IOException e) {
            return null;
        } finally { // will not work unless this finally block commented out
            if ( zipfile != null ) {
                try {
                    zipfile.close();
                } catch ( IOException ignored ) {
                }
            }
         }
    }

        
    
    public static void main(String[] args) {
        File zip = new File( "test.zip" );
        InputStream is = loadResourceFromZipfile( zip, "ZipReadTest.java" );
        StringWriter sw = new StringWriter();
        try {
            while (true) {
                int datum = is.read();
                if (datum == -1) break;
                sw.write(datum);
            }
        } catch (IOException ex) {
            System.out.println( ex.getMessage() );
        }
        System.out.println( sw.toString() );
    }
    
} // ZipReadTest


--
--------------------------------------------------------------
Please read the FAQ! <http://java.apache.org/faq/>
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
Archives and Other:  <http://java.apache.org/main/mail.html>
Problems?:           [EMAIL PROTECTED]

Reply via email to