Skip to content

Commit 7a9f3a8

Browse files
committed
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.
1 parent d21d551 commit 7a9f3a8

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

src/support/lombok/eclipse/dependencies/DownloadEclipseDependencies.java

+17-28
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public static void main(String[] args) throws Exception {
6969
for (String artifact : artifacts) {
7070
// Download artifact
7171
downloadFile(artifact, pluginSource, pluginTarget);
72+
7273
// Download artifact source
7374
int index = artifact.lastIndexOf("_");
7475
String source = artifact.substring(0, index) + ".source" + artifact.substring(index);
@@ -95,31 +96,29 @@ private static void downloadFile(String filename, String repositoryUrl, String t
9596

9697
copyZipButStripSignatures(in, out);
9798
System.out.println("[done]");
98-
} catch (IOException e) {
99-
System.out.println("[error]");
10099
} finally {
101-
closeQuietly(in);
102-
closeQuietly(out);
100+
try {
101+
if (in != null) in.close();
102+
} finally {
103+
if (out != null) out.close();
104+
}
103105
}
104106
}
105107

106108
private static void copyZipButStripSignatures(InputStream rawIn, OutputStream rawOut) throws IOException {
107109
ZipInputStream in = null;
108110
ZipOutputStream out = null;
109-
try {
110-
in = new ZipInputStream(rawIn);
111-
out = new ZipOutputStream(rawOut);
112-
113-
ZipEntry zipEntry;
114-
while ((zipEntry = in.getNextEntry()) != null) {
115-
if (zipEntry.getName().matches("META-INF/.*\\.(SF|RSA)")) continue;
116-
out.putNextEntry(zipEntry);
117-
copy(in, out);
118-
}
119-
} finally {
120-
closeQuietly(in);
121-
closeQuietly(out);
111+
112+
in = new ZipInputStream(rawIn);
113+
out = new ZipOutputStream(rawOut);
114+
115+
ZipEntry zipEntry;
116+
while ((zipEntry = in.getNextEntry()) != null) {
117+
if (zipEntry.getName().matches("META-INF/.*\\.(SF|RSA)")) continue;
118+
out.putNextEntry(zipEntry);
119+
copy(in, out);
122120
}
121+
out.close(); // zip streams buffer.
123122
}
124123

125124
private static void copy(InputStream from, OutputStream to) throws IOException {
@@ -131,21 +130,11 @@ private static void copy(InputStream from, OutputStream to) throws IOException {
131130
}
132131
}
133132

134-
private static void closeQuietly(Closeable closeable) {
135-
if (closeable != null) {
136-
try {
137-
closeable.close();
138-
} catch (IOException ignore) {
139-
}
140-
}
141-
}
142-
143133
private static InputStream getStreamForUrl(String url) throws IOException, MalformedURLException {
144134
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
145135
connection.setRequestProperty("User-Agent", "lombok");
146136
connection.setRequestProperty("Accept", "*/*");
147-
InputStream in = new BufferedInputStream(connection.getInputStream());
148-
return in;
137+
return new BufferedInputStream(connection.getInputStream());
149138
}
150139

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

0 commit comments

Comments
 (0)