From 9341b166ba85c1a1d7680a88fead6ddeda5d36d6 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 6 Jul 2019 13:02:32 +0200 Subject: [PATCH] Read message file as stream --- app/src/main/java/eu/faircode/email/Helper.java | 15 ++++++++++----- .../java/eu/faircode/email/MessageHelper.java | 11 +---------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java index bb2981b5ba..1ab7a64f26 100644 --- a/app/src/main/java/eu/faircode/email/Helper.java +++ b/app/src/main/java/eu/faircode/email/Helper.java @@ -67,7 +67,7 @@ import com.sun.mail.iap.ConnectionException; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedWriter; -import java.io.DataInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -510,12 +510,17 @@ public class Helper { } } + static String readStream(InputStream is, String charset) throws IOException { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + byte[] buffer = new byte[16384]; + for (int len = is.read(buffer); len != -1; len = is.read(buffer)) + os.write(buffer, 0, len); + return new String(os.toByteArray(), charset); + } + static String readText(File file) throws IOException { try (FileInputStream in = new FileInputStream(file)) { - byte[] buffer = new byte[(int) file.length()]; - DataInputStream dis = new DataInputStream(in); - dis.readFully(buffer); - return new String(buffer); + return readStream(in, "UTF-8"); } } diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index 26339632da..d4bf5c2ff6 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -29,7 +29,6 @@ import com.sun.mail.util.MessageRemovedIOException; import java.io.BufferedOutputStream; import java.io.BufferedReader; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; @@ -801,7 +800,7 @@ public class MessageHelper { result = (String) content; else if (content instanceof InputStream) // Typically com.sun.mail.util.QPDecoderStream - result = readStream((InputStream) content, "UTF-8"); + result = Helper.readStream((InputStream) content, "UTF-8"); else result = content.toString(); } catch (IOException | FolderClosedException | MessageRemovedException ex) { @@ -1112,14 +1111,6 @@ public class MessageHelper { } } - private static String readStream(InputStream is, String charset) throws IOException { - ByteArrayOutputStream os = new ByteArrayOutputStream(); - byte[] buffer = new byte[4096]; - for (int len = is.read(buffer); len != -1; len = is.read(buffer)) - os.write(buffer, 0, len); - return new String(os.toByteArray(), charset); - } - static boolean equal(Address[] a1, Address[] a2) { if (a1 == null && a2 == null) return true;