Skip to content

Commit 936c41e

Browse files
committed
Bug: warning 'Resource leak: 'reader' is never closed' for FileUtil.java
Fix: close the FileStream
1 parent 399b61e commit 936c41e

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

  • sources/net.sf.j2s.core/src/net/sf/j2s/core/compiler

sources/net.sf.j2s.core/src/net/sf/j2s/core/compiler/FileUtil.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ public class FileUtil {
99

1010
public static String readSource(File f) {
1111
StringBuffer sb = new StringBuffer();
12+
FileReader reader = null;
1213
try {
13-
FileReader reader = new FileReader(f);
14+
reader = new FileReader(f);
1415
char[] buf = new char[1024];
1516
int read = reader.read(buf);
1617
while (read != -1) {
@@ -21,8 +22,27 @@ public static String readSource(File f) {
2122
e.printStackTrace();
2223
} catch (IOException e) {
2324
e.printStackTrace();
25+
} finally {
26+
close(reader);
2427
}
2528
return sb.toString();
2629
}
30+
31+
/**
32+
* Close the given FileReader.
33+
*
34+
* <p>In case of an error the exception and its backtrace is written to the standard error stream.
35+
*
36+
* @param fileReader null or the FileReader to close
37+
*/
38+
public static void close(FileReader fileReader) {
39+
if (fileReader != null) {
40+
try {
41+
fileReader.close();
42+
} catch (IOException e) {
43+
e.printStackTrace();
44+
}
45+
}
46+
}
2747
}
2848

0 commit comments

Comments
 (0)