avoid resource leak upon write() exception (Coverity)

This commit is contained in:
Terence Parr 2013-08-31 17:31:17 -07:00
parent be0d6b3fce
commit ba6c711e85
2 changed files with 13 additions and 5 deletions

View File

@ -30,7 +30,7 @@
package org.antlr.v4.runtime.misc; package org.antlr.v4.runtime.misc;
import java.awt.Window; import java.awt.*;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.io.BufferedWriter; import java.io.BufferedWriter;
@ -94,8 +94,12 @@ public class Utils {
public static void writeFile(String fileName, String content) throws IOException { public static void writeFile(String fileName, String content) throws IOException {
FileWriter fw = new FileWriter(fileName); FileWriter fw = new FileWriter(fileName);
Writer w = new BufferedWriter(fw); Writer w = new BufferedWriter(fw);
w.write(content); try {
w.close(); w.write(content);
}
finally {
w.close();
}
} }
public static void waitForClose(final Window window) throws InterruptedException { public static void waitForClose(final Window window) throws InterruptedException {

View File

@ -68,8 +68,12 @@ public class Trees {
String ps = getPS(t, ruleNames, fontName, fontSize); String ps = getPS(t, ruleNames, fontName, fontSize);
FileWriter f = new FileWriter(fileName); FileWriter f = new FileWriter(fileName);
BufferedWriter bw = new BufferedWriter(f); BufferedWriter bw = new BufferedWriter(f);
bw.write(ps); try {
bw.close(); bw.write(ps);
}
finally {
bw.close();
}
} }
public static void writePS(Tree t, @Nullable List<String> ruleNames, String fileName) public static void writePS(Tree t, @Nullable List<String> ruleNames, String fileName)