From 84fbfaf76e5b13b6512b4dc8a8be7c62099e57ef Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 16 Jan 2019 13:42:17 +0000 Subject: [PATCH] Buffer all file streams --- .../java/eu/faircode/email/ActivityView.java | 46 ++++++++++--------- .../eu/faircode/email/FragmentCompose.java | 4 +- .../eu/faircode/email/FragmentWebView.java | 12 +++-- .../main/java/eu/faircode/email/Helper.java | 5 +- .../java/eu/faircode/email/HtmlHelper.java | 9 ++-- 5 files changed, 43 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index 58042c43db..1328821600 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -59,6 +59,8 @@ import org.openintents.openpgp.OpenPgpError; import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpServiceConnection; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -69,6 +71,7 @@ import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.OutputStream; import java.net.URL; import java.text.Collator; import java.util.ArrayList; @@ -1156,7 +1159,8 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB if (!attachment.available) throw new IllegalArgumentException(getString(R.string.title_attachments_missing)); - encrypted = new FileInputStream(EntityAttachment.getFile(context, attachment.id)); + File file = EntityAttachment.getFile(context, attachment.id); + encrypted = new BufferedInputStream(new FileInputStream(file)); break; } @@ -1319,17 +1323,17 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB File file = EntityAttachment.getFile(context, id); ParcelFileDescriptor pfd = null; - FileOutputStream fos = null; - FileInputStream fis = null; + OutputStream os = null; + InputStream is = null; try { pfd = context.getContentResolver().openFileDescriptor(uri, "w"); - fos = new FileOutputStream(pfd.getFileDescriptor()); - fis = new FileInputStream(file); + os = new BufferedOutputStream(new FileOutputStream(pfd.getFileDescriptor())); + is = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[ATTACHMENT_BUFFER_SIZE]; int read; - while ((read = fis.read(buffer)) != -1) - fos.write(buffer, 0, read); + while ((read = is.read(buffer)) != -1) + os.write(buffer, 0, read); } finally { try { if (pfd != null) @@ -1338,14 +1342,14 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB Log.w(ex); } try { - if (fos != null) - fos.close(); + if (os != null) + os.close(); } catch (Throwable ex) { Log.w(ex); } try { - if (fis != null) - fis.close(); + if (is != null) + is.close(); } catch (Throwable ex) { Log.w(ex); } @@ -1391,17 +1395,17 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB DocumentFile document = tree.createFile(attachment.type, name); ParcelFileDescriptor pfd = null; - FileOutputStream fos = null; - FileInputStream fis = null; + OutputStream os = null; + InputStream is = null; try { pfd = context.getContentResolver().openFileDescriptor(document.getUri(), "w"); - fos = new FileOutputStream(pfd.getFileDescriptor()); - fis = new FileInputStream(file); + os = new BufferedOutputStream(new FileOutputStream(pfd.getFileDescriptor())); + is = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[ATTACHMENT_BUFFER_SIZE]; int read; - while ((read = fis.read(buffer)) != -1) - fos.write(buffer, 0, read); + while ((read = is.read(buffer)) != -1) + os.write(buffer, 0, read); } finally { try { if (pfd != null) @@ -1410,14 +1414,14 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB Log.w(ex); } try { - if (fos != null) - fos.close(); + if (os != null) + os.close(); } catch (Throwable ex) { Log.w(ex); } try { - if (fis != null) - fis.close(); + if (is != null) + is.close(); } catch (Throwable ex) { Log.w(ex); } diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 09033b6303..abebbec11e 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -1328,9 +1328,9 @@ public class FragmentCompose extends FragmentBase { Log.i("Image target size=" + scaled.getWidth() + "x" + scaled.getHeight()); - FileOutputStream out = null; + OutputStream out = null; try { - out = new FileOutputStream(file); + out = new BufferedOutputStream(new FileOutputStream(file)); scaled.compress("image/jpeg".equals(attachment.type) ? Bitmap.CompressFormat.JPEG : Bitmap.CompressFormat.PNG, diff --git a/app/src/main/java/eu/faircode/email/FragmentWebView.java b/app/src/main/java/eu/faircode/email/FragmentWebView.java index df0d12b74e..422623a63a 100644 --- a/app/src/main/java/eu/faircode/email/FragmentWebView.java +++ b/app/src/main/java/eu/faircode/email/FragmentWebView.java @@ -45,9 +45,11 @@ import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; +import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -143,13 +145,13 @@ public class FragmentWebView extends FragmentBase { String cid = "<" + cids[1] + ">"; EntityAttachment attachment = DB.getInstance(context).attachment().getAttachment(id, cid); if (attachment != null && attachment.available) { - FileInputStream fis = null; + InputStream is = null; try { File file = EntityAttachment.getFile(context, attachment.id); - fis = new FileInputStream(file); + is = new BufferedInputStream(new FileInputStream(file)); byte[] bytes = new byte[(int) file.length()]; - if (fis.read(bytes) != bytes.length) + if (is.read(bytes) != bytes.length) throw new IOException("length"); StringBuilder sb = new StringBuilder(); @@ -160,8 +162,8 @@ public class FragmentWebView extends FragmentBase { img.attr("src", sb.toString()); } finally { - if (fis != null) - fis.close(); + if (is != null) + is.close(); } } } diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java index 008faf78a9..1b33743499 100644 --- a/app/src/main/java/eu/faircode/email/Helper.java +++ b/app/src/main/java/eu/faircode/email/Helper.java @@ -54,6 +54,7 @@ import com.android.billingclient.api.BillingClient; import com.google.android.material.bottomnavigation.BottomNavigationView; import com.sun.mail.imap.IMAPStore; +import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; @@ -588,9 +589,9 @@ public class Helper { } static void copy(File src, File dst) throws IOException { - InputStream in = new FileInputStream(src); + InputStream in = new BufferedInputStream(new FileInputStream(src)); try { - OutputStream out = new FileOutputStream(dst); + OutputStream out = new BufferedOutputStream(new FileOutputStream(dst)); try { byte[] buf = new byte[4096]; int len; diff --git a/app/src/main/java/eu/faircode/email/HtmlHelper.java b/app/src/main/java/eu/faircode/email/HtmlHelper.java index a183d5c707..50c9ca4030 100644 --- a/app/src/main/java/eu/faircode/email/HtmlHelper.java +++ b/app/src/main/java/eu/faircode/email/HtmlHelper.java @@ -37,11 +37,14 @@ import org.jsoup.safety.Whitelist; import org.jsoup.select.NodeTraversor; import org.jsoup.select.NodeVisitor; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.net.URL; import java.util.Arrays; import java.util.Date; @@ -182,7 +185,7 @@ public class HtmlHelper { dir.mkdir(); InputStream is = null; - FileOutputStream os = null; + OutputStream os = null; try { // Create unique file name File file = new File(dir, id + "_" + source.hashCode()); @@ -190,7 +193,7 @@ public class HtmlHelper { // Get input stream if (file.exists()) { Log.i("Using cached " + file); - is = new FileInputStream(file); + is = new BufferedInputStream(new FileInputStream(file)); } else { Log.i("Downloading " + source); is = new URL(source).openStream(); @@ -203,7 +206,7 @@ public class HtmlHelper { // Cache bitmap if (!file.exists()) { - os = new FileOutputStream(file); + os = new BufferedOutputStream(new FileOutputStream(file)); bm.compress(Bitmap.CompressFormat.PNG, 100, os); }