diff options
author | Alexander Kriegisch <Alexander@Kriegisch.name> | 2021-03-11 13:23:29 +0700 |
---|---|---|
committer | Alexander Kriegisch <Alexander@Kriegisch.name> | 2021-03-11 13:23:29 +0700 |
commit | f996d0247c3f695cde7d220289438d6c8e89bd0c (patch) | |
tree | 5055cf722753c36606bd903d1422e4f313b17786 | |
parent | 35acb11f77a4649fc844676693e1b6a5adc7fab6 (diff) | |
download | aspectj-f996d0247c3f695cde7d220289438d6c8e89bd0c.tar.gz aspectj-f996d0247c3f695cde7d220289438d6c8e89bd0c.zip |
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 <Alexander@Kriegisch.name>
-rw-r--r-- | util/src/main/java/org/aspectj/util/FileUtil.java | 39 |
1 files changed, 15 insertions, 24 deletions
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(); - } } } |