Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-34927] Close streams of extracted files #14

Merged
merged 2 commits into from
Jun 21, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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