Buffer all file streams

pull/147/head
M66B 7 years ago
parent 9275b6e14f
commit 84fbfaf76e

@ -59,6 +59,8 @@ import org.openintents.openpgp.OpenPgpError;
import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpApi;
import org.openintents.openpgp.util.OpenPgpServiceConnection; import org.openintents.openpgp.util.OpenPgpServiceConnection;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -69,6 +71,7 @@ import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL; import java.net.URL;
import java.text.Collator; import java.text.Collator;
import java.util.ArrayList; import java.util.ArrayList;
@ -1156,7 +1159,8 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
if (!attachment.available) if (!attachment.available)
throw new IllegalArgumentException(getString(R.string.title_attachments_missing)); 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; break;
} }
@ -1319,17 +1323,17 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
File file = EntityAttachment.getFile(context, id); File file = EntityAttachment.getFile(context, id);
ParcelFileDescriptor pfd = null; ParcelFileDescriptor pfd = null;
FileOutputStream fos = null; OutputStream os = null;
FileInputStream fis = null; InputStream is = null;
try { try {
pfd = context.getContentResolver().openFileDescriptor(uri, "w"); pfd = context.getContentResolver().openFileDescriptor(uri, "w");
fos = new FileOutputStream(pfd.getFileDescriptor()); os = new BufferedOutputStream(new FileOutputStream(pfd.getFileDescriptor()));
fis = new FileInputStream(file); is = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[ATTACHMENT_BUFFER_SIZE]; byte[] buffer = new byte[ATTACHMENT_BUFFER_SIZE];
int read; int read;
while ((read = fis.read(buffer)) != -1) while ((read = is.read(buffer)) != -1)
fos.write(buffer, 0, read); os.write(buffer, 0, read);
} finally { } finally {
try { try {
if (pfd != null) if (pfd != null)
@ -1338,14 +1342,14 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
Log.w(ex); Log.w(ex);
} }
try { try {
if (fos != null) if (os != null)
fos.close(); os.close();
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
} }
try { try {
if (fis != null) if (is != null)
fis.close(); is.close();
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
} }
@ -1391,17 +1395,17 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
DocumentFile document = tree.createFile(attachment.type, name); DocumentFile document = tree.createFile(attachment.type, name);
ParcelFileDescriptor pfd = null; ParcelFileDescriptor pfd = null;
FileOutputStream fos = null; OutputStream os = null;
FileInputStream fis = null; InputStream is = null;
try { try {
pfd = context.getContentResolver().openFileDescriptor(document.getUri(), "w"); pfd = context.getContentResolver().openFileDescriptor(document.getUri(), "w");
fos = new FileOutputStream(pfd.getFileDescriptor()); os = new BufferedOutputStream(new FileOutputStream(pfd.getFileDescriptor()));
fis = new FileInputStream(file); is = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[ATTACHMENT_BUFFER_SIZE]; byte[] buffer = new byte[ATTACHMENT_BUFFER_SIZE];
int read; int read;
while ((read = fis.read(buffer)) != -1) while ((read = is.read(buffer)) != -1)
fos.write(buffer, 0, read); os.write(buffer, 0, read);
} finally { } finally {
try { try {
if (pfd != null) if (pfd != null)
@ -1410,14 +1414,14 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
Log.w(ex); Log.w(ex);
} }
try { try {
if (fos != null) if (os != null)
fos.close(); os.close();
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
} }
try { try {
if (fis != null) if (is != null)
fis.close(); is.close();
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
} }

@ -1328,9 +1328,9 @@ public class FragmentCompose extends FragmentBase {
Log.i("Image target size=" + scaled.getWidth() + "x" + scaled.getHeight()); Log.i("Image target size=" + scaled.getWidth() + "x" + scaled.getHeight());
FileOutputStream out = null; OutputStream out = null;
try { try {
out = new FileOutputStream(file); out = new BufferedOutputStream(new FileOutputStream(file));
scaled.compress("image/jpeg".equals(attachment.type) scaled.compress("image/jpeg".equals(attachment.type)
? Bitmap.CompressFormat.JPEG ? Bitmap.CompressFormat.JPEG
: Bitmap.CompressFormat.PNG, : Bitmap.CompressFormat.PNG,

@ -45,9 +45,11 @@ import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -143,13 +145,13 @@ public class FragmentWebView extends FragmentBase {
String cid = "<" + cids[1] + ">"; String cid = "<" + cids[1] + ">";
EntityAttachment attachment = DB.getInstance(context).attachment().getAttachment(id, cid); EntityAttachment attachment = DB.getInstance(context).attachment().getAttachment(id, cid);
if (attachment != null && attachment.available) { if (attachment != null && attachment.available) {
FileInputStream fis = null; InputStream is = null;
try { try {
File file = EntityAttachment.getFile(context, attachment.id); File file = EntityAttachment.getFile(context, attachment.id);
fis = new FileInputStream(file); is = new BufferedInputStream(new FileInputStream(file));
byte[] bytes = new byte[(int) file.length()]; byte[] bytes = new byte[(int) file.length()];
if (fis.read(bytes) != bytes.length) if (is.read(bytes) != bytes.length)
throw new IOException("length"); throw new IOException("length");
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@ -160,8 +162,8 @@ public class FragmentWebView extends FragmentBase {
img.attr("src", sb.toString()); img.attr("src", sb.toString());
} finally { } finally {
if (fis != null) if (is != null)
fis.close(); is.close();
} }
} }
} }

@ -54,6 +54,7 @@ import com.android.billingclient.api.BillingClient;
import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.sun.mail.imap.IMAPStore; import com.sun.mail.imap.IMAPStore;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
@ -588,9 +589,9 @@ public class Helper {
} }
static void copy(File src, File dst) throws IOException { static void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src); InputStream in = new BufferedInputStream(new FileInputStream(src));
try { try {
OutputStream out = new FileOutputStream(dst); OutputStream out = new BufferedOutputStream(new FileOutputStream(dst));
try { try {
byte[] buf = new byte[4096]; byte[] buf = new byte[4096];
int len; int len;

@ -37,11 +37,14 @@ import org.jsoup.safety.Whitelist;
import org.jsoup.select.NodeTraversor; import org.jsoup.select.NodeTraversor;
import org.jsoup.select.NodeVisitor; import org.jsoup.select.NodeVisitor;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL; import java.net.URL;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
@ -182,7 +185,7 @@ public class HtmlHelper {
dir.mkdir(); dir.mkdir();
InputStream is = null; InputStream is = null;
FileOutputStream os = null; OutputStream os = null;
try { try {
// Create unique file name // Create unique file name
File file = new File(dir, id + "_" + source.hashCode()); File file = new File(dir, id + "_" + source.hashCode());
@ -190,7 +193,7 @@ public class HtmlHelper {
// Get input stream // Get input stream
if (file.exists()) { if (file.exists()) {
Log.i("Using cached " + file); Log.i("Using cached " + file);
is = new FileInputStream(file); is = new BufferedInputStream(new FileInputStream(file));
} else { } else {
Log.i("Downloading " + source); Log.i("Downloading " + source);
is = new URL(source).openStream(); is = new URL(source).openStream();
@ -203,7 +206,7 @@ public class HtmlHelper {
// Cache bitmap // Cache bitmap
if (!file.exists()) { if (!file.exists()) {
os = new FileOutputStream(file); os = new BufferedOutputStream(new FileOutputStream(file));
bm.compress(Bitmap.CompressFormat.PNG, 100, os); bm.compress(Bitmap.CompressFormat.PNG, 100, os);
} }

Loading…
Cancel
Save