Skip to content

Commit

Permalink
use try-with-resource
Browse files Browse the repository at this point in the history
to close streams.
  • Loading branch information
EcljpseB0T committed Jun 9, 2023
1 parent e382788 commit 90a65ad
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2009 IBM Corporation and others.
* Copyright (c) 2008, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -101,7 +101,11 @@ protected void setName(String newname) {
* @throws CoreException
*/
protected void abort(String message, Throwable e) throws CoreException {
throw new CoreException(Status.error(message, e));
throw abortException(message, e);
}

protected CoreException abortException(String message, Throwable e) {
return new CoreException(Status.error(message, e));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2009, 2013 IBM Corporation and others.
* Copyright (c) 2009, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -94,31 +94,20 @@ public int hashCode() {
@Override
public byte[] getContents() throws CoreException {
StubArchiveApiTypeContainer archive = (StubArchiveApiTypeContainer) getParent();
ZipFile zipFile = archive.open();
ZipEntry entry = zipFile.getEntry(getName());
InputStream stream = null;
if (entry != null) {
try {
stream = zipFile.getInputStream(entry);
} catch (IOException e) {
abort("Failed to open class file: " + getTypeName() + " in archive: " + archive.fLocation, e); //$NON-NLS-1$ //$NON-NLS-2$
return null;
}
try {
return stream.readAllBytes();
} catch (IOException ioe) {
abort("Unable to read class file: " + getTypeName(), ioe); //$NON-NLS-1$
return null; // never gets here
} finally {
try {
stream.close();
} catch (IOException e) {
ApiPlugin.log(e);
try (ZipFile zipFile = archive.open()) {
ZipEntry entry = zipFile.getEntry(getName());
if (entry != null) {
try (InputStream stream = zipFile.getInputStream(entry)) {
return stream.readAllBytes();
}
}
throw abortException("Class file not found: " + getTypeName() + " in archive: " + archive.fLocation, //$NON-NLS-1$ //$NON-NLS-2$
null);
} catch (IOException e) {
ApiPlugin.log(e);
throw abortException(
"Failed to read class file: " + getTypeName() + " in archive: " + archive.fLocation, e); //$NON-NLS-1$ //$NON-NLS-2$
}
abort("Class file not found: " + getTypeName() + " in archive: " + archive.fLocation, null); //$NON-NLS-1$ //$NON-NLS-2$
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2007, 2021 IBM Corporation and others.
* Copyright (c) 2007, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -1983,15 +1983,14 @@ public static void guntar(String zipPath, String destDirPath) throws TarExceptio
new File(destDirPath, fileDir).mkdirs();
// write file
File outFile = new File(destDirPath, filePath);
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outFile));
int n = 0;
InputStream inputStream = tarFile.getInputStream(zEntry);
BufferedInputStream stream = new BufferedInputStream(inputStream);
while ((n = stream.read(buf)) >= 0) {
outputStream.write(buf, 0, n);
try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outFile))) {
int n = 0;
try (BufferedInputStream stream = new BufferedInputStream(tarFile.getInputStream(zEntry))) {
while ((n = stream.read(buf)) >= 0) {
outputStream.write(buf, 0, n);
}
}
}
outputStream.close();
stream.close();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2005, 2012 IBM Corporation and others.
* Copyright (c) 2005, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -14,6 +14,7 @@
package org.eclipse.pde.internal.core;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.Comparator;
Expand Down Expand Up @@ -113,12 +114,14 @@ private boolean isValid(IConfigurationElement elem) {
value = elem.getAttribute("definition"); //$NON-NLS-1$
String symbolicName = elem.getDeclaringExtension().getContributor().getName();
URL url = getResourceURL(symbolicName, value);
try {
if (url != null && url.openStream().available() > 0) {
return true;
if (url != null) {
try (InputStream s = url.openStream()) {
if (s.available() > 0) {
return true;
}
} catch (IOException e) {
// file does not exist
}
} catch (IOException e) {
// file does not exist
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2017 IBM Corporation and others.
* Copyright (c) 2000, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -212,8 +212,8 @@ private String getPluginID() {
File OSGiFile = new File(file.getParentFile(), ICoreConstants.BUNDLE_FILENAME_DESCRIPTOR);

if (OSGiFile.exists()) {
try {
Map<String, String> headers = ManifestElement.parseBundleManifest(new FileInputStream(OSGiFile), new HeaderMap<>());
try (FileInputStream manifestStream = new FileInputStream(OSGiFile)) {
Map<String, String> headers = ManifestElement.parseBundleManifest(manifestStream, new HeaderMap<>());
String value = headers.get(Constants.BUNDLE_SYMBOLICNAME);
if (value == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2021 IBM Corporation and others.
* Copyright (c) 2000, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -183,11 +183,12 @@ private static IEditorPart openWorkspacePlugin(IFile pluginFile) {
private static IEditorPart openExternalPlugin(File location, String filename) {
IEditorInput input = null;
if (location.isFile()) {
try {
ZipFile zipFile = new ZipFile(location);
if (zipFile.getEntry(filename) != null)
try (ZipFile zipFile = new ZipFile(location)) {
if (zipFile.getEntry(filename) != null) {
input = new JarEntryEditorInput(new JarEntryFile(zipFile, filename));
}
} catch (IOException e) {
// ignore
}
} else {
File file = new File(location, filename);
Expand Down

0 comments on commit 90a65ad

Please sign in to comment.