From 16f4e99cb152e1577929a44bfd74f0b5733f6076 Mon Sep 17 00:00:00 2001 From: Avinash Kumar Deepak Date: Mon, 2 Mar 2026 21:14:23 +0530 Subject: [PATCH] Fix stream/zip leaks and infinite loop in export path --- java/src/processing/mode/java/JavaBuild.java | 33 +++++++++++--------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/java/src/processing/mode/java/JavaBuild.java b/java/src/processing/mode/java/JavaBuild.java index b696ab0e2..e3848e4ec 100644 --- a/java/src/processing/mode/java/JavaBuild.java +++ b/java/src/processing/mode/java/JavaBuild.java @@ -1216,7 +1216,9 @@ protected void addClasses(ZipOutputStream zos, File dir, String rootPath) throws ZipEntry entry = new ZipEntry(relativePath); zos.putNextEntry(entry); //zos.write(Base.loadBytesRaw(sub)); - PApplet.saveStream(zos, new FileInputStream(sub)); + try (InputStream fis = new FileInputStream(sub)) { + PApplet.saveStream(zos, fis); + } zos.closeEntry(); } } @@ -1241,7 +1243,9 @@ protected void addDataFolder(ZipOutputStream zos) throws IOException { ZipEntry entry = new ZipEntry(path.substring(offset)); zos.putNextEntry(entry); //zos.write(Base.loadBytesRaw(dataFile)); - PApplet.saveStream(zos, new FileInputStream(dataFile)); + try (InputStream fis = new FileInputStream(dataFile)) { + PApplet.saveStream(zos, fis); + } zos.closeEntry(); } } @@ -1266,8 +1270,7 @@ protected void packClassPathIntoZipFile(String path, // is it a jar file or directory? if (piece.toLowerCase().endsWith(".jar") || piece.toLowerCase().endsWith(".zip")) { - try { - ZipFile file = new ZipFile(piece); + try (ZipFile file = new ZipFile(piece)) { Enumeration entries = file.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); @@ -1284,14 +1287,15 @@ protected void packClassPathIntoZipFile(String path, zos.putNextEntry(entree); byte[] buffer = new byte[(int) entry.getSize()]; - InputStream is = file.getInputStream(entry); - - int offset = 0; - int remaining = buffer.length; - while (remaining > 0) { - int count = is.read(buffer, offset, remaining); - offset += count; - remaining -= count; + try (InputStream is = file.getInputStream(entry)) { + int offset = 0; + int remaining = buffer.length; + while (remaining > 0) { + int count = is.read(buffer, offset, remaining); + if (count == -1) break; + offset += count; + remaining -= count; + } } zos.write(buffer); @@ -1299,7 +1303,6 @@ protected void packClassPathIntoZipFile(String path, zos.closeEntry(); } } - file.close(); } catch (IOException e) { System.err.println("Error in file " + piece); @@ -1348,7 +1351,9 @@ static protected void packClassPathIntoZipFileRecursive(File dir, ZipEntry entry = new ZipEntry(nowfar); zos.putNextEntry(entry); //zos.write(Base.loadBytesRaw(sub)); - PApplet.saveStream(zos, new FileInputStream(sub)); + try (InputStream fis = new FileInputStream(sub)) { + PApplet.saveStream(zos, fis); + } zos.closeEntry(); } }