Commit 83da113b authored by Yanjun Jiang's avatar Yanjun Jiang
Browse files

Make EventBFileWriter safe from exceptions.

Make sure all closeable objects in EventBFileWriter are properly closed,
even in the presence of exceptions.
parent 51d4fec4
Loading
Loading
Loading
Loading
+30 −11
Original line number Diff line number Diff line
@@ -28,10 +28,7 @@
 */
package ca.mcscert.jtet.eventbgenerator;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.*;

/**
 *
@@ -39,14 +36,36 @@ import java.io.OutputStreamWriter;
 */
final public class EventBFileWriter {

    public static void writeEventBFile(String fileName, String fileContent) {
    public static void writeEventBBumFile(String fileName, String content) throws IOException {
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(fileName);
            OutputStreamWriter osw = null;

            try {
            BufferedWriter br = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName + ".bum"), "UTF8"));
            br.write(fileContent);
            br.flush();
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
                osw = new OutputStreamWriter(fos, "UTF8");
                BufferedWriter bw = null;

                try {
                    bw = new BufferedWriter(osw);
                    bw.write(content);
                    bw.flush();
                } finally {
                    if (bw != null) {
                        bw.close();
                    }
                }

            } finally {
                if (osw != null) {
                    osw.close();
                }
            }
        } finally {
            if (fos != null) {
                fos.close();
            }
        }
    }
}