提交 8d7a5a8d authored 作者: noelgrandin's avatar noelgrandin

make Eclipse's resource-close checker a little happier.

Also remove an unnecesssary copy of the closeSilently() method
上级 80bf2074
...@@ -10,6 +10,7 @@ import java.io.BufferedReader; ...@@ -10,6 +10,7 @@ import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.EOFException; import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
...@@ -38,7 +39,7 @@ public class IOUtils { ...@@ -38,7 +39,7 @@ public class IOUtils {
* *
* @param out the output stream or null * @param out the output stream or null
*/ */
public static void closeSilently(OutputStream out) { public static void closeSilently(Closeable out) {
if (out != null) { if (out != null) {
try { try {
trace("closeSilently", null, out); trace("closeSilently", null, out);
......
...@@ -7,11 +7,11 @@ ...@@ -7,11 +7,11 @@
package org.h2.test.coverage; package org.h2.test.coverage;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.LineNumberReader; import java.io.LineNumberReader;
import org.h2.util.IOUtils;
/** /**
* The class used at runtime to measure the code usage and performance. * The class used at runtime to measure the code usage and performance.
...@@ -30,10 +30,9 @@ public class Profile extends Thread { ...@@ -30,10 +30,9 @@ public class Profile extends Thread {
private BufferedWriter trace; private BufferedWriter trace;
private Profile() { private Profile() {
FileReader reader = null; LineNumberReader r = null;
try { try {
reader = new FileReader("profile.txt"); r = new LineNumberReader(new FileReader("profile.txt"));
LineNumberReader r = new LineNumberReader(reader);
while (r.readLine() != null) { while (r.readLine() != null) {
// nothing - just count lines // nothing - just count lines
} }
...@@ -46,7 +45,7 @@ public class Profile extends Thread { ...@@ -46,7 +45,7 @@ public class Profile extends Thread {
e.printStackTrace(); e.printStackTrace();
System.exit(1); System.exit(1);
} finally { } finally {
closeSilently(reader); IOUtils.closeSilently(r);
} }
} }
...@@ -107,16 +106,6 @@ public class Profile extends Thread { ...@@ -107,16 +106,6 @@ public class Profile extends Thread {
} }
} }
private static void closeSilently(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
// ignore
}
}
}
private void addVisit(int i) { private void addVisit(int i) {
if (stop) { if (stop) {
return; return;
...@@ -143,13 +132,11 @@ public class Profile extends Thread { ...@@ -143,13 +132,11 @@ public class Profile extends Thread {
printLine('='); printLine('=');
print("NOT COVERED"); print("NOT COVERED");
printLine('-'); printLine('-');
FileReader reader = null; LineNumberReader r = null;
FileWriter fileWriter = null; BufferedWriter writer = null;
try { try {
reader = new FileReader("profile.txt"); r = new LineNumberReader(new FileReader("profile.txt"));
LineNumberReader r = new LineNumberReader(reader); writer = new BufferedWriter(new FileWriter("notCovered.txt"));
fileWriter = new FileWriter("notCovered.txt");
BufferedWriter writer = new BufferedWriter(fileWriter);
int unvisited = 0; int unvisited = 0;
int unvisitedThrow = 0; int unvisitedThrow = 0;
for (int i = 0; i < maxIndex; i++) { for (int i = 0; i < maxIndex; i++) {
...@@ -170,8 +157,8 @@ public class Profile extends Thread { ...@@ -170,8 +157,8 @@ public class Profile extends Thread {
print("Not covered: " + percent + " % " + " (" + unvisited + " of " + maxIndex + "; throw=" print("Not covered: " + percent + " % " + " (" + unvisited + " of " + maxIndex + "; throw="
+ unvisitedThrow + ")"); + unvisitedThrow + ")");
} finally { } finally {
closeSilently(fileWriter); IOUtils.closeSilently(writer);
closeSilently(reader); IOUtils.closeSilently(r);
} }
} }
...@@ -207,10 +194,9 @@ public class Profile extends Thread { ...@@ -207,10 +194,9 @@ public class Profile extends Thread {
list[bigIndex] = -(big + 1); list[bigIndex] = -(big + 1);
index[i] = bigIndex; index[i] = bigIndex;
} }
FileReader reader = null; LineNumberReader r = null;
try { try {
reader = new FileReader("profile.txt"); r = new LineNumberReader(new FileReader("profile.txt"));
LineNumberReader r = new LineNumberReader(reader);
for (int i = 0; i < maxIndex; i++) { for (int i = 0; i < maxIndex; i++) {
String line = r.readLine(); String line = r.readLine();
int k = list[i]; int k = list[i];
...@@ -229,7 +215,7 @@ public class Profile extends Thread { ...@@ -229,7 +215,7 @@ public class Profile extends Thread {
print(text[i]); print(text[i]);
} }
} finally { } finally {
closeSilently(reader); IOUtils.closeSilently(r);
} }
} }
......
...@@ -36,6 +36,7 @@ import java.util.zip.CRC32; ...@@ -36,6 +36,7 @@ import java.util.zip.CRC32;
import java.util.zip.Deflater; import java.util.zip.Deflater;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import org.h2.util.IOUtils;
/** /**
* This class is a complete pure Java build tool. It allows to build this * This class is a complete pure Java build tool. It allows to build this
...@@ -695,8 +696,9 @@ public class BuildBase { ...@@ -695,8 +696,9 @@ public class BuildBase {
* @return the data * @return the data
*/ */
public static byte[] readFile(File file) { public static byte[] readFile(File file) {
RandomAccessFile ra = null;
try { try {
RandomAccessFile ra = new RandomAccessFile(file, "r"); ra = new RandomAccessFile(file, "r");
long len = ra.length(); long len = ra.length();
if (len >= Integer.MAX_VALUE) { if (len >= Integer.MAX_VALUE) {
throw new RuntimeException("File " + file.getPath() + " is too large"); throw new RuntimeException("File " + file.getPath() + " is too large");
...@@ -707,6 +709,8 @@ public class BuildBase { ...@@ -707,6 +709,8 @@ public class BuildBase {
return buffer; return buffer;
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Error reading from file " + file, e); throw new RuntimeException("Error reading from file " + file, e);
} finally {
IOUtils.closeSilently(ra);
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论