BuildCheckpoints: Wrap streams in try/finally block to avoid possible resource leaks.
@@ -172,11 +172,11 @@ public class BuildCheckpoints {
}
private static void writeBinaryCheckpoints(TreeMap<Integer, StoredBlock> checkpoints, File file) throws Exception {
- final FileOutputStream fileOutputStream = new FileOutputStream(file, false);
MessageDigest digest = Sha256Hash.newDigest();
- final DigestOutputStream digestOutputStream = new DigestOutputStream(fileOutputStream, digest);
+ try (FileOutputStream fileOutputStream = new FileOutputStream(file, false);
+ DigestOutputStream digestOutputStream = new DigestOutputStream(fileOutputStream, digest);
+ DataOutputStream dataOutputStream = new DataOutputStream(digestOutputStream)) {
digestOutputStream.on(false);
- final DataOutputStream dataOutputStream = new DataOutputStream(digestOutputStream);
dataOutputStream.writeBytes("CHECKPOINTS 1");
dataOutputStream.writeInt(0); // Number of signatures to read. Do this later.
digestOutputStream.on(true);
@@ -187,16 +187,16 @@ public class BuildCheckpoints {
dataOutputStream.write(buffer.array());
buffer.position(0);
}
- dataOutputStream.close();
Sha256Hash checkpointsHash = Sha256Hash.wrap(digest.digest());
System.out.println("Hash of checkpoints data is " + checkpointsHash);
- digestOutputStream.close();
- fileOutputStream.close();
System.out.println("Checkpoints written to '" + file.getCanonicalPath() + "'.");
}
+ }
- private static void writeTextualCheckpoints(TreeMap<Integer, StoredBlock> checkpoints, File file) throws IOException {
- PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.US_ASCII));
+ private static void writeTextualCheckpoints(TreeMap<Integer, StoredBlock> checkpoints, File file)
+ throws IOException {
+ try (PrintWriter writer = new PrintWriter(
+ new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.US_ASCII))) {
writer.println("TXT CHECKPOINTS 1");
writer.println("0"); // Number of signatures to read. Do this later.
writer.println(checkpoints.size());
@@ -206,9 +206,9 @@ public class BuildCheckpoints {
writer.println(CheckpointManager.BASE64.encode(buffer.array()));
buffer.position(0);
}
- writer.close();
System.out.println("Checkpoints written to '" + file.getCanonicalPath() + "'.");
}
+ }
private static void sanityCheck(File file, int expectedSize) throws IOException {
FileInputStream fis = new FileInputStream(file);
