Skip to content

Commit

Permalink
Fix resource management in the eclipse downloader:
Browse files Browse the repository at this point in the history
- closeQuietly is a pet peeve of mine; it is an abomination that must never be.
- All actual resources are closed; all filters aren't because there is no need.
- the underlying problem (of 'flushing' zips) is addressed.
  • Loading branch information
rzwitserloot committed Oct 24, 2024
1 parent d21d551 commit 7a9f3a8
Showing 1 changed file with 17 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public static void main(String[] args) throws Exception {
for (String artifact : artifacts) {
// Download artifact
downloadFile(artifact, pluginSource, pluginTarget);

// Download artifact source
int index = artifact.lastIndexOf("_");
String source = artifact.substring(0, index) + ".source" + artifact.substring(index);
Expand All @@ -95,31 +96,29 @@ private static void downloadFile(String filename, String repositoryUrl, String t

copyZipButStripSignatures(in, out);
System.out.println("[done]");
} catch (IOException e) {
System.out.println("[error]");
} finally {
closeQuietly(in);
closeQuietly(out);
try {
if (in != null) in.close();
} finally {
if (out != null) out.close();
}
}
}

private static void copyZipButStripSignatures(InputStream rawIn, OutputStream rawOut) throws IOException {
ZipInputStream in = null;
ZipOutputStream out = null;
try {
in = new ZipInputStream(rawIn);
out = new ZipOutputStream(rawOut);

ZipEntry zipEntry;
while ((zipEntry = in.getNextEntry()) != null) {
if (zipEntry.getName().matches("META-INF/.*\\.(SF|RSA)")) continue;
out.putNextEntry(zipEntry);
copy(in, out);
}
} finally {
closeQuietly(in);
closeQuietly(out);

in = new ZipInputStream(rawIn);
out = new ZipOutputStream(rawOut);

ZipEntry zipEntry;
while ((zipEntry = in.getNextEntry()) != null) {
if (zipEntry.getName().matches("META-INF/.*\\.(SF|RSA)")) continue;
out.putNextEntry(zipEntry);
copy(in, out);
}
out.close(); // zip streams buffer.
}

private static void copy(InputStream from, OutputStream to) throws IOException {
Expand All @@ -131,21 +130,11 @@ private static void copy(InputStream from, OutputStream to) throws IOException {
}
}

private static void closeQuietly(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException ignore) {
}
}
}

private static InputStream getStreamForUrl(String url) throws IOException, MalformedURLException {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestProperty("User-Agent", "lombok");
connection.setRequestProperty("Accept", "*/*");
InputStream in = new BufferedInputStream(connection.getInputStream());
return in;
return new BufferedInputStream(connection.getInputStream());
}

private static void writeEclipseLibrary(String target, String eclipseVersion) throws IOException {
Expand Down

0 comments on commit 7a9f3a8

Please sign in to comment.