diff options
author | Joerg Kubitz | 2021-05-05 12:31:07 +0000 |
---|---|---|
committer | Jay Arthanareeswaran | 2021-07-01 05:01:40 +0000 |
commit | a19a9734fc706ff528cd0d5f71093214e576c18b (patch) | |
tree | a1454cc3190337529ba7f8795a93170fcfcd6cf9 /org.eclipse.jdt.compiler.apt/src | |
parent | 211bb32d9587002e05ba83fc45b47990e5954d0e (diff) | |
download | eclipse.jdt.core-a19a9734fc706ff528cd0d5f71093214e576c18b.tar.gz eclipse.jdt.core-a19a9734fc706ff528cd0d5f71093214e576c18b.tar.xz eclipse.jdt.core-a19a9734fc706ff528cd0d5f71093214e576c18b.zip |
Bug 573287 - unclosed zipfile handle in ArchiveFileObject
Do not rely on garbage collection for closing resources:
Read file content in memory and close the ZipFile immediately.
Change-Id: I04d2415157a23b2b03b939cde2a7440a50d13ffd
Signed-off-by: Joerg Kubitz <jkubitz-eclipse@gmx.de>
Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/180237
Tested-by: JDT Bot <jdt-bot@eclipse.org>
Reviewed-by: Jay Arthanareeswaran <jarthana@in.ibm.com>
Diffstat (limited to 'org.eclipse.jdt.compiler.apt/src')
3 files changed, 8 insertions, 24 deletions
diff --git a/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/util/Archive.java b/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/util/Archive.java index e881d2b34f..94f0d8e7a1 100644 --- a/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/util/Archive.java +++ b/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/util/Archive.java @@ -13,6 +13,7 @@ *******************************************************************************/ package org.eclipse.jdt.internal.compiler.apt.util; +import java.io.Closeable; import java.io.File; import java.io.IOException; import java.nio.charset.Charset; @@ -29,7 +30,7 @@ import java.util.zip.ZipFile; /** * Used as a zip file cache. */ -public class Archive { +public class Archive implements Closeable { public static final Archive UNKNOWN_ARCHIVE = new Archive(); @@ -106,6 +107,7 @@ public class Archive { this.packagesCache = null; } + @Override public void close() { try { if (this.zipFile != null) { diff --git a/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/util/ArchiveFileObject.java b/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/util/ArchiveFileObject.java index 190266fd02..ddb6eec555 100644 --- a/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/util/ArchiveFileObject.java +++ b/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/util/ArchiveFileObject.java @@ -13,6 +13,7 @@ *******************************************************************************/ package org.eclipse.jdt.internal.compiler.apt.util; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -39,7 +40,6 @@ import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException; public class ArchiveFileObject implements JavaFileObject { protected String entryName; protected File file; - protected ZipFile zipFile; protected Charset charset; public ArchiveFileObject(File file, String entryName, Charset charset) { @@ -48,19 +48,6 @@ public class ArchiveFileObject implements JavaFileObject { this.charset = charset; } - @SuppressWarnings("deprecation") - @Override - protected void finalize() throws Throwable { - if (this.zipFile != null) { - try { - this.zipFile.close(); - } catch (IOException e) { - // ignore - } - } - super.finalize(); - } - /* (non-Javadoc) * @see javax.tools.JavaFileObject#getAccessLevel() */ @@ -217,11 +204,11 @@ public class ArchiveFileObject implements JavaFileObject { */ @Override public InputStream openInputStream() throws IOException { - if (this.zipFile == null) { - this.zipFile = new ZipFile(this.file); + try (ZipFile zipFile = new ZipFile(this.file); + InputStream inputStream = zipFile.getInputStream(zipFile.getEntry(this.entryName));) { + ByteArrayInputStream buffer = new ByteArrayInputStream(inputStream.readAllBytes()); + return buffer; } - ZipEntry zipEntry = this.zipFile.getEntry(this.entryName); - return this.zipFile.getInputStream(zipEntry); } /* (non-Javadoc) diff --git a/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/util/JrtFileSystem.java b/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/util/JrtFileSystem.java index 755a9e5f1c..68640bb243 100644 --- a/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/util/JrtFileSystem.java +++ b/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/util/JrtFileSystem.java @@ -147,11 +147,6 @@ public class JrtFileSystem extends Archive { } @Override - protected void finalize() throws Throwable { - // Nothing to do here - } - - @Override protected ClassFileReader getClassReader() { ClassFileReader reader = null; try { |