Skip to content

Commit

Permalink
use try-with-resource, java.nio.file.Files#readString
Browse files Browse the repository at this point in the history
  • Loading branch information
EcljpseB0T authored and jukzi committed Jan 30, 2024
1 parent a53dc16 commit f26df87
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 242 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
Expand Down Expand Up @@ -212,39 +213,25 @@ public void generateAPIFile() {
File currentManifest = new File(allManifestFile);
Set<String> currentApiPackages = null;
if (currentManifest.exists()) {
BufferedInputStream inputStream = null;
ZipFile zipFile = null;
try {
if (isZipJarFile(currentManifest.getName())) {
zipFile = new ZipFile(currentManifest);
final ZipEntry entry = zipFile.getEntry("META-INF/MANIFEST.MF"); //$NON-NLS-1$
if (entry != null) {
inputStream = new BufferedInputStream(zipFile.getInputStream(entry));
try (ZipFile zipFile = new ZipFile(currentManifest)) {
final ZipEntry entry = zipFile.getEntry("META-INF/MANIFEST.MF"); //$NON-NLS-1$
if (entry != null) {
InputStream inputStream = zipFile.getInputStream(entry);
manifestMap = ManifestElement.parseBundleManifest(inputStream, null);
currentApiPackages = collectApiPackageNames(manifestMap);

}
}
} else {
inputStream = new BufferedInputStream(new FileInputStream(currentManifest));
}
if (inputStream != null) {
manifestMap = ManifestElement.parseBundleManifest(inputStream, null);
currentApiPackages = collectApiPackageNames(manifestMap);
try (InputStream inputStream = new FileInputStream(currentManifest)) {
manifestMap = ManifestElement.parseBundleManifest(inputStream, null);
currentApiPackages = collectApiPackageNames(manifestMap);
}
}
} catch (IOException | BundleException e) {
ApiPlugin.log(e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// ignore
}
}
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException e) {
// ignore
}
}
}
}
if (currentApiPackages != null) {
Expand Down Expand Up @@ -424,8 +411,8 @@ private boolean isAPIToolsNature(File dotProjectFile) {
if (!dotProjectFile.exists()) {
return false;
}
try (BufferedInputStream stream = new BufferedInputStream(new FileInputStream(dotProjectFile))) {
String contents = new String(Util.getInputStreamAsCharArray(stream, StandardCharsets.UTF_8));
try {
String contents = Files.readString(dotProjectFile.toPath());
return containsAPIToolsNature(contents);
} catch (IOException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
*******************************************************************************/
package org.eclipse.pde.api.tools.internal;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -251,8 +249,8 @@ public synchronized void saving(ISaveContext context) throws CoreException {
private boolean restoreDescription(IJavaProject project, ProjectApiDescription description) throws CoreException {
File file = API_DESCRIPTIONS_CONTAINER_PATH.append(project.getElementName()).append(IApiCoreConstants.API_DESCRIPTION_XML_NAME).toFile();
if (file.exists()) {
try (BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file))) {
String xml = new String(Util.getInputStreamAsCharArray(stream, StandardCharsets.UTF_8));
try {
String xml = Files.readString(file.toPath());
Element root = Util.parseDocument(xml);
if (!root.getNodeName().equals(IApiXmlConstants.ELEMENT_COMPONENT)) {
abort(ScannerMessages.ComponentXMLScanner_0, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
package org.eclipse.pde.api.tools.internal;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -443,46 +442,28 @@ private ApiDescriptionProcessor() {
*/
public static String serializeComponentXml(File location) {
if (location.exists()) {
ZipFile jarFile = null;
InputStream stream = null;
try {
String extension = IPath.fromOSString(location.getName()).getFileExtension();
if (extension != null && extension.equals("jar") && location.isFile()) { //$NON-NLS-1$
jarFile = new ZipFile(location, ZipFile.OPEN_READ);
ZipEntry manifestEntry = jarFile.getEntry(IApiCoreConstants.COMPONENT_XML_NAME);
if (manifestEntry != null) {
stream = jarFile.getInputStream(manifestEntry);
try (ZipFile jarFile = new ZipFile(location, ZipFile.OPEN_READ)) {
ZipEntry manifestEntry = jarFile.getEntry(IApiCoreConstants.COMPONENT_XML_NAME);
if (manifestEntry != null) {
byte[] allBytes = jarFile.getInputStream(manifestEntry).readAllBytes();
return new String(allBytes, StandardCharsets.UTF_8);
}
}
} else if (location.isDirectory()) {
File file = new File(location, IApiCoreConstants.COMPONENT_XML_NAME);
if (file.exists()) {
stream = new FileInputStream(file);
return Files.readString(file.toPath());
}
} else if (location.isFile()) {
if (location.getName().equals(IApiCoreConstants.COMPONENT_XML_NAME)) {
stream = new FileInputStream(location);
return Files.readString(location.toPath());
}
}
if (stream != null) {
return new String(Util.getInputStreamAsCharArray(stream, StandardCharsets.UTF_8));
}
} catch (IOException e) {
ApiPlugin.log(e);
} finally {
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
ApiPlugin.log(e);
}
try {
if (jarFile != null) {
jarFile.close();
}
} catch (IOException e) {
ApiPlugin.log(e);
}
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
package org.eclipse.pde.api.tools.internal;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

Expand Down Expand Up @@ -65,46 +64,28 @@ private SystemApiDescriptionProcessor() {
*/
public static String serializeComponentXml(File location) {
if (location.exists()) {
ZipFile jarFile = null;
InputStream stream = null;
try {
String extension = IPath.fromOSString(location.getName()).getFileExtension();
if (extension != null && extension.equals("jar") && location.isFile()) { //$NON-NLS-1$
jarFile = new ZipFile(location, ZipFile.OPEN_READ);
ZipEntry manifestEntry = jarFile.getEntry(IApiCoreConstants.SYSTEM_API_DESCRIPTION_XML_NAME);
if (manifestEntry != null) {
stream = jarFile.getInputStream(manifestEntry);
try (ZipFile jarFile = new ZipFile(location, ZipFile.OPEN_READ)) {
ZipEntry manifestEntry = jarFile.getEntry(IApiCoreConstants.SYSTEM_API_DESCRIPTION_XML_NAME);
if (manifestEntry != null) {
byte[] allBytes = jarFile.getInputStream(manifestEntry).readAllBytes();
return new String(allBytes, StandardCharsets.UTF_8);
}
}
} else if (location.isDirectory()) {
File file = new File(location, IApiCoreConstants.SYSTEM_API_DESCRIPTION_XML_NAME);
if (file.exists()) {
stream = new FileInputStream(file);
return Files.readString(file.toPath());
}
} else if (location.isFile()) {
if (location.getName().equals(IApiCoreConstants.SYSTEM_API_DESCRIPTION_XML_NAME)) {
stream = new FileInputStream(location);
return Files.readString(location.toPath());
}
}
if (stream != null) {
return new String(Util.getInputStreamAsCharArray(stream, StandardCharsets.UTF_8));
}
} catch (IOException e) {
ApiPlugin.log(e);
} finally {
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
ApiPlugin.log(e);
}
try {
if (jarFile != null) {
jarFile.close();
}
} catch (IOException e) {
ApiPlugin.log(e);
}
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1761,14 +1761,12 @@ public static void saveFile(File file, String contents) throws IOException {
* @return the contents of the file as a {@link String} or <code>null</code>
*/
public static String getFileContentAsString(File file) {
String contents = null;
try (FileInputStream stream = new FileInputStream(file)) {
char[] array = getInputStreamAsCharArray(stream, StandardCharsets.UTF_8);
contents = new String(array);
try {
return Files.readString(file.toPath());
} catch (IOException ioe) {
ApiPlugin.log(ioe);
return null;
}
return contents;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,41 +118,39 @@ public JNLPGenerator(String feature, String destination, String codebase, String
* Parses the specified url and constructs a feature
*/
public void process() {
InputStream in = null;
final String FEATURE_XML = "feature.xml"; //$NON-NLS-1$

try {
ZipFile featureArchive = null;
InputStream nlsStream = null;
ZipFile featureArchive;
InputStream nlsStream;
InputStream in;
if (featureRoot.isFile()) {
featureArchive = new ZipFile(featureRoot);
nlsStream = getNLSStream(featureArchive);
ZipEntry featureXML = featureArchive.getEntry(FEATURE_XML);
in = featureArchive.getInputStream(featureXML);
} else {
featureArchive = null;
nlsStream = getNLSStream(this.featureRoot);
in = new BufferedInputStream(new FileInputStream(new File(featureRoot, FEATURE_XML)));
}
try {
if (nlsStream != null) {
nlsBundle = new PropertyResourceBundle(nlsStream);
nlsStream.close();
try (featureArchive; nlsStream; in) {
try {
if (nlsStream != null) {
nlsBundle = new PropertyResourceBundle(nlsStream);
}
} catch (IOException e) {
// do nothing
}
try {
parser.parse(new InputSource(in), this);
writeResourceEpilogue();
writeEpilogue();
} catch (SAXException e) {
//Ignore the exception
}
} catch (IOException e) {
// do nothing
}
try {
parser.parse(new InputSource(in), this);
writeResourceEpilogue();
writeEpilogue();
} catch (SAXException e) {
//Ignore the exception
} finally {
in.close();
if (out != null)
out.close();
if (featureArchive != null)
featureArchive.close();
}
} catch (IOException e) {
//Ignore the exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,34 +215,29 @@ private void addBundle(IExtensionRegistry registry, IPluginModelBase base) {
if (input == null) {
return;
}
InputStream is = null;
ZipFile jfile = null;

try {
if (new File(base.getInstallLocation()).isDirectory()) {
// Directory bundle, access the extensions file directly
is = new FileInputStream(input);
try (InputStream is = new BufferedInputStream(new FileInputStream(input))) {
registry.addContribution(is, contributor, true, input.getPath(), null, fKey);
}
} else {
// Archived bundle, need to extract the file
jfile = new ZipFile(input, ZipFile.OPEN_READ);
String fileName = (base.isFragmentModel()) ? ICoreConstants.FRAGMENT_FILENAME_DESCRIPTOR
: ICoreConstants.PLUGIN_FILENAME_DESCRIPTOR;
ZipEntry entry = jfile.getEntry(fileName);
if (entry != null) {
is = jfile.getInputStream(entry);
}
}
if (is != null) {
registry.addContribution(new BufferedInputStream(is), contributor, true, input.getPath(), null, fKey);
}
} catch (IOException e) {
} finally {
if (jfile != null) {
try {
jfile.close();
} catch (IOException e) {
try (ZipFile jfile = new ZipFile(input, ZipFile.OPEN_READ)) {
String fileName = (base.isFragmentModel()) ? ICoreConstants.FRAGMENT_FILENAME_DESCRIPTOR
: ICoreConstants.PLUGIN_FILENAME_DESCRIPTOR;
ZipEntry entry = jfile.getEntry(fileName);
if (entry != null) {
try (InputStream is = jfile.getInputStream(entry)) {
if (is != null) {
registry.addContribution(new BufferedInputStream(is), contributor, true,
input.getPath(), null, fKey);
}
}
}
}
}
} catch (IOException ignored) {
}
}

Expand Down
Loading

0 comments on commit f26df87

Please sign in to comment.