Buffer all file streams

This commit is contained in:
M66B 2019-01-16 13:42:17 +00:00
parent 9275b6e14f
commit 84fbfaf76e
5 changed files with 43 additions and 33 deletions

View File

@ -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);
}

View File

@ -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,

View File

@ -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();
}
}
}

View File

@ -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;

View File

@ -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);
}