From: Alexander Kriegisch Date: Thu, 11 Mar 2021 06:23:29 +0000 (+0700) Subject: Fix resource leak in FileUtil X-Git-Tag: java16-add-opens~36^2~6 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f996d0247c3f695cde7d220289438d6c8e89bd0c;p=aspectj.git Fix resource leak in FileUtil This made ModuleTests.testBuildModuleAndApplyAspectsFromAspectPath fail because a file delete job for a module JAR failed after a previous compile job had called FileUtil.isZipFile(File) in which the opened zip file was never closed. A try with resources fixes that. Maybe the corresponding test worked on Linux before, I did not try. I just know that Linux is more forgiving about deleting open files while on Windows they are being locked, which makes Windows the better system to search for open file leaks. Signed-off-by: Alexander Kriegisch --- diff --git a/util/src/main/java/org/aspectj/util/FileUtil.java b/util/src/main/java/org/aspectj/util/FileUtil.java index 34b2cc17f..91686dd13 100644 --- a/util/src/main/java/org/aspectj/util/FileUtil.java +++ b/util/src/main/java/org/aspectj/util/FileUtil.java @@ -1,14 +1,14 @@ /* ******************************************************************* - * Copyright (c) 1999-2001 Xerox Corporation, + * Copyright (c) 1999-2001 Xerox Corporation, * 2002 Palo Alto Research Center, Incorporated (PARC). - * All rights reserved. - * This program and the accompanying materials are made available - * under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Xerox/PARC initial implementation + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Xerox/PARC initial implementation * ******************************************************************/ package org.aspectj.util; @@ -96,8 +96,10 @@ public class FileUtil { /** @return true if file exists and is a zip file */ public static boolean isZipFile(File file) { - try { - return (null != file) && new ZipFile(file) != null; + if (file == null) + return false; + try (ZipFile zipFile = new ZipFile(file)) { + return true; } catch (IOException e) { return false; } @@ -428,7 +430,7 @@ public class FileUtil { } try { File f = new File(path); - + if (f.exists() && f.canRead()) { if (mustBeJar && !f.isDirectory()) { result = FileUtil.getBestFile(f); @@ -890,19 +892,8 @@ public class FileUtil { * @throws IOException */ public static void copyValidFiles(File fromFile, File toFile) throws IOException { - FileInputStream in = null; - FileOutputStream out = null; - try { - in = new FileInputStream(fromFile); - out = new FileOutputStream(toFile); + try (FileInputStream in = new FileInputStream(fromFile); FileOutputStream out = new FileOutputStream(toFile)){ copyStream(in, out); - } finally { - if (out != null) { - out.close(); - } - if (in != null) { - in.close(); - } } }