Skip to content

Commit

Permalink
Merge pull request #14 from mkaring/feature/jenkins-34927
Browse files Browse the repository at this point in the history
[JENKINS-34927] Close streams of extracted files
  • Loading branch information
rsandell authored Jun 21, 2016
2 parents a676e56 + 707e3d5 commit 2bac282
Showing 1 changed file with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;

import javax.inject.Inject;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.Enumeration;
Expand Down Expand Up @@ -142,16 +142,23 @@ public Map<String, String> invoke(File zipFile, VirtualChannel channel) throws I
logger.print(entry.getName());
logger.print(" -> ");
logger.println(f.getRemote());
IOUtils.copy(zip.getInputStream(entry), f.write());

/*
It is not by all means required to close the input streams of the zip file because they are
closed once the zip file is closed. How ever doing so allows the zip class to reuse the
Inflater instance that is used.
*/
try (InputStream inputStream = zip.getInputStream(entry);
OutputStream outputStream = f.write()) {
IOUtils.copy(inputStream, outputStream);
outputStream.flush();
}
} else {
logger.print("Reading: ");
logger.println(entry.getName());
// we need to copy byte by byte everything to be sure that no carriage return characters are skipped
// readLine skips the carriage return and for example files ending with only one carriage return are trimmed

try (InputStream is = zip.getInputStream(entry); ByteArrayOutputStream output = new ByteArrayOutputStream()) {
IOUtils.copyLarge(is, output);
strMap.put(entry.getName(), new String(output.toByteArray(), Charset.defaultCharset()));
try (InputStream is = zip.getInputStream(entry)) {
strMap.put(entry.getName(), IOUtils.toString(is, Charset.defaultCharset()));
}
}
}
Expand All @@ -162,9 +169,7 @@ public Map<String, String> invoke(File zipFile, VirtualChannel channel) throws I
return null;
}
} finally {
if (zip != null) {
zip.close(); //according to docs this should also close all open input streams.
}
IOUtils.closeQuietly(zip);
}
}

Expand Down

0 comments on commit 2bac282

Please sign in to comment.