From a21feceaf2efaeaeae3a69b617f9eaf639e95a1f Mon Sep 17 00:00:00 2001
From: Nelson Tavares de Sousa <stu103017@mail.uni-kiel.de>
Date: Mon, 27 Oct 2014 16:10:03 +0100
Subject: [PATCH] fixed bug #59 in ByteArrayFileWriter

---
 .../teetime/stage/io/ByteArrayFileWriter.java     | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/main/java/teetime/stage/io/ByteArrayFileWriter.java b/src/main/java/teetime/stage/io/ByteArrayFileWriter.java
index 3df492b4..641bd5e2 100644
--- a/src/main/java/teetime/stage/io/ByteArrayFileWriter.java
+++ b/src/main/java/teetime/stage/io/ByteArrayFileWriter.java
@@ -11,11 +11,13 @@ import com.google.common.io.Files;
 public class ByteArrayFileWriter extends ConsumerStage<byte[]> {
 
 	private final File file;
+	private FileOutputStream fo;
 
 	public ByteArrayFileWriter(final File file) {
 		this.file = file;
 		try {
 			Files.touch(file);
+			fo = new FileOutputStream(this.file);
 		} catch (IOException e) {
 			e.printStackTrace();
 		}
@@ -23,13 +25,20 @@ public class ByteArrayFileWriter extends ConsumerStage<byte[]> {
 
 	@Override
 	protected void execute(final byte[] element) {
-		FileOutputStream fo;
+
 		try {
-			fo = new FileOutputStream(this.file);
 			fo.write(element);
-			fo.close();
 		} catch (Exception e) {
 			throw new IllegalStateException(e);
 		}
 	}
+
+	@Override
+	public void onTerminating() {
+		try {
+			fo.close();
+		} catch (IOException e) {
+			throw new IllegalStateException(e);
+		}
+	}
 }
-- 
GitLab