From 5cf581e6c9206825e104fcd4a81c69a78e0f4db4 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Wed, 17 May 2017 19:25:13 +0100 Subject: [PATCH] Fix files not writing data when closed As the raw stream was being provided to the parent class, buffered data was not written, resulting in empty files. This ensures the buffered reader/writer is the one which is closed. --- .../core/apis/handles/EncodedInputHandle.java | 8 ++++++-- .../core/apis/handles/EncodedOutputHandle.java | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java b/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java index 09c4cd498..1cc5141a1 100644 --- a/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java +++ b/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java @@ -23,7 +23,11 @@ public class EncodedInputHandle extends HandleGeneric public EncodedInputHandle( InputStream stream, String encoding ) { - super( stream ); + this( makeReader( stream, encoding ) ); + } + + private static BufferedReader makeReader( InputStream stream, String encoding ) + { if( encoding == null ) encoding = "UTF-8"; InputStreamReader streamReader; try @@ -34,7 +38,7 @@ public class EncodedInputHandle extends HandleGeneric { streamReader = new InputStreamReader( stream ); } - this.m_reader = new BufferedReader( streamReader ); + return new BufferedReader( streamReader ); } @Nonnull diff --git a/src/main/java/dan200/computercraft/core/apis/handles/EncodedOutputHandle.java b/src/main/java/dan200/computercraft/core/apis/handles/EncodedOutputHandle.java index 61db07fa4..89eb6dd2f 100644 --- a/src/main/java/dan200/computercraft/core/apis/handles/EncodedOutputHandle.java +++ b/src/main/java/dan200/computercraft/core/apis/handles/EncodedOutputHandle.java @@ -23,7 +23,11 @@ public class EncodedOutputHandle extends HandleGeneric public EncodedOutputHandle( OutputStream stream, String encoding ) { - super( stream ); + this( makeWriter( stream, encoding ) ); + } + + private static BufferedWriter makeWriter( OutputStream stream, String encoding ) + { if( encoding == null ) encoding = "UTF-8"; OutputStreamWriter streamWriter; try @@ -34,7 +38,7 @@ public class EncodedOutputHandle extends HandleGeneric { streamWriter = new OutputStreamWriter( stream ); } - this.m_writer = new BufferedWriter( streamWriter ); + return new BufferedWriter( streamWriter ); } @Nonnull