Browse Source

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>
tags/java16-add-opens
Alexander Kriegisch 3 years ago
parent
commit
f996d0247c
1 changed files with 15 additions and 24 deletions
  1. 15
    24
      util/src/main/java/org/aspectj/util/FileUtil.java

+ 15
- 24
util/src/main/java/org/aspectj/util/FileUtil.java View File

@@ -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();
}
}
}


Loading…
Cancel
Save