Optimized copy streams

pull/206/head
M66B 3 years ago
parent 5871699033
commit 44472f25b2

@ -47,6 +47,7 @@ import android.os.BatteryManager;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.FileUtils;
import android.os.LocaleList; import android.os.LocaleList;
import android.os.Parcel; import android.os.Parcel;
import android.os.PowerManager; import android.os.PowerManager;
@ -1632,43 +1633,30 @@ public class Helper {
} }
static void copy(File src, File dst) throws IOException { static void copy(File src, File dst) throws IOException {
try (InputStream in = new FileInputStream(src)) { try (InputStream is = new FileInputStream(src)) {
try (FileOutputStream out = new FileOutputStream(dst)) { try (OutputStream os = new FileOutputStream(dst)) {
copy(in, out); copy(is, os);
} }
} }
} }
static void copy(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[BUFFER_SIZE];
int len;
while ((len = in.read(buf)) > 0)
out.write(buf, 0, len);
}
static long copy(Context context, Uri uri, File file) throws IOException { static long copy(Context context, Uri uri, File file) throws IOException {
long size = 0; try (InputStream is = context.getContentResolver().openInputStream(uri)) {
InputStream is = null; try (OutputStream os = new FileOutputStream(file)) {
OutputStream os = null; copy(is, os);
try {
is = context.getContentResolver().openInputStream(uri);
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);
}
} finally {
try {
if (is != null)
is.close();
} finally {
if (os != null)
os.close();
} }
} }
return size; return file.length();
}
static void copy(InputStream is, OutputStream os) throws IOException {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
byte[] buf = new byte[BUFFER_SIZE];
int len;
while ((len = is.read(buf)) > 0)
os.write(buf, 0, len);
} else
FileUtils.copy(is, os);
} }
static long getAvailableStorageSpace() { static long getAvailableStorageSpace() {

Loading…
Cancel
Save