From de963c56b727d4a85c0f88824cf3f81ac865ed52 Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 26 Jul 2019 08:39:05 +0200 Subject: [PATCH] Removed not nessary input/output buffering --- .../java/eu/faircode/email/ActivityDSN.java | 8 +++---- .../java/eu/faircode/email/ActivityEml.java | 3 +-- .../java/eu/faircode/email/ActivitySetup.java | 3 +-- .../java/eu/faircode/email/ApplicationEx.java | 1 - app/src/main/java/eu/faircode/email/Core.java | 7 +++--- .../eu/faircode/email/FragmentCompose.java | 9 ++++---- .../eu/faircode/email/FragmentMessages.java | 15 ++++++------- .../main/java/eu/faircode/email/Helper.java | 22 +++++++++---------- .../java/eu/faircode/email/HtmlHelper.java | 6 ++--- .../java/eu/faircode/email/MessageHelper.java | 8 +++---- 10 files changed, 35 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivityDSN.java b/app/src/main/java/eu/faircode/email/ActivityDSN.java index f3f8106eac..f9eebf5857 100644 --- a/app/src/main/java/eu/faircode/email/ActivityDSN.java +++ b/app/src/main/java/eu/faircode/email/ActivityDSN.java @@ -32,9 +32,9 @@ import androidx.constraintlayout.widget.Group; import com.google.android.material.snackbar.Snackbar; -import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; +import java.nio.charset.StandardCharsets; public class ActivityDSN extends ActivityBase { @Override @@ -82,13 +82,13 @@ public class ActivityDSN extends ActivityBase { ContentResolver resolver = context.getContentResolver(); AssetFileDescriptor descriptor = resolver.openTypedAssetFileDescriptor(uri, "*/*", null); - try (InputStream is = new BufferedInputStream(descriptor.createInputStream())) { + try (InputStream is = descriptor.createInputStream()) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024]; + byte[] buffer = new byte[Helper.BUFFER_SIZE]; int length; while ((length = is.read(buffer)) != -1) bos.write(buffer, 0, length); - result.headers = MessageHelper.decodeMime(bos.toString("UTF-8")); + result.headers = MessageHelper.decodeMime(bos.toString(StandardCharsets.UTF_8.name())); } return result; diff --git a/app/src/main/java/eu/faircode/email/ActivityEml.java b/app/src/main/java/eu/faircode/email/ActivityEml.java index f71c3e1fd3..5b7383ec8c 100644 --- a/app/src/main/java/eu/faircode/email/ActivityEml.java +++ b/app/src/main/java/eu/faircode/email/ActivityEml.java @@ -33,7 +33,6 @@ import androidx.constraintlayout.widget.Group; import com.google.android.material.snackbar.Snackbar; -import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.Properties; @@ -96,7 +95,7 @@ public class ActivityEml extends ActivityBase { ContentResolver resolver = context.getContentResolver(); AssetFileDescriptor descriptor = resolver.openTypedAssetFileDescriptor(uri, "*/*", null); - try (InputStream is = new BufferedInputStream(descriptor.createInputStream())) { + try (InputStream is = descriptor.createInputStream()) { Properties props = MessageHelper.getSessionProperties(null, false); Session isession = Session.getInstance(props, null); diff --git a/app/src/main/java/eu/faircode/email/ActivitySetup.java b/app/src/main/java/eu/faircode/email/ActivitySetup.java index ad9412613c..6f80600868 100644 --- a/app/src/main/java/eu/faircode/email/ActivitySetup.java +++ b/app/src/main/java/eu/faircode/email/ActivitySetup.java @@ -73,7 +73,6 @@ import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -602,7 +601,7 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac ContentResolver resolver = context.getContentResolver(); DocumentFile file = DocumentFile.fromSingleUri(context, uri); - try (OutputStream raw = new BufferedOutputStream(resolver.openOutputStream(uri))) { + try (OutputStream raw = resolver.openOutputStream(uri)) { Log.i("Writing URI=" + uri + " name=" + file.getName() + " virtual=" + file.isVirtual()); if (TextUtils.isEmpty(password)) diff --git a/app/src/main/java/eu/faircode/email/ApplicationEx.java b/app/src/main/java/eu/faircode/email/ApplicationEx.java index ffbc0c4a7d..0cf4feaf0a 100644 --- a/app/src/main/java/eu/faircode/email/ApplicationEx.java +++ b/app/src/main/java/eu/faircode/email/ApplicationEx.java @@ -30,7 +30,6 @@ import android.content.SharedPreferences; import android.content.res.Configuration; import android.os.Build; import android.os.DeadSystemException; -import android.os.Handler; import android.os.RemoteException; import android.view.OrientationEventListener; import android.webkit.CookieManager; diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index 243054e22e..8ecf5a6870 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -55,7 +55,6 @@ import org.jsoup.Jsoup; import org.jsoup.nodes.Element; import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -500,7 +499,7 @@ class Core { throw new IllegalArgumentException("raw message file not found"); Log.i(folder.name + " reading " + file); - try (InputStream is = new BufferedInputStream(new FileInputStream(file))) { + try (InputStream is = new FileInputStream(file)) { imessage = new MimeMessage(isession, is); } } @@ -602,7 +601,7 @@ class Core { Log.i(folder.name + " move from " + folder.type + " to " + target.type); File file = message.getRawFile(context); - try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { + try (OutputStream os = new FileOutputStream(file)) { imessage.writeTo(os); } @@ -702,7 +701,7 @@ class Core { throw new MessageRemovedException(); File file = message.getRawFile(context); - try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { + try (OutputStream os = new FileOutputStream(file)) { imessage.writeTo(os); db.message().setMessageRaw(message.id, true); } diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 45c2ad8d69..336030a6dd 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -115,7 +115,6 @@ import org.openintents.openpgp.OpenPgpError; import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpServiceConnection; -import java.io.BufferedOutputStream; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -1426,7 +1425,7 @@ public class FragmentCompose extends FragmentBase { File file = attachment.getFile(context); if (BuildConfig.DEBUG || BuildConfig.BETA_RELEASE) Log.i("Writing " + file + " size=" + bytes.length); - try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) { + try (OutputStream out = new FileOutputStream(file)) { out.write(bytes); db.attachment().setDownloaded(attachment.id, (long) bytes.length); } @@ -1828,9 +1827,9 @@ public class FragmentCompose extends FragmentBase { OutputStream os = null; try { is = context.getContentResolver().openInputStream(uri); - os = new BufferedOutputStream(new FileOutputStream(file)); + os = new FileOutputStream(file); - byte[] buffer = new byte[MessageHelper.ATTACHMENT_BUFFER_SIZE]; + byte[] buffer = new byte[Helper.BUFFER_SIZE]; for (int len = is.read(buffer); len != -1; len = is.read(buffer)) { size += len; os.write(buffer, 0, len); @@ -1918,7 +1917,7 @@ public class FragmentCompose extends FragmentBase { scaled = rotated; } - try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) { + try (OutputStream out = 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/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index 4219f455be..474d65134b 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -117,7 +117,6 @@ import org.openintents.openpgp.OpenPgpSignatureResult; import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpServiceConnection; -import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; @@ -3536,9 +3535,9 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. try { pfd = context.getContentResolver().openFileDescriptor(uri, "w"); os = new FileOutputStream(pfd.getFileDescriptor()); - is = new BufferedInputStream(new FileInputStream(file)); + is = new FileInputStream(file); - byte[] buffer = new byte[MessageHelper.ATTACHMENT_BUFFER_SIZE]; + byte[] buffer = new byte[Helper.BUFFER_SIZE]; int read; while ((read = is.read(buffer)) != -1) os.write(buffer, 0, read); @@ -3609,9 +3608,9 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. try { pfd = context.getContentResolver().openFileDescriptor(uri, "w"); os = new FileOutputStream(pfd.getFileDescriptor()); - is = new BufferedInputStream(new FileInputStream(file)); + is = new FileInputStream(file); - byte[] buffer = new byte[MessageHelper.ATTACHMENT_BUFFER_SIZE]; + byte[] buffer = new byte[Helper.BUFFER_SIZE]; int read; while ((read = is.read(buffer)) != -1) os.write(buffer, 0, read); @@ -3684,9 +3683,9 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. try { pfd = context.getContentResolver().openFileDescriptor(document.getUri(), "w"); os = new FileOutputStream(pfd.getFileDescriptor()); - is = new BufferedInputStream(new FileInputStream(file)); + is = new FileInputStream(file); - byte[] buffer = new byte[MessageHelper.ATTACHMENT_BUFFER_SIZE]; + byte[] buffer = new byte[Helper.BUFFER_SIZE]; int read; while ((read = is.read(buffer)) != -1) os.write(buffer, 0, read); @@ -3752,7 +3751,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. throw new IllegalArgumentException(context.getString(R.string.title_attachments_missing)); File file = attachment.getFile(context); - encrypted = new BufferedInputStream(new FileInputStream(file)); + encrypted = new FileInputStream(file); break; } diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java index c6ff91a08f..891c94172a 100644 --- a/app/src/main/java/eu/faircode/email/Helper.java +++ b/app/src/main/java/eu/faircode/email/Helper.java @@ -73,18 +73,14 @@ import com.google.android.material.bottomnavigation.BottomNavigationView; import com.sun.mail.iap.ConnectionException; import com.sun.mail.util.FolderClosedIOException; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.DateFormat; @@ -114,6 +110,7 @@ public class Helper { static final int NOTIFICATION_UPDATE = 4; static final float LOW_LIGHT = 0.6f; + static final int BUFFER_SIZE = 8192; // Same as in Files class static final String FAQ_URI = "https://github.com/M66B/FairEmail/blob/master/FAQ.md"; static final String XDA_URI = "https://forum.xda-developers.com/android/apps-games/source-email-t3824168"; @@ -536,14 +533,15 @@ public class Helper { } static void writeText(File file, String content) throws IOException { - try (BufferedWriter out = new BufferedWriter(new FileWriter(file))) { - out.write(content == null ? "" : content); + try (FileOutputStream out = new FileOutputStream(file)) { + if (content != null) + out.write(content.getBytes()); } } static String readStream(InputStream is, String charset) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); - byte[] buffer = new byte[16384]; + byte[] buffer = new byte[BUFFER_SIZE]; for (int len = is.read(buffer); len != -1; len = is.read(buffer)) os.write(buffer, 0, len); return new String(os.toByteArray(), charset); @@ -551,14 +549,14 @@ public class Helper { static String readText(File file) throws IOException { try (FileInputStream in = new FileInputStream(file)) { - return readStream(in, "UTF-8"); + return readStream(in, StandardCharsets.UTF_8.name()); } } static void copy(File src, File dst) throws IOException { - try (InputStream in = new BufferedInputStream(new FileInputStream(src))) { - try (OutputStream out = new BufferedOutputStream(new FileOutputStream(dst))) { - byte[] buf = new byte[4096]; + try (InputStream in = new FileInputStream(src)) { + try (FileOutputStream out = new FileOutputStream(dst)) { + byte[] buf = new byte[BUFFER_SIZE]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); diff --git a/app/src/main/java/eu/faircode/email/HtmlHelper.java b/app/src/main/java/eu/faircode/email/HtmlHelper.java index fcba02b0d7..0be9a7cd2d 100644 --- a/app/src/main/java/eu/faircode/email/HtmlHelper.java +++ b/app/src/main/java/eu/faircode/email/HtmlHelper.java @@ -54,8 +54,6 @@ 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.FileNotFoundException; @@ -459,7 +457,7 @@ public class HtmlHelper { Log.i("Downloaded image source=" + source); - try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { + try (OutputStream os = new FileOutputStream(file)) { bm.compress(Bitmap.CompressFormat.PNG, 90, os); } @@ -638,7 +636,7 @@ public class HtmlHelper { EntityAttachment attachment = db.attachment().getAttachment(id, cid); if (attachment != null && attachment.available) { File file = attachment.getFile(context); - try (InputStream is = new BufferedInputStream(new FileInputStream(file))) { + try (InputStream is = new FileInputStream(file)) { byte[] bytes = new byte[(int) file.length()]; if (is.read(bytes) != bytes.length) throw new IOException("length"); diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index 4885872342..d8b65fa2a8 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -27,7 +27,6 @@ import android.webkit.MimeTypeMap; import com.sun.mail.util.FolderClosedIOException; import com.sun.mail.util.MessageRemovedIOException; -import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; @@ -85,7 +84,6 @@ public class MessageHelper { static final int SMALL_MESSAGE_SIZE = 32 * 1024; // bytes - static final int ATTACHMENT_BUFFER_SIZE = 8192; // bytes static final int DEFAULT_ATTACHMENT_DOWNLOAD_SIZE = 256 * 1024; // bytes static void setSystemProperties() { @@ -874,7 +872,7 @@ public class MessageHelper { result = (String) content; else if (content instanceof InputStream) // Typically com.sun.mail.util.QPDecoderStream - result = Helper.readStream((InputStream) content, "UTF-8"); + result = Helper.readStream((InputStream) content, StandardCharsets.UTF_8.name()); else result = content.toString(); } catch (IOException | FolderClosedException | MessageRemovedException ex) { @@ -972,8 +970,8 @@ public class MessageHelper { long total = apart.part.getSize(); int lastprogress = 0; - try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { - byte[] buffer = new byte[ATTACHMENT_BUFFER_SIZE]; + try (OutputStream os = new FileOutputStream(file)) { + byte[] buffer = new byte[Helper.BUFFER_SIZE]; for (int len = is.read(buffer); len != -1; len = is.read(buffer)) { size += len; os.write(buffer, 0, len);