From 1a00f4db72c9202c75f502b152e10d36319440da Mon Sep 17 00:00:00 2001 From: Carsten Hammer Date: Tue, 21 May 2019 20:57:56 +0200 Subject: Use try-with-resources Convert try finally block to try-with-resources Change-Id: I1b6632068bb6fbb4934cf25d040e4b2ae22749a5 Signed-off-by: Carsten Hammer --- .../artifact/processors/jardelta/DeltaApplier.java | 5 +- .../META-INF/MANIFEST.MF | 2 +- .../pom.xml | 2 +- .../metadata/repository/MetadataRepositoryIO.java | 8 +-- .../META-INF/MANIFEST.MF | 2 +- .../pom.xml | 2 +- .../eclipse/pde/internal/swt/tools/IconExe.java | 75 +++++++++++----------- .../processing/ProcessingStepHandlerTest.java | 12 ++-- .../sharedinstall/AbstractSharedInstallTest.java | 8 +-- 9 files changed, 52 insertions(+), 64 deletions(-) diff --git a/bundles/org.eclipse.equinox.p2.artifact.processors/src/org/eclipse/equinox/internal/p2/artifact/processors/jardelta/DeltaApplier.java b/bundles/org.eclipse.equinox.p2.artifact.processors/src/org/eclipse/equinox/internal/p2/artifact/processors/jardelta/DeltaApplier.java index 1e87d5476..305dd5cdd 100644 --- a/bundles/org.eclipse.equinox.p2.artifact.processors/src/org/eclipse/equinox/internal/p2/artifact/processors/jardelta/DeltaApplier.java +++ b/bundles/org.eclipse.equinox.p2.artifact.processors/src/org/eclipse/equinox/internal/p2/artifact/processors/jardelta/DeltaApplier.java @@ -101,11 +101,8 @@ public class DeltaApplier { try { // if there is a sourceJar copy over the content for the entry into the result if (sourceJar != null) { - InputStream contents = sourceJar.getInputStream(entry); - try { + try (InputStream contents = sourceJar.getInputStream(entry)) { transferStreams(contents, result); - } finally { - contents.close(); } } } finally { diff --git a/bundles/org.eclipse.equinox.p2.metadata.repository/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.p2.metadata.repository/META-INF/MANIFEST.MF index cd4c47db2..d210b3bf7 100644 --- a/bundles/org.eclipse.equinox.p2.metadata.repository/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.equinox.p2.metadata.repository/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.equinox.p2.metadata.repository;singleton:=true -Bundle-Version: 1.3.100.qualifier +Bundle-Version: 1.3.200.qualifier Bundle-Activator: org.eclipse.equinox.internal.p2.metadata.repository.Activator Bundle-Vendor: %providerName Bundle-Localization: plugin diff --git a/bundles/org.eclipse.equinox.p2.metadata.repository/pom.xml b/bundles/org.eclipse.equinox.p2.metadata.repository/pom.xml index 965d1d9e9..ed96b91b2 100644 --- a/bundles/org.eclipse.equinox.p2.metadata.repository/pom.xml +++ b/bundles/org.eclipse.equinox.p2.metadata.repository/pom.xml @@ -19,6 +19,6 @@ org.eclipse.equinox org.eclipse.equinox.p2.metadata.repository - 1.3.100-SNAPSHOT + 1.3.200-SNAPSHOT eclipse-plugin diff --git a/bundles/org.eclipse.equinox.p2.metadata.repository/src/org/eclipse/equinox/internal/p2/metadata/repository/MetadataRepositoryIO.java b/bundles/org.eclipse.equinox.p2.metadata.repository/src/org/eclipse/equinox/internal/p2/metadata/repository/MetadataRepositoryIO.java index 77657163a..c5c2b1e56 100644 --- a/bundles/org.eclipse.equinox.p2.metadata.repository/src/org/eclipse/equinox/internal/p2/metadata/repository/MetadataRepositoryIO.java +++ b/bundles/org.eclipse.equinox.p2.metadata.repository/src/org/eclipse/equinox/internal/p2/metadata/repository/MetadataRepositoryIO.java @@ -89,15 +89,9 @@ public class MetadataRepositoryIO { * */ public void write(IMetadataRepository repository, OutputStream output) throws IOException { - OutputStream bufferedOutput = null; - try { - bufferedOutput = new BufferedOutputStream(output); + try (OutputStream bufferedOutput = new BufferedOutputStream(output)) { Writer repositoryWriter = new Writer(bufferedOutput, repository.getClass()); repositoryWriter.write(repository); - } finally { - if (bufferedOutput != null) { - bufferedOutput.close(); - } } } diff --git a/bundles/org.eclipse.equinox.p2.publisher.eclipse/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.p2.publisher.eclipse/META-INF/MANIFEST.MF index 8a6048c33..dd9eec45c 100644 --- a/bundles/org.eclipse.equinox.p2.publisher.eclipse/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.equinox.p2.publisher.eclipse/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %bundleName Bundle-SymbolicName: org.eclipse.equinox.p2.publisher.eclipse;singleton:=true -Bundle-Version: 1.3.200.qualifier +Bundle-Version: 1.3.300.qualifier Bundle-Activator: org.eclipse.pde.internal.publishing.Activator Bundle-ActivationPolicy: lazy Bundle-Vendor: %providerName diff --git a/bundles/org.eclipse.equinox.p2.publisher.eclipse/pom.xml b/bundles/org.eclipse.equinox.p2.publisher.eclipse/pom.xml index 12f016ade..ff9d57186 100644 --- a/bundles/org.eclipse.equinox.p2.publisher.eclipse/pom.xml +++ b/bundles/org.eclipse.equinox.p2.publisher.eclipse/pom.xml @@ -10,6 +10,6 @@ org.eclipse.equinox org.eclipse.equinox.p2.publisher.eclipse - 1.3.200-SNAPSHOT + 1.3.300-SNAPSHOT eclipse-plugin diff --git a/bundles/org.eclipse.equinox.p2.publisher.eclipse/src/org/eclipse/pde/internal/swt/tools/IconExe.java b/bundles/org.eclipse.equinox.p2.publisher.eclipse/src/org/eclipse/pde/internal/swt/tools/IconExe.java index a93034d3a..052aa07c4 100644 --- a/bundles/org.eclipse.equinox.p2.publisher.eclipse/src/org/eclipse/pde/internal/swt/tools/IconExe.java +++ b/bundles/org.eclipse.equinox.p2.publisher.eclipse/src/org/eclipse/pde/internal/swt/tools/IconExe.java @@ -125,13 +125,15 @@ public class IconExe { * @param program the Windows executable e.g c:/eclipse/eclipse.exe */ static ImageData[] loadIcons(String program) throws FileNotFoundException, IOException { - RandomAccessFile raf = new RandomAccessFile(program, "r"); //$NON-NLS-1$ - IconExe iconExe = new IconExe(); - IconResInfo[] iconInfo = iconExe.getIcons(raf); - ImageData[] data = new ImageData[iconInfo.length]; - for (int i = 0; i < data.length; i++) - data[i] = iconInfo[i].data; - raf.close(); + ImageData[] data; + try (RandomAccessFile raf = new RandomAccessFile(program, "r") //$NON-NLS-1$ + ) { + IconExe iconExe = new IconExe(); + IconResInfo[] iconInfo = iconExe.getIcons(raf); + data = new ImageData[iconInfo.length]; + for (int i = 0; i < data.length; i++) + data[i] = iconInfo[i].data; + } return data; } @@ -176,31 +178,33 @@ public class IconExe { * @return the list of icons from the original program that were not successfully replaced (empty if success) */ static List unloadIcons(String program, ImageData[] icons) throws FileNotFoundException, IOException { - RandomAccessFile raf = new RandomAccessFile(program, "rw"); //$NON-NLS-1$ - IconExe iconExe = new IconExe(); - List iconInfo = new ArrayList<>(Arrays.asList(iconExe.getIcons(raf))); - // Display an error if no icons found in target executable. - if (iconInfo.isEmpty()) { - System.err.println("Warning - no icons detected in \"" + program + "\"."); //$NON-NLS-1$ //$NON-NLS-2$ - raf.close(); - return Collections.emptyList(); - } - Iterator originalIconsIterator = iconInfo.iterator(); - while (originalIconsIterator.hasNext()) { - IconResInfo iconToReplace = originalIconsIterator.next(); - for (ImageData iconToWrite : Arrays.asList(icons)) { - if (iconToWrite == null) - continue; - - if (iconToReplace.data.width == iconToWrite.width && iconToReplace.data.height == iconToWrite.height && iconToReplace.data.depth == iconToWrite.depth) { - raf.seek(iconToReplace.offset); - unloadIcon(raf, iconToWrite); - originalIconsIterator.remove(); - break; + List iconInfo; + try (RandomAccessFile raf = new RandomAccessFile(program, "rw") //$NON-NLS-1$ + ) { + IconExe iconExe = new IconExe(); + iconInfo = new ArrayList<>(Arrays.asList(iconExe.getIcons(raf))); + // Display an error if no icons found in target executable. + if (iconInfo.isEmpty()) { + System.err.println("Warning - no icons detected in \"" + program + "\"."); //$NON-NLS-1$ //$NON-NLS-2$ + raf.close(); + return Collections.emptyList(); + } + Iterator originalIconsIterator = iconInfo.iterator(); + while (originalIconsIterator.hasNext()) { + IconResInfo iconToReplace = originalIconsIterator.next(); + for (ImageData iconToWrite : Arrays.asList(icons)) { + if (iconToWrite == null) + continue; + + if (iconToReplace.data.width == iconToWrite.width && iconToReplace.data.height == iconToWrite.height && iconToReplace.data.depth == iconToWrite.depth) { + raf.seek(iconToReplace.offset); + unloadIcon(raf, iconToWrite); + originalIconsIterator.remove(); + break; + } } } } - raf.close(); return iconInfo; } @@ -516,13 +520,12 @@ public class IconExe { static void copyFile(String src, String dst) throws FileNotFoundException, IOException { File srcFile = new File(src); File dstFile = new File(dst); - InputStream in = new BufferedInputStream(new FileInputStream(srcFile)); - OutputStream out = new BufferedOutputStream(new FileOutputStream(dstFile)); - int c; - while ((c = in.read()) != -1) - out.write(c); - in.close(); - out.close(); + try (InputStream in = new BufferedInputStream(new FileInputStream(srcFile)); + OutputStream out = new BufferedOutputStream(new FileOutputStream(dstFile))) { + int c; + while ((c = in.read()) != -1) + out.write(c); + } } /* IO utilities to parse Windows executable */ diff --git a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/artifact/repository/processing/ProcessingStepHandlerTest.java b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/artifact/repository/processing/ProcessingStepHandlerTest.java index 5179dc98e..c8102ffbd 100644 --- a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/artifact/repository/processing/ProcessingStepHandlerTest.java +++ b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/artifact/repository/processing/ProcessingStepHandlerTest.java @@ -115,9 +115,9 @@ public class ProcessingStepHandlerTest extends AbstractProvisioningTest { public void testExecuteOneMD5VerifierPSOk() throws IOException { ProcessingStep[] steps = new ProcessingStep[] {new MD5Verifier("0cbc6611f5540bd0809a388dc95a615b")}; ByteArrayOutputStream result = new ByteArrayOutputStream(10); - OutputStream testStream = handler.link(steps, result, monitor); - testStream.write("Test".getBytes()); - testStream.close(); + try (OutputStream testStream = handler.link(steps, result, monitor)) { + testStream.write("Test".getBytes()); + } assertEquals("Test", result.toString()); } @@ -139,9 +139,9 @@ public class ProcessingStepHandlerTest extends AbstractProvisioningTest { // Order of PSs is important!! ProcessingStep[] steps = new ProcessingStep[] {new ByteShifter(1), new MD5Verifier("ceeee507e8db83294600218b4e198897")}; ByteArrayOutputStream result = new ByteArrayOutputStream(10); - OutputStream testStream = handler.link(steps, result, monitor); - testStream.write(new byte[] {1, 2, 3, 4, 5}); - testStream.close(); + try (OutputStream testStream = handler.link(steps, result, monitor)) { + testStream.write(new byte[] {1, 2, 3, 4, 5}); + } assertTrue(Arrays.equals(new byte[] {2, 4, 6, 8, 10}, result.toByteArray())); } diff --git a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/sharedinstall/AbstractSharedInstallTest.java b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/sharedinstall/AbstractSharedInstallTest.java index 7a0d95dc2..de8373cda 100644 --- a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/sharedinstall/AbstractSharedInstallTest.java +++ b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/sharedinstall/AbstractSharedInstallTest.java @@ -253,14 +253,8 @@ public abstract class AbstractSharedInstallTest extends AbstractReconcilerTest { public static Properties loadProperties(File inputFile) throws FileNotFoundException, IOException { Properties props = new Properties(); - InputStream is = null; - try { - is = new FileInputStream(inputFile); + try (InputStream is = new FileInputStream(inputFile)) { props.load(is); - } finally { - if (is != null) - is.close(); - is = null; } return props; } -- cgit v1.2.3