Skip to content

Commit

Permalink
fix Potential resource leak warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
EcljpseB0T authored and jukzi committed Jan 22, 2024
1 parent 7cb0273 commit 55034d1
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import jakarta.inject.Named;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
Expand Down Expand Up @@ -290,7 +291,9 @@ private Resource getResource(URI uri) throws Exception {
// The DataArea.assertLocationInitialized is called by ResourceSetImpl.getResource(URI,
// boolean)
resource = resourceSet.createResource(uri);
resource.load(new URL(uri.toString()).openStream(), resourceSet.getLoadOptions());
try (InputStream openStream = new URL(uri.toString()).openStream()) {
resource.load(openStream, resourceSet.getLoadOptions());
}
}

return resource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ private static boolean isCommentEnd(StringBuilder buf) {
return tagLen >= 5 && "--".equals(buf.substring(tagLen - 2)); //$NON-NLS-1$
}

@SuppressWarnings("resource")
private void unread(int ch) throws IOException {
((PushbackReader) getReader()).unread(ch);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,10 @@ private InputStream getInputStream(String charset) throws CoreException, IOExcep
int bytesRead= 0;
do {
int bytes= contents.read(bomStore, bytesRead, bomLength - bytesRead);
if (bytes == -1)
if (bytes == -1) {
contents.close();
throw new IOException();
}
bytesRead += bytes;
} while (bytesRead < bomLength);

Expand Down
3 changes: 2 additions & 1 deletion bundles/org.eclipse.ui.ide/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ Require-Bundle: org.eclipse.core.resources;bundle-version="[3.20.0,4.0.0)";resol
org.eclipse.e4.ui.ide;bundle-version="[3.15.0,4.0.0)";visibility:=reexport,
org.eclipse.e4.core.di;bundle-version="[1.9.0,2.0.0)",
org.eclipse.e4.core.di.extensions;bundle-version="[0.18.0,1.0.0)",
org.eclipse.ui.navigator;bundle-version="3.12.0"
org.eclipse.ui.navigator;bundle-version="3.12.0",
org.eclipse.jdt.annotation
Import-Package: jakarta.annotation;version="[2.1.0,3.0.0)",
jakarta.inject;version="[2.0.0,3.0.0)",
org.osgi.service.event;version="[1.4.0,2.0.0)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public static boolean isZipFile(String fileName) {
* @param shell
* The shell to display any possible Dialogs in
*/
@SuppressWarnings("resource")
public static void closeStructureProvider(ILeveledImportStructureProvider structureProvider, Shell shell) {
if (structureProvider instanceof ZipLeveledStructureProvider) {
closeZipFile(((ZipLeveledStructureProvider) structureProvider).getZipFile(), shell);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @since 3.1
*/
public class TarFile implements AutoCloseable {
private File file;
private final File file;
private TarInputStream entryEnumerationStream;
private TarEntry curEntry;
private TarInputStream entryStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,12 @@ public TarFile getTarFile() {

@Override
public boolean closeArchive(){
try {
getTarFile().close();
TarFile tf = getTarFile();
try (tf) {
// autoclose
} catch (IOException e) {
IDEWorkbenchPlugin.log(DataTransferMessages.ZipImport_couldNotClose
+ getTarFile().getName(), e);
+ tf.getName(), e);
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,22 @@ public class FileTool {
public static void unzip(ZipFile zipFile, File dstDir) throws IOException {
Enumeration<? extends ZipEntry> entries = zipFile.entries();

try {
while(entries.hasMoreElements()){
ZipEntry entry = entries.nextElement();
if(entry.isDirectory()){
continue;
}
String entryName = entry.getName();
if (!new File(dstDir, entryName).toPath().normalize().startsWith(dstDir.toPath().normalize())) {
throw new RuntimeException("Bad zip entry: " + entryName); //$NON-NLS-1$
}
File file = new File(dstDir, changeSeparator(entryName, '/', File.separatorChar));
file.getParentFile().mkdirs();

try (
InputStream src = zipFile.getInputStream(entry);
OutputStream dst = new FileOutputStream(file)
) {
transferData(src, dst);
}
while (entries.hasMoreElements()) {
ZipEntry entry= entries.nextElement();
if (entry.isDirectory()) {
continue;
}
String entryName= entry.getName();
if (!new File(dstDir, entryName).toPath().normalize().startsWith(dstDir.toPath().normalize())) {
throw new RuntimeException("Bad zip entry: " + entryName); //$NON-NLS-1$
}
} finally {
try {
zipFile.close();
} catch(IOException e){
File file= new File(dstDir, changeSeparator(entryName, '/', File.separatorChar));
file.getParentFile().mkdirs();

try (
InputStream src= zipFile.getInputStream(entry);
OutputStream dst= new FileOutputStream(file)) {
transferData(src, dst);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ public static IProject createLinkedProject(String projectName, Plugin plugin, IP

public static IProject createJUnitSourceProject(String projectName) throws CoreException, ZipException, IOException {
IProject project= ResourceHelper.createProject(projectName);
ZipFile zip= new ZipFile(FileTool.getFileInPlugin(SearchTestPlugin.getDefault(), IPath.fromOSString("testresources/junit37-noUI-src.zip"))); //$NON-NLS-1$
FileTool.unzip(zip, project.getLocation().toFile());
try (ZipFile zip= new ZipFile(FileTool.getFileInPlugin(SearchTestPlugin.getDefault(), IPath.fromOSString("testresources/junit37-noUI-src.zip")))) { //$NON-NLS-1$
FileTool.unzip(zip, project.getLocation().toFile());
}
project.refreshLocal(IResource.DEPTH_INFINITE, null);
return project;
}
Expand Down

0 comments on commit 55034d1

Please sign in to comment.