commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Martijn Dwars (JIRA)" <>
Subject [jira] [Created] (IO-593) copyToFile incorrectly closes input stream
Date Wed, 05 Dec 2018 19:40:00 GMT
Martijn Dwars created IO-593:

             Summary: copyToFile incorrectly closes input stream
                 Key: IO-593
             Project: Commons IO
          Issue Type: Bug
          Components: Utilities
    Affects Versions: 2.6
         Environment: macOS 10.14
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
            Reporter: Martijn Dwars

h3. Description of the Problem

FileUtils.copyToFile is supposed to _not_ close the input stream. According to the documentation: 
{quote}The \{@code source} stream is left open, e.g. for use with \{@link
In 2.5 [|]
this was implemented correctly. In 2.6 [|] both
resources are used in a try-with-resources block, which closes the input stream.
h3. How to Reproduce?

We discovered this bug in the following code:
try (JarInputStream jarStream = new JarInputStream(fileInputStream)) {
  JarEntry entry = jarStream.getNextJarEntry();
  while (jarStream.available() != 0) {
    File file = new File(directory, entry.getName());

    if (!entry.isDirectory()) {
      FileUtils.copyToFile(jarStream, file);
    entry = jarStream.getNextJarEntry();
At some point, jarStream.getNextJarEntry(); throws an exception because the stream is closed:
Caused by: Stream closed
        at java.util.jar.JarInputStream.getNextEntry(
        at java.util.jar.JarInputStream.getNextJarEntry(
You may need to read from a sufficiently large JAR to expose the bug.
h3. How to Fix?

Fixing this is relatively easy: do not alias `in = source` in the try-with-resources block.
Let me know if this bug gets acknowledged, I would be happy to work on a fix.

This message was sent by Atlassian JIRA

View raw message