From a3bb6d56f5bd1793921c8e57660295f4e139de61 Mon Sep 17 00:00:00 2001 From: Chris Lloyd Date: Sun, 2 Jun 2019 17:14:14 +0100 Subject: [PATCH 1/2] Different implementation of readFileAsString that does not insert stray newlines --- .../com/getcapacitor/plugin/Filesystem.java | 30 +++++++------------ 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java index b2e9faa7d..045fee9cb 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java @@ -17,7 +17,6 @@ import com.getcapacitor.PluginRequestCodes; import org.json.JSONException; -import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; import java.io.File; @@ -26,7 +25,6 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; @@ -114,29 +112,23 @@ private InputStream getInputStream(String path, String directory) throws IOExcep File androidDirectory = this.getDirectory(directory); if (androidDirectory == null) { - return null; + throw new IOException("Directory not found"); } return new FileInputStream(new File(androidDirectory, path)); } - private String readFileAsString(InputStream is, Charset charset) throws IOException { - final StringBuilder text = new StringBuilder(); + private String readFileAsString(InputStream is, String encoding) throws IOException { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + + byte[] buffer = new byte[1024]; + int length = 0; - BufferedReader br = new BufferedReader( - new InputStreamReader( - is, - charset - ) - ); - String line; + while ((length = is.read(buffer)) != -1) { + outputStream.write(buffer, 0, length); + }; - while ((line = br.readLine()) != null) { - text.append(line); - text.append('\n'); - } - br.close(); - return text.toString(); + return outputStream.toString(encoding); } private String readFileAsBase64EncodedData(InputStream is) throws IOException { @@ -174,7 +166,7 @@ public void readFile(PluginCall call) { InputStream is = getInputStream(file, directory); String dataStr; if (charset != null) { - dataStr = readFileAsString(is, charset); + dataStr = readFileAsString(is, encoding); } else { dataStr = readFileAsBase64EncodedData(is); } From 2df6895a3bec3a2e01296152b6994752578ca1d8 Mon Sep 17 00:00:00 2001 From: Chris Lloyd Date: Tue, 11 Jun 2019 20:10:23 +0100 Subject: [PATCH 2/2] Feedback from review --- .../src/main/java/com/getcapacitor/plugin/Filesystem.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java index 045fee9cb..662d46d0d 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java @@ -166,7 +166,7 @@ public void readFile(PluginCall call) { InputStream is = getInputStream(file, directory); String dataStr; if (charset != null) { - dataStr = readFileAsString(is, encoding); + dataStr = readFileAsString(is, charset.name()); } else { dataStr = readFileAsBase64EncodedData(is); }