diff options
author | Carsten Hammer | 2019-05-25 13:38:32 +0000 |
---|---|---|
committer | Karsten Thoms | 2019-09-25 18:52:27 +0000 |
commit | 46ff79e0c747ec2308cff542015e4b82d6882472 (patch) | |
tree | 8402cbf7e54a5fa2c1adb7f9bdd3fbd604ddfd25 | |
parent | c869c9dfb29c62db5a39b440b425eea6bb85caac (diff) | |
download | eclipse.platform.team-46ff79e0c747ec2308cff542015e4b82d6882472.tar.gz eclipse.platform.team-46ff79e0c747ec2308cff542015e4b82d6882472.tar.xz eclipse.platform.team-46ff79e0c747ec2308cff542015e4b82d6882472.zip |
Use try-with-resourcesI20190926-0625
Convert try finally block to try-with-resources
Change-Id: Iff35bb041daf77cd29f9c17ee931c8b2de186e4c
Signed-off-by: Carsten Hammer <carsten.hammer@t-online.de>
21 files changed, 98 insertions, 189 deletions
diff --git a/bundles/org.eclipse.compare.win32/src/org/eclipse/compare/internal/win32/AbstractMergeViewer.java b/bundles/org.eclipse.compare.win32/src/org/eclipse/compare/internal/win32/AbstractMergeViewer.java index c898d908c..6c5b509b5 100644 --- a/bundles/org.eclipse.compare.win32/src/org/eclipse/compare/internal/win32/AbstractMergeViewer.java +++ b/bundles/org.eclipse.compare.win32/src/org/eclipse/compare/internal/win32/AbstractMergeViewer.java @@ -181,16 +181,13 @@ public abstract class AbstractMergeViewer extends Viewer { private File createTempFile(InputStream contents) throws IOException { File file = File.createTempFile("compare", ".doc"); //$NON-NLS-1$ //$NON-NLS-2$ file.deleteOnExit(); - OutputStream out = new BufferedOutputStream(new FileOutputStream(file)); - try { + try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) { byte[] buffer = new byte[1024]; int length; while ((length = contents.read(buffer)) != -1) { out.write(buffer, 0, length); } return file; - } finally { - out.close(); } } @@ -270,8 +267,7 @@ public abstract class AbstractMergeViewer extends Viewer { } protected byte[] asBytes(File file) throws IOException { - InputStream in = new BufferedInputStream(new FileInputStream(file)); - try { + try (InputStream in = new BufferedInputStream(new FileInputStream(file))) { ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; @@ -280,8 +276,6 @@ public abstract class AbstractMergeViewer extends Viewer { } out.close(); return out.toByteArray(); - } finally { - in.close(); } } diff --git a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/CompareWithOtherResourceDialog.java b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/CompareWithOtherResourceDialog.java index 805c090f5..dd13979da 100644 --- a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/CompareWithOtherResourceDialog.java +++ b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/CompareWithOtherResourceDialog.java @@ -569,12 +569,10 @@ public class CompareWithOtherResourceDialog extends TitleAreaDialog { } catch (CoreException e) { // in case .project file or folder has been deleted IPath projectPath = stateLocation.append(TMP_PROJECT_NAME); projectPath.toFile().mkdirs(); - FileOutputStream output = new FileOutputStream( - projectPath.append(".project").toOSString()); //$NON-NLS-1$ - try { + try (FileOutputStream output = new FileOutputStream( + projectPath.append(".project").toOSString()) //$NON-NLS-1$ + ) { output.write(TMP_PROJECT_FILE.getBytes()); - } finally { - output.close(); } project.open(null); } diff --git a/bundles/org.eclipse.jsch.ui/src/org/eclipse/jsch/internal/ui/preference/PreferencePage.java b/bundles/org.eclipse.jsch.ui/src/org/eclipse/jsch/internal/ui/preference/PreferencePage.java index f213b710b..7efee3892 100644 --- a/bundles/org.eclipse.jsch.ui/src/org/eclipse/jsch/internal/ui/preference/PreferencePage.java +++ b/bundles/org.eclipse.jsch.ui/src/org/eclipse/jsch/internal/ui/preference/PreferencePage.java @@ -1440,9 +1440,9 @@ protected Control createContents(Composite parent){ } try{ - ByteArrayInputStream bis=new ByteArrayInputStream(pkey); - c.put(bis, "authorized_keys", null, ChannelSftp.APPEND); //$NON-NLS-1$ - bis.close(); + try (ByteArrayInputStream bis = new ByteArrayInputStream(pkey)) { + c.put(bis, "authorized_keys", null, ChannelSftp.APPEND); //$NON-NLS-1$ + } checkPermission(c, "authorized_keys"); //$NON-NLS-1$ checkPermission(c, "."); // .ssh //$NON-NLS-1$ c.cd(".."); //$NON-NLS-1$ diff --git a/bundles/org.eclipse.team.core/src/org/eclipse/team/core/Team.java b/bundles/org.eclipse.team.core/src/org/eclipse/team/core/Team.java index d25103c29..3b5145116 100644 --- a/bundles/org.eclipse.team.core/src/org/eclipse/team/core/Team.java +++ b/bundles/org.eclipse.team.core/src/org/eclipse/team/core/Team.java @@ -435,8 +435,7 @@ public final class Team { File f = pluginStateLocation.toFile(); if (!f.exists()) return false; try { - DataInputStream dis = new DataInputStream(new FileInputStream(f)); - try { + try (DataInputStream dis = new DataInputStream(new FileInputStream(f))) { int ignoreCount = 0; try { ignoreCount = dis.readInt(); @@ -450,8 +449,6 @@ public final class Team { boolean enabled = dis.readBoolean(); globalIgnore.put(pattern, Boolean.valueOf(enabled)); } - } finally { - dis.close(); } f.delete(); } catch (FileNotFoundException e) { diff --git a/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/CVSProviderPlugin.java b/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/CVSProviderPlugin.java index 70568ab3b..7903dd799 100644 --- a/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/CVSProviderPlugin.java +++ b/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/CVSProviderPlugin.java @@ -507,9 +507,9 @@ public class CVSProviderPlugin extends Plugin { File file = pluginStateLocation.toFile(); if (file.exists()) { try { - DataInputStream dis = new DataInputStream(new FileInputStream(file)); - readOldState(dis); - dis.close(); + try (DataInputStream dis = new DataInputStream(new FileInputStream(file))) { + readOldState(dis); + } // The file is no longer needed as the state is // persisted in the user settings file.delete(); diff --git a/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/client/TemplateHandler.java b/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/client/TemplateHandler.java index dd7783fac..22dfd4246 100644 --- a/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/client/TemplateHandler.java +++ b/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/client/TemplateHandler.java @@ -66,18 +66,13 @@ public class TemplateHandler extends ResponseHandler { IProgressMonitor monitor) throws CVSException { - try { - // Transfer the contents - OutputStream out = new ByteArrayOutputStream(); - try { - byte[] buffer = new byte[1024]; - int read; - while ((read = stream.read(buffer)) >= 0) { - Policy.checkCanceled(monitor); - out.write(buffer, 0, read); - } - } finally { - out.close(); + try ( // Transfer the contents + OutputStream out = new ByteArrayOutputStream()) { + byte[] buffer = new byte[1024]; + int read; + while ((read = stream.read(buffer)) >= 0) { + Policy.checkCanceled(monitor); + out.write(buffer, 0, read); } } catch (IOException e) { throw CVSException.wrapException(e); diff --git a/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/util/SyncFileWriter.java b/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/util/SyncFileWriter.java index 5bdbdcce3..a6f8c2814 100644 --- a/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/util/SyncFileWriter.java +++ b/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/util/SyncFileWriter.java @@ -491,13 +491,10 @@ public class SyncFileWriter { try { InputStream in = getInputStream(file); if (in != null) { - BufferedReader reader = new BufferedReader(new InputStreamReader(in), 512); - try { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(in), 512)) { String line = reader.readLine(); if (line == null) return ""; //$NON-NLS-1$ return line; - } finally { - reader.close(); } } return null; diff --git a/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/repo/RepositoryManager.java b/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/repo/RepositoryManager.java index 6ef78e44d..3238e3e2c 100644 --- a/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/repo/RepositoryManager.java +++ b/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/repo/RepositoryManager.java @@ -405,13 +405,8 @@ public class RepositoryManager { IPath pluginStateLocation = CVSUIPlugin.getPlugin().getStateLocation().append(REPOSITORIES_VIEW_FILE); File file = pluginStateLocation.toFile(); if (file.exists()) { - try { - BufferedInputStream is = new BufferedInputStream(new FileInputStream(file)); - try { - readState(is); - } finally { - is.close(); - } + try (BufferedInputStream is = new BufferedInputStream(new FileInputStream(file))) { + readState(is); } catch (IOException e) { CVSUIPlugin.log(IStatus.ERROR, CVSUIMessages.RepositoryManager_ioException, e); } catch (TeamException e) { @@ -422,11 +417,8 @@ public class RepositoryManager { file = oldPluginStateLocation.toFile(); if (file.exists()) { try { - DataInputStream dis = new DataInputStream(new FileInputStream(file)); - try { + try (DataInputStream dis = new DataInputStream(new FileInputStream(file))) { readOldState(dis); - } finally { - dis.close(); } saveState(); file.delete(); @@ -442,13 +434,8 @@ public class RepositoryManager { IPath pluginStateLocation = CVSUIPlugin.getPlugin().getStateLocation().append(COMMENT_HIST_FILE); File file = pluginStateLocation.toFile(); if (!file.exists()) return; - try { - BufferedInputStream is = new BufferedInputStream(new FileInputStream(file)); - try { - readCommentHistory(is); - } finally { - is.close(); - } + try (BufferedInputStream is = new BufferedInputStream(new FileInputStream(file))) { + readCommentHistory(is); } catch (IOException e) { CVSUIPlugin.log(IStatus.ERROR, CVSUIMessages.RepositoryManager_ioException, e); } catch (TeamException e) { @@ -459,13 +446,8 @@ public class RepositoryManager { IPath pluginStateLocation = CVSUIPlugin.getPlugin().getStateLocation().append(COMMENT_TEMPLATES_FILE); File file = pluginStateLocation.toFile(); if (!file.exists()) return; - try { - BufferedInputStream is = new BufferedInputStream(new FileInputStream(file)); - try { - readCommentTemplates(is); - } finally { - is.close(); - } + try (BufferedInputStream is = new BufferedInputStream(new FileInputStream(file))) { + readCommentTemplates(is); } catch (IOException e) { CVSUIPlugin.log(IStatus.ERROR, CVSUIMessages.RepositoryManager_ioException, e); } catch (TeamException e) { @@ -478,11 +460,8 @@ public class RepositoryManager { File tempFile = pluginStateLocation.append(REPOSITORIES_VIEW_FILE + ".tmp").toFile(); //$NON-NLS-1$ File stateFile = pluginStateLocation.append(REPOSITORIES_VIEW_FILE).toFile(); try { - XMLWriter writer = new XMLWriter(new BufferedOutputStream(new FileOutputStream(tempFile))); - try { + try (XMLWriter writer = new XMLWriter(new BufferedOutputStream(new FileOutputStream(tempFile)))) { writeState(writer); - } finally { - writer.close(); } if (stateFile.exists()) { stateFile.delete(); @@ -600,11 +579,8 @@ public class RepositoryManager { File tempFile = pluginStateLocation.append(COMMENT_HIST_FILE + ".tmp").toFile(); //$NON-NLS-1$ File histFile = pluginStateLocation.append(COMMENT_HIST_FILE).toFile(); try { - XMLWriter writer = new XMLWriter(new BufferedOutputStream(new FileOutputStream(tempFile))); - try { + try (XMLWriter writer = new XMLWriter(new BufferedOutputStream(new FileOutputStream(tempFile)))) { writeCommentHistory(writer); - } finally { - writer.close(); } if (histFile.exists()) { histFile.delete(); @@ -944,12 +920,9 @@ public class RepositoryManager { File histFile = pluginStateLocation.append(COMMENT_TEMPLATES_FILE) .toFile(); try { - XMLWriter writer = new XMLWriter(new BufferedOutputStream( - new FileOutputStream(tempFile))); - try { + try (XMLWriter writer = new XMLWriter(new BufferedOutputStream( + new FileOutputStream(tempFile)))) { writeCommentTemplates(writer); - } finally { - writer.close(); } if (histFile.exists()) { histFile.delete(); diff --git a/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/ProjectSetImporter.java b/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/ProjectSetImporter.java index d8bcb7f67..bd87b9d44 100644 --- a/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/ProjectSetImporter.java +++ b/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/ProjectSetImporter.java @@ -235,16 +235,10 @@ public class ProjectSetImporter { private static XMLMemento stringToXMLMemento(String stringContents) throws InvocationTargetException { - StringReader reader = null; - try { - reader = new StringReader(stringContents); + try (StringReader reader = new StringReader(stringContents)) { return XMLMemento.createReadRoot(reader); } catch (WorkbenchException e) { throw new InvocationTargetException(e); - } finally { - if (reader != null) { - reader.close(); - } } } diff --git a/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/synchronize/SynchronizeManager.java b/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/synchronize/SynchronizeManager.java index 7cd1eb76d..2a3cd25a1 100644 --- a/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/synchronize/SynchronizeManager.java +++ b/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/synchronize/SynchronizeManager.java @@ -641,11 +641,8 @@ public class SynchronizeManager implements ISynchronizeManager { ref.save(participantData); } try { - Writer writer = new BufferedWriter(new FileWriter(getStateFile())); - try { + try (Writer writer = new BufferedWriter(new FileWriter(getStateFile()))) { xmlMemento.save(writer); - } finally { - writer.close(); } } catch (IOException e) { TeamUIPlugin.log(new Status(IStatus.ERROR, TeamUIPlugin.ID, 1, TeamUIMessages.SynchronizeManager_10, e)); diff --git a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/FileSystemOperations.java b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/FileSystemOperations.java index 46276b40a..91afa6e69 100644 --- a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/FileSystemOperations.java +++ b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/FileSystemOperations.java @@ -272,21 +272,15 @@ public class FileSystemOperations { // so nothing needs to be done return; } - try { - //Copy from the local file to the remote file: - InputStream source = null; - try { - // Get the remote file content. - source = remote.getContents(); - // Set the local file content to be the same as the remote file. - if (localFile.exists()) - localFile.setContents(source, false, false, progress); - else - localFile.create(source, false, progress); - } finally { - if (source != null) - source.close(); - } + // Copy from the local file to the remote file: + // Get the remote file content. + try (InputStream source = remote.getContents()) { + // Set the local file content to be the same as the remote file. + if (localFile.exists()) + localFile.setContents(source, false, false, progress); + else + localFile.create(source, false, progress); + // Mark as read-only to force a checkout before editing localFile.setReadOnly(true); synchronizer.setBaseBytes(localFile, remote.asBytes()); diff --git a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/pessimistic/PessimisticFilesystemProvider.java b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/pessimistic/PessimisticFilesystemProvider.java index e85f5fff1..f7a8b7e41 100644 --- a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/pessimistic/PessimisticFilesystemProvider.java +++ b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/pessimistic/PessimisticFilesystemProvider.java @@ -602,12 +602,9 @@ public class PessimisticFilesystemProvider extends RepositoryProvider { public static String getFileContents(IFile file) throws IOException, CoreException { StringBuilder buf = new StringBuilder(); - Reader reader = new InputStreamReader(new BufferedInputStream(file.getContents())); - try { + try (Reader reader = new InputStreamReader(new BufferedInputStream(file.getContents()))) { int c; while ((c = reader.read()) != -1) buf.append((char)c); - } finally { - reader.close(); } return buf.toString(); } diff --git a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/CVSTestSetup.java b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/CVSTestSetup.java index bc3180255..1f76a79d0 100644 --- a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/CVSTestSetup.java +++ b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/CVSTestSetup.java @@ -13,17 +13,30 @@ *******************************************************************************/ package org.eclipse.team.tests.ccvs.core; -import java.io.*; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.PrintStream; -import junit.extensions.TestSetup; -import junit.framework.Test; - -import org.eclipse.core.runtime.*; -import org.eclipse.team.internal.ccvs.core.*; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.OperationCanceledException; +import org.eclipse.core.runtime.Platform; +import org.eclipse.team.internal.ccvs.core.CVSException; +import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin; +import org.eclipse.team.internal.ccvs.core.CVSStatus; +import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation; import org.eclipse.team.internal.ccvs.core.connection.CVSCommunicationException; import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation; import org.eclipse.team.internal.ccvs.core.util.KnownRepositories; +import junit.extensions.TestSetup; +import junit.framework.Test; + public class CVSTestSetup extends TestSetup { public static final String REPOSITORY_LOCATION; public static final boolean INITIALIZE_REPO; @@ -62,18 +75,14 @@ public class CVSTestSetup extends TestSetup { if (propertiesFile == null) return; File file = new File(propertiesFile); if (file.isDirectory()) file = new File(file, "repository.properties"); - try { - BufferedReader reader = new BufferedReader(new FileReader(file)); - try { - for (String line; (line = reader.readLine()) != null; ) { - if (line.startsWith("#")) continue; - int sep = line.indexOf("="); - String property = line.substring(0, sep).trim(); - String value = line.substring(sep + 1).trim(); - System.setProperty("eclipse.cvs." + property, value); - } - } finally { - reader.close(); + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + for (String line; (line = reader.readLine()) != null;) { + if (line.startsWith("#")) + continue; + int sep = line.indexOf("="); + String property = line.substring(0, sep).trim(); + String value = line.substring(sep + 1).trim(); + System.setProperty("eclipse.cvs." + property, value); } } catch (Exception e) { System.err.println("Could not read repository properties file: " + file.getAbsolutePath()); diff --git a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/EclipseTest.java b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/EclipseTest.java index a6cdf88e1..6c22b010d 100644 --- a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/EclipseTest.java +++ b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/EclipseTest.java @@ -271,12 +271,9 @@ public class EclipseTest extends ResourceTest { public static String getFileContents(IFile file) throws IOException, CoreException { StringBuilder buf = new StringBuilder(); - Reader reader = new InputStreamReader(new BufferedInputStream(file.getContents())); - try { + try (Reader reader = new InputStreamReader(new BufferedInputStream(file.getContents()))) { int c; while ((c = reader.read()) != -1) buf.append((char)c); - } finally { - reader.close(); } return buf.toString(); } diff --git a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/JUnitTestCase.java b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/JUnitTestCase.java index 5e018d788..2cc2d7bbf 100644 --- a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/JUnitTestCase.java +++ b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/JUnitTestCase.java @@ -144,8 +144,7 @@ public abstract class JUnitTestCase extends TestCase { protected static void writeToFile(IFile file, String[] contents) throws IOException, CoreException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); - PrintStream os = new PrintStream(bos); - try { + try (PrintStream os = new PrintStream(bos)) { for (String content : contents) { os.println(content); } @@ -156,8 +155,6 @@ public abstract class JUnitTestCase extends TestCase { mkdirs(file.getParent()); file.create(bis, false /*force*/, null); } - } finally { - os.close(); } } diff --git a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/compatible/SameResultEnv.java b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/compatible/SameResultEnv.java index 1d72833ac..fe7aed3c4 100644 --- a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/compatible/SameResultEnv.java +++ b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/compatible/SameResultEnv.java @@ -369,22 +369,18 @@ public final class SameResultEnv extends JUnitTestCase { assertEquals(mFile1.isReadOnly(), mFile2.isReadOnly()); // Compare the content of the files - try { - InputStream in1 = mFile1.getContents(); - InputStream in2 = mFile2.getContents(); - byte[] buffer1 = new byte[(int)mFile1.getSize()]; - byte[] buffer2 = new byte[(int)mFile2.getSize()]; + byte[] buffer1 = new byte[(int) mFile1.getSize()]; + byte[] buffer2 = new byte[(int) mFile2.getSize()]; + try (InputStream in1 = mFile1.getContents(); InputStream in2 = mFile2.getContents()) { // This is not the right way to do it, because the Stream // may read less than the whole file in1.read(buffer1); in2.read(buffer2); - in1.close(); - in2.close(); - assertEquals("Length differs for file " + mFile1.getName(), buffer1.length, buffer2.length); - assertEquals("Contents differs for file " + mFile1.getName(), new String(buffer1),new String(buffer2)); } catch (IOException e) { throw new CVSException("Error in TestCase"); } + assertEquals("Length differs for file " + mFile1.getName(), buffer1.length, buffer2.length); + assertEquals("Contents differs for file " + mFile1.getName(), new String(buffer1),new String(buffer2)); // We can not do the check, because the reference client does // check out dirty files ?!? diff --git a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/provider/ModuleTest.java b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/provider/ModuleTest.java index 80cfcf3c1..b3a0b3bd3 100644 --- a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/provider/ModuleTest.java +++ b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/provider/ModuleTest.java @@ -82,11 +82,8 @@ public class ModuleTest extends EclipseTest { waitMsec(1000); IProject cvsroot = checkoutProject(null, "CVSROOT", null); - InputStream in = url.openStream(); - try { + try (InputStream in = url.openStream()) { cvsroot.getFile("modules").setContents(in, false, false, DEFAULT_MONITOR); - } finally { - in.close(); } commitProject(cvsroot); diff --git a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/provider/ResourceDeltaTest.java b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/provider/ResourceDeltaTest.java index c4a03dcff..cc44611b9 100644 --- a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/provider/ResourceDeltaTest.java +++ b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/provider/ResourceDeltaTest.java @@ -156,15 +156,12 @@ public class ResourceDeltaTest extends EclipseTest { // wait to ensure the timestamp differs from the one Core has waitMsec(1500); InputStream in = new BufferedInputStream(getRandomContents()); - OutputStream out = new BufferedOutputStream(new FileOutputStream(ioFile)); - try { + try (OutputStream out = new BufferedOutputStream(new FileOutputStream(ioFile))) { int next = in.read(); while (next != -1) { out.write(next); next = in.read(); } - } finally { - out.close(); } } diff --git a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/ProjectSetImporterTests.java b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/ProjectSetImporterTests.java index 1012510c3..8e91640bc 100644 --- a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/ProjectSetImporterTests.java +++ b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/ProjectSetImporterTests.java @@ -94,10 +94,8 @@ public class ProjectSetImporterTests extends EclipseTest { IProject project2 = createProject("testImportProject", new String[] { "file.txt", "folder1/", "folder1/a.txt" }); - PrintWriter out = null; - try { - out = new PrintWriter(new BufferedWriter(new FileWriter(PSF_FILE)), - true); + try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(PSF_FILE)), + true)) { out.println(psf_header_0); out.println(psf_header_1); @@ -123,9 +121,6 @@ public class ProjectSetImporterTests extends EclipseTest { fail("1.", e.getCause()); } catch (IOException e) { fail("2.", e); - } finally { - if (out != null) - out.close(); } } @@ -136,11 +131,9 @@ public class ProjectSetImporterTests extends EclipseTest { IProject project2 = createProject("testBug234149_aFewProviders", new String[0]); - // create psf with two providers - PrintWriter out = null; - try { - out = new PrintWriter(new BufferedWriter(new FileWriter(PSF_FILE)), - true); + try ( // create psf with two providers + PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(PSF_FILE)), + true)) { // add first provider to psf out.println(psf_header_0); @@ -172,9 +165,6 @@ public class ProjectSetImporterTests extends EclipseTest { fail("1.", e.getCause()); } catch (IOException e) { fail("2.", e); - } finally { - if (out != null) - out.close(); } } diff --git a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/benchmark/BenchmarkUtils.java b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/benchmark/BenchmarkUtils.java index e86cb12a6..0da269355 100644 --- a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/benchmark/BenchmarkUtils.java +++ b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/benchmark/BenchmarkUtils.java @@ -156,18 +156,19 @@ public class BenchmarkUtils { fileSize = (int) Math.abs(gen.nextGaussian() * variance + meanSize); } while (fileSize > meanSize + variance * 4); // avoid huge files - ByteArrayOutputStream os = new ByteArrayOutputStream(); - String fileName; - if (gen.nextInt(100) < probBinary) { - fileName = makeUniqueName(gen, "file", "class"); // binary - writeRandomBytes(gen, os, fileSize); - } else { - fileName = makeUniqueName(gen, "file", "txt"); // text - writeRandomText(gen, os, fileSize); + IFile file; + try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { + String fileName; + if (gen.nextInt(100) < probBinary) { + fileName = makeUniqueName(gen, "file", "class"); // binary + writeRandomBytes(gen, os, fileSize); + } else { + fileName = makeUniqueName(gen, "file", "txt"); // text + writeRandomText(gen, os, fileSize); + } + file = parent.getFile(new Path(fileName)); + file.create(new ByteArrayInputStream(os.toByteArray()), true, new NullProgressMonitor()); } - IFile file = parent.getFile(new Path(fileName)); - file.create(new ByteArrayInputStream(os.toByteArray()), true, new NullProgressMonitor()); - os.close(); return file; } @@ -221,10 +222,7 @@ public class BenchmarkUtils { */ public static void modifyFile(SequenceGenerator gen, IFile file) throws IOException, CoreException { - ByteArrayOutputStream os = new ByteArrayOutputStream(); - try { - InputStream is = file.getContents(true); - try { + try (ByteArrayOutputStream os = new ByteArrayOutputStream(); InputStream is = file.getContents(true)) { byte[] buffer = new byte[8192]; int rsize; boolean changed = false; @@ -240,11 +238,6 @@ public class BenchmarkUtils { } if (! changed) os.write('!'); // make sure we actually did change the file file.setContents(new ByteArrayInputStream(os.toByteArray()), false /*force*/, true /*keepHistory*/, null); - } finally { - is.close(); - } - } finally { - os.close(); } } diff --git a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/benchmark/Bug152581Test.java b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/benchmark/Bug152581Test.java index 239b09c32..79f82a94c 100644 --- a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/benchmark/Bug152581Test.java +++ b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/benchmark/Bug152581Test.java @@ -93,8 +93,7 @@ public class Bug152581Test extends BenchmarkTest { private IProject createProject(String filename) throws IOException, CoreException { File file = BenchmarkTestSetup.getTestFile(filename + ".txt"); - InputStream content = getContents(file, "Could not read seed file " + filename + ".txt"); - try { + try (InputStream content = getContents(file, "Could not read seed file " + filename + ".txt")) { BufferedReader reader = new BufferedReader(new InputStreamReader(content)); IProject project = getUniqueTestProject(filename); populateProject(reader, project); @@ -102,8 +101,6 @@ public class Bug152581Test extends BenchmarkTest { // Perform an update to prune any empty directories updateProject(project, null, false); return project; - } finally { - content.close(); } } |