diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index 6bf89b1082..50aafaef3c 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -61,8 +61,7 @@ import org.jsoup.Jsoup; import org.jsoup.nodes.Element; import java.io.BufferedInputStream; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; +import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -696,7 +695,7 @@ class Core { throw new IllegalArgumentException("raw message file not found"); Log.i(folder.name + " reading " + file); - try (InputStream is = new FileInputStream(file)) { + try (InputStream is = new BufferedInputStream(new FileInputStream(file))) { imessage = new MimeMessage(isession, is); } } @@ -776,7 +775,7 @@ class Core { Message imessage = ifolder.getMessageByUID(message.uid); if (imessage != null) map.put(imessage, message); - } catch (MessageRemovedException ex) { + } catch (MessagingException ex) { Log.w(ex); } @@ -789,7 +788,7 @@ class Core { EntityMessage message = map.get(imessage); File file = File.createTempFile("draft", "." + message.id, context.getCacheDir()); - try (OutputStream os = new FileOutputStream(file)) { + try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { imessage.writeTo(os); } @@ -1026,10 +1025,11 @@ class Core { throw new MessageRemovedException(); File file = message.getRawFile(context); - try (OutputStream os = new FileOutputStream(file)) { + try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { imessage.writeTo(os); - db.message().setMessageRaw(message.id, true); } + + db.message().setMessageRaw(message.id, true); } if (jargs.length() > 0) { @@ -1911,17 +1911,20 @@ class Core { Log.w(folder.name + " " + ex.getMessage()); Log.i(folder.name + " fetching raw message uid=" + uid); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - imessage.writeTo(bos); - bos.close(); + File file = File.createTempFile("serverbug", "." + uid, context.getCacheDir()); + try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { + imessage.writeTo(os); + } - Properties properties = MessageHelper.getSessionProperties(); - Session isession = Session.getInstance(properties, null); + Properties props = MessageHelper.getSessionProperties(); + Session isession = Session.getInstance(props, null); Log.i(folder.name + " decoding again uid=" + uid); - ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); - imessage = new MimeMessage(isession, bis); - bis.close(); + try (InputStream is = new BufferedInputStream(new FileInputStream(file))) { + imessage = new MimeMessage(isession, is); + } + + file.delete(); Log.i(folder.name + " synchronizing again uid=" + uid); return _synchronizeMessage(context, account, folder, uid, imessage, browsed, download, rules, state); diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 2d07adf039..4513b24ea7 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -114,6 +114,7 @@ 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; @@ -1528,8 +1529,8 @@ public class FragmentCompose extends FragmentBase { Log.i("Writing " + file + " size=" + bytes.length); try (OutputStream out = new FileOutputStream(file)) { out.write(bytes); - db.attachment().setDownloaded(attachment.id, (long) bytes.length); } + db.attachment().setDownloaded(attachment.id, (long) bytes.length); db.setTransactionSuccessful(); } finally { @@ -1973,7 +1974,7 @@ public class FragmentCompose extends FragmentBase { } File tmp = File.createTempFile("image", ".resized", context.getCacheDir()); - try (OutputStream out = new FileOutputStream(tmp)) { + try (OutputStream out = new BufferedOutputStream(new FileOutputStream(tmp))) { resized.compress("image/jpeg".equals(attachment.type) ? Bitmap.CompressFormat.JPEG : Bitmap.CompressFormat.PNG, diff --git a/app/src/main/java/eu/faircode/email/HtmlHelper.java b/app/src/main/java/eu/faircode/email/HtmlHelper.java index 8ed04dcb05..f86bb08957 100644 --- a/app/src/main/java/eu/faircode/email/HtmlHelper.java +++ b/app/src/main/java/eu/faircode/email/HtmlHelper.java @@ -61,6 +61,7 @@ import org.jsoup.safety.Whitelist; import org.jsoup.select.NodeTraversor; import org.jsoup.select.NodeVisitor; +import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -702,7 +703,7 @@ public class HtmlHelper { Log.i("Downloaded image source=" + a.source); - try (OutputStream os = new FileOutputStream(file)) { + try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { bm.compress(Bitmap.CompressFormat.PNG, 90, os); } diff --git a/app/src/main/java/eu/faircode/email/Log.java b/app/src/main/java/eu/faircode/email/Log.java index 11a0011aad..dd407488cb 100644 --- a/app/src/main/java/eu/faircode/email/Log.java +++ b/app/src/main/java/eu/faircode/email/Log.java @@ -601,18 +601,17 @@ public class Log { attachment.progress = 0; attachment.id = db.attachment().insertAttachment(attachment); + long size = 0; File file = attachment.getFile(context); try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { - - long size = 0; SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); Map settings = prefs.getAll(); for (String key : settings.keySet()) size += write(os, key + "=" + settings.get(key) + "\r\n"); - - db.attachment().setDownloaded(attachment.id, size); } + + db.attachment().setDownloaded(attachment.id, size); } private static void attachAccounts(Context context, long id, int sequence) throws IOException { @@ -628,11 +627,9 @@ public class Log { attachment.progress = 0; attachment.id = db.attachment().insertAttachment(attachment); + long size = 0; File file = attachment.getFile(context); try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { - - long size = 0; - List accounts = db.account().getAccounts(); for (EntityAccount account : accounts) try { @@ -656,9 +653,9 @@ public class Log { } catch (JSONException ex) { size += write(os, ex.toString() + "\r\n"); } - - db.attachment().setDownloaded(attachment.id, size); } + + db.attachment().setDownloaded(attachment.id, size); } private static void attachNetworkInfo(Context context, long id, int sequence) throws IOException { @@ -674,10 +671,9 @@ public class Log { attachment.progress = 0; attachment.id = db.attachment().insertAttachment(attachment); + long size = 0; File file = attachment.getFile(context); try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { - - long size = 0; ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); Network active = null; @@ -688,9 +684,9 @@ public class Log { NetworkCapabilities caps = cm.getNetworkCapabilities(network); size += write(os, (network.equals(active) ? "active=" : "network=") + network + " capabilities=" + caps + "\r\n\r\n"); } - - db.attachment().setDownloaded(attachment.id, size); } + + db.attachment().setDownloaded(attachment.id, size); } private static void attachLog(Context context, long id, int sequence) throws IOException { @@ -706,18 +702,17 @@ public class Log { attachment.progress = 0; attachment.id = db.attachment().insertAttachment(attachment); + long size = 0; File file = attachment.getFile(context); try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { - - long size = 0; long from = new Date().getTime() - 24 * 3600 * 1000L; DateFormat TF = Helper.getTimeInstance(context); for (EntityLog entry : db.log().getLogs(from)) size += write(os, String.format("%s %s\r\n", TF.format(entry.time), entry.data)); - - db.attachment().setDownloaded(attachment.id, size); } + + db.attachment().setDownloaded(attachment.id, size); } private static void attachOperations(Context context, long id, int sequence) throws IOException { @@ -733,10 +728,9 @@ public class Log { attachment.progress = 0; attachment.id = db.attachment().insertAttachment(attachment); + long size = 0; File file = attachment.getFile(context); try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { - - long size = 0; DateFormat TF = Helper.getTimeInstance(context); for (EntityOperation op : db.operation().getOperations()) @@ -746,9 +740,9 @@ public class Log { op.name, op.args, op.error)); - - db.attachment().setDownloaded(attachment.id, size); } + + db.attachment().setDownloaded(attachment.id, size); } private static void attachLogcat(Context context, long id, int sequence) throws IOException { @@ -767,7 +761,6 @@ public class Log { Process proc = null; File file = attachment.getFile(context); try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { - String[] cmd = new String[]{"logcat", "-d", "-v", "threadtime", @@ -782,7 +775,6 @@ public class Log { size += write(os, line + "\r\n"); } - db.attachment().setDownloaded(attachment.id, size); } finally { if (proc != null)