Skip to content

Commit 16f4e99

Browse files
committed
Fix stream/zip leaks and infinite loop in export path
1 parent 4206874 commit 16f4e99

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

java/src/processing/mode/java/JavaBuild.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,9 @@ protected void addClasses(ZipOutputStream zos, File dir, String rootPath) throws
12161216
ZipEntry entry = new ZipEntry(relativePath);
12171217
zos.putNextEntry(entry);
12181218
//zos.write(Base.loadBytesRaw(sub));
1219-
PApplet.saveStream(zos, new FileInputStream(sub));
1219+
try (InputStream fis = new FileInputStream(sub)) {
1220+
PApplet.saveStream(zos, fis);
1221+
}
12201222
zos.closeEntry();
12211223
}
12221224
}
@@ -1241,7 +1243,9 @@ protected void addDataFolder(ZipOutputStream zos) throws IOException {
12411243
ZipEntry entry = new ZipEntry(path.substring(offset));
12421244
zos.putNextEntry(entry);
12431245
//zos.write(Base.loadBytesRaw(dataFile));
1244-
PApplet.saveStream(zos, new FileInputStream(dataFile));
1246+
try (InputStream fis = new FileInputStream(dataFile)) {
1247+
PApplet.saveStream(zos, fis);
1248+
}
12451249
zos.closeEntry();
12461250
}
12471251
}
@@ -1266,8 +1270,7 @@ protected void packClassPathIntoZipFile(String path,
12661270
// is it a jar file or directory?
12671271
if (piece.toLowerCase().endsWith(".jar") ||
12681272
piece.toLowerCase().endsWith(".zip")) {
1269-
try {
1270-
ZipFile file = new ZipFile(piece);
1273+
try (ZipFile file = new ZipFile(piece)) {
12711274
Enumeration<?> entries = file.entries();
12721275
while (entries.hasMoreElements()) {
12731276
ZipEntry entry = (ZipEntry) entries.nextElement();
@@ -1284,22 +1287,22 @@ protected void packClassPathIntoZipFile(String path,
12841287

12851288
zos.putNextEntry(entree);
12861289
byte[] buffer = new byte[(int) entry.getSize()];
1287-
InputStream is = file.getInputStream(entry);
1288-
1289-
int offset = 0;
1290-
int remaining = buffer.length;
1291-
while (remaining > 0) {
1292-
int count = is.read(buffer, offset, remaining);
1293-
offset += count;
1294-
remaining -= count;
1290+
try (InputStream is = file.getInputStream(entry)) {
1291+
int offset = 0;
1292+
int remaining = buffer.length;
1293+
while (remaining > 0) {
1294+
int count = is.read(buffer, offset, remaining);
1295+
if (count == -1) break;
1296+
offset += count;
1297+
remaining -= count;
1298+
}
12951299
}
12961300

12971301
zos.write(buffer);
12981302
zos.flush();
12991303
zos.closeEntry();
13001304
}
13011305
}
1302-
file.close();
13031306

13041307
} catch (IOException e) {
13051308
System.err.println("Error in file " + piece);
@@ -1348,7 +1351,9 @@ static protected void packClassPathIntoZipFileRecursive(File dir,
13481351
ZipEntry entry = new ZipEntry(nowfar);
13491352
zos.putNextEntry(entry);
13501353
//zos.write(Base.loadBytesRaw(sub));
1351-
PApplet.saveStream(zos, new FileInputStream(sub));
1354+
try (InputStream fis = new FileInputStream(sub)) {
1355+
PApplet.saveStream(zos, fis);
1356+
}
13521357
zos.closeEntry();
13531358
}
13541359
}

0 commit comments

Comments
 (0)