From e636b977a94cd9b00d5f5437578a08e0945d8ff5 Mon Sep 17 00:00:00 2001 From: Alexander Kurtakov Date: Wed, 2 Dec 2015 22:58:45 +0200 Subject: [PATCH] core.tests: Ensure zipFile is closed. Try-with-resources to ensure everything is properly closed. Change-Id: I5c0d286b926a7ff86d48f943347929de03de5f0e Signed-off-by: Alexander Kurtakov --- .../cdt/core/suite/ProjectCreator.java | 62 +++++++++---------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/suite/org/eclipse/cdt/core/suite/ProjectCreator.java b/core/org.eclipse.cdt.core.tests/suite/org/eclipse/cdt/core/suite/ProjectCreator.java index 47d90093b79..591f2036a45 100644 --- a/core/org.eclipse.cdt.core.tests/suite/org/eclipse/cdt/core/suite/ProjectCreator.java +++ b/core/org.eclipse.cdt.core.tests/suite/org/eclipse/cdt/core/suite/ProjectCreator.java @@ -44,41 +44,41 @@ public class ProjectCreator extends TestCase { private static final IProgressMonitor monitor = new NullProgressMonitor(); public static IProject createProject(IPath zipPath, String projectName) throws Exception { - ZipFile zipFile = new ZipFile(CTestPlugin.getDefault().getFileInPlugin(zipPath)); - - IWorkspace workspace = ResourcesPlugin.getWorkspace(); - IWorkspaceRoot root = workspace.getRoot(); - IPath rootPath = root.getLocation(); - String zipProjectName = null; + try (ZipFile zipFile = new ZipFile(CTestPlugin.getDefault().getFileInPlugin(zipPath))) { + IWorkspace workspace = ResourcesPlugin.getWorkspace(); + IWorkspaceRoot root = workspace.getRoot(); + IPath rootPath = root.getLocation(); + String zipProjectName = null; - Enumeration entries = zipFile.entries(); - while (entries.hasMoreElements()) { - ZipEntry entry = (ZipEntry)entries.nextElement(); - if (!entry.isDirectory()) { - IPath entryPath = rootPath.append(entry.getName()); - IPath entryDir = entryPath.removeLastSegments(1); - entryDir.toFile().mkdirs(); - InputStream in = zipFile.getInputStream(entry); - OutputStream out = new FileOutputStream(entryPath.toFile()); - for (int n = in.read(buffer); n >= 0; n = in.read(buffer)) - out.write(buffer, 0, n); - in.close(); - out.close(); - - // Is this the .project file? - if (".project".equals(entryPath.lastSegment())) { - IProjectDescription desc = workspace.loadProjectDescription(entryPath); - zipProjectName = desc.getName(); + Enumeration entries = zipFile.entries(); + while (entries.hasMoreElements()) { + ZipEntry entry = (ZipEntry) entries.nextElement(); + if (!entry.isDirectory()) { + IPath entryPath = rootPath.append(entry.getName()); + IPath entryDir = entryPath.removeLastSegments(1); + entryDir.toFile().mkdirs(); + try (InputStream in = zipFile.getInputStream(entry); + OutputStream out = new FileOutputStream(entryPath.toFile())) { + for (int n = in.read(buffer); n >= 0; n = in.read(buffer)) { + out.write(buffer, 0, n); + } + } + + // Is this the .project file? + if (".project".equals(entryPath.lastSegment())) { + IProjectDescription desc = workspace.loadProjectDescription(entryPath); + zipProjectName = desc.getName(); + } } } - } - - IProject project = root.getProject(zipProjectName); - project.create(monitor); - project.open(monitor); - project.move(new Path(projectName), true, monitor); - return project; + IProject project = root.getProject(zipProjectName); + project.create(monitor); + project.open(monitor); + project.move(new Path(projectName), true, monitor); + + return project; + } } public static IProject createCManagedProject(String projectName) throws Exception {