Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8013722

java.lang.IllegalStateException: zip file closed whith setUseCaches(true)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P3 P3
    • None
    • 7u2
    • core-libs

      FULL PRODUCT VERSION :
      java version " 1.7.0_21 "
      Java(TM) SE Runtime Environment (build 1.7.0_21-b11)
      Java HotSpot(TM) Client VM (build 23.21-b01, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      java.lang.IllegalStateException: zip file closed is raised when using an URL connection to a resource within a JAR:

      Please find the stack trace and standalone reproduction code below.


         

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      - URLConnection setDefaultUseCaches(false) has been called
      - two overlapping URLConnection, with setUseCache(true) are used

      This situation occurs in real life when using:
      - CXF 2.2.10: java.org.apache.cxf.common.logging.JDKBugHacks call setDefaultUseCaches
      - Jetty 8.1.9.v20130131: that use the URL connection to serve static resource of META-INF/resources directory


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No exception throw
      ACTUAL -
      An IllegalStateException is thrown

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread " main " java.lang.IllegalStateException: zip file closed
      at java.util.zip.ZipFile.ensureOpen(ZipFile.java:632)
      at java.util.zip.ZipFile.getInputStream(ZipFile.java:345)
      at java.util.jar.JarFile.getInputStream(JarFile.java:409)
      at sun.net.www.protocol.jar.JarURLConnection.getInputStream(JarURLConnection.java:162)
      at com.mycompany.bug.App.main(App.java:43)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException, ExecutionException {
              // Create a jar to reproduce the bug (skip)
              File jarFile = File.createTempFile( " archive " , " jar " );
              JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile));
              JarEntry e = new JarEntry( " lorem.txt " );
              jos.putNextEntry(e);
              jos.write( " Lorem ipsum dolor sit amet " .getBytes());
              jos.closeEntry();
              jos.close();
              URI uri = new URI( " jar: " + jarFile.toURI() + " !/lorem.txt " );

              // Bug is reproduced here
              final URL url = uri.toURL();
              url.openConnection().setDefaultUseCaches(false);

              URLConnection con1 = url.openConnection();
              con1.setUseCaches(true);
              // HERE con1.getUseCaches()==true
              con1.connect();
              // HERE con1.getUseCaches()==false

              URLConnection con2 = url.openConnection();
              con2.setUseCaches(true);
              con2.connect();
              con2.getInputStream().close();

              con1.getInputStream().close(); // KO KO KO : Throws IllegalArgumentException: Zip file closed
          }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      setDefaultUseCaches(true);

            jpai Jaikiran Pai
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: