From d21d551a4dab3ec88c968761fbd3c62bab27fb2a Mon Sep 17 00:00:00 2001 From: Rawi01 Date: Sat, 19 Oct 2024 15:31:26 +0200 Subject: [PATCH 1/2] Close ZipOutputStream in eclipse dependency downloader --- .../DownloadEclipseDependencies.java | 53 +++++++++++-------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/src/support/lombok/eclipse/dependencies/DownloadEclipseDependencies.java b/src/support/lombok/eclipse/dependencies/DownloadEclipseDependencies.java index eff44bf73..a60cd7a71 100644 --- a/src/support/lombok/eclipse/dependencies/DownloadEclipseDependencies.java +++ b/src/support/lombok/eclipse/dependencies/DownloadEclipseDependencies.java @@ -23,6 +23,7 @@ import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; +import java.io.Closeable; import java.io.File; import java.io.FileOutputStream; import java.io.FilenameFilter; @@ -66,17 +67,12 @@ public static void main(String[] args) throws Exception { String pluginSource = updateSite.getResolvedUrl() + "/plugins/"; for (String artifact : artifacts) { - try { - downloadFile(artifact, pluginSource, pluginTarget); - } catch (Exception e) { - } - + // Download artifact + downloadFile(artifact, pluginSource, pluginTarget); + // Download artifact source int index = artifact.lastIndexOf("_"); String source = artifact.substring(0, index) + ".source" + artifact.substring(index); - try { - downloadFile(source, pluginSource, pluginTarget); - } catch (Exception e) { - } + downloadFile(source, pluginSource, pluginTarget); } writeEclipseLibrary(target, eclipseVersion); @@ -102,23 +98,27 @@ private static void downloadFile(String filename, String repositoryUrl, String t } catch (IOException e) { System.out.println("[error]"); } finally { - if (in != null) try { - in.close(); - } catch (Exception ignore) { - } - if (out != null) out.close(); + closeQuietly(in); + closeQuietly(out); } } private static void copyZipButStripSignatures(InputStream rawIn, OutputStream rawOut) throws IOException { - ZipInputStream in = new ZipInputStream(rawIn); - ZipOutputStream 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); + 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); } } @@ -131,6 +131,15 @@ 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"); From 7a9f3a8b7599d4be61bd32698874f6d7e4c15b89 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Thu, 24 Oct 2024 22:15:40 +0200 Subject: [PATCH 2/2] Fix resource management in the eclipse downloader: - 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. --- .../DownloadEclipseDependencies.java | 45 +++++++------------ 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/src/support/lombok/eclipse/dependencies/DownloadEclipseDependencies.java b/src/support/lombok/eclipse/dependencies/DownloadEclipseDependencies.java index a60cd7a71..20b590e63 100644 --- a/src/support/lombok/eclipse/dependencies/DownloadEclipseDependencies.java +++ b/src/support/lombok/eclipse/dependencies/DownloadEclipseDependencies.java @@ -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); @@ -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 { @@ -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 {