1

My question is regarding this exception

java.io.IOException: Stream closed
java.util.zip.InflaterInputStream.ensureOpen(InflaterInputStream.java:67)
java.util.zip.InflaterInputStream.read(InflaterInputStream.java:142)
java.io.FilterInputStream.read(FilterInputStream.java:107)
com.avnet.css.servlet.PartNotificationFileServlet.doGet(PartNotificationFileServlet.java:51)
javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

So we recently upgraded our code for Java 7, and we had to implement a try block for a ZipFile declaration. This seems to cause the input stream to close, where it didn't before when there was no try block. Not sure I understand why, when it is declared before the block. Can anyone explain or offer a solution?

    String uri = req.getRequestURI();
    String[] tokens = uri.split("/");
    String folder = tokens[tokens.length - 2];//req.getParameter("folder");
    String filename = tokens[tokens.length - 1];req.getParameter("filename");
    Properties properties = new CSSProperties();
    InputStream inputStream = null;
    if(!folder.contains("_AVT")){
        //below is the new try block, when the declaration inside the parenthesis is on its own, instead of inside try, it works fine.  
        try (ZipFile docsZip = new ZipFile(new File(properties.getProperty(PCN_DIR) + File.separator + folder + File.separator + "Documents.zip"))) {
            ZipEntry entry = docsZip.getEntry(filename);
            if (entry != null)                              
                inputStream = docsZip.getInputStream(entry);
        }
    }
    else{
        String decodedFileName = URLDecoder.decode(filename, "UTF-8");
        File file = new File(properties.getProperty(PCN_DIR) + File.separator + folder + File.separator + decodedFileName);
        if(file.exists())
            inputStream = new FileInputStream(file);
    }
    if(inputStream != null){
        resp.setHeader("Content-Type", MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(filename));
        ServletOutputStream out = resp.getOutputStream();
        byte[] buffer = new byte[2048];
        int read;
        while ((read = inputStream.read(buffer)) > 0) //this is line 51, where the error occurs
            out.write(buffer, 0, read);
        inputStream.close();
        out.close();
    }
1
  • Where is try-catch block in your code ? Commented May 12, 2014 at 18:05

2 Answers 2

2

In the current code you are using the try-with-resources statement, so your ZipFile docsZip is closed as soon as the try is completed.

When you put the declaration of docsZip within the try it will not be closed (note that this should then be done somewhere after out.close().

Sign up to request clarification or add additional context in comments.

3 Comments

So if I just remove it from that line, throw it inside the try block and have an empty finally block, all will be good?
It will probably prevent the exception you are currently experiencing. Note that using an empty finally block may lead to other issues since the docsZip resource is never closed.
Cool, thanks for the explanation. Think I have it sorted out now.
1

This is documented behavior in ZipFile#close:

Closing this ZIP file will close all of the input streams previously returned by invocations of the getInputStream method.

(A Java 7 try-with-resources statement calls close automatically at the end of the block.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.