mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
feat(file): copy sync and async support (#10273)
This commit is contained in:
@@ -5,6 +5,7 @@ import android.content.res.Resources;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.net.Uri;
|
||||
import android.os.Looper;
|
||||
import android.util.Base64;
|
||||
import android.util.Log;
|
||||
@@ -13,6 +14,7 @@ import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.Closeable;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
@@ -29,6 +31,8 @@ import java.net.URL;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
@@ -595,6 +599,94 @@ public class Async {
|
||||
|
||||
public static class File {
|
||||
|
||||
public static boolean copySync(final String src, final String dest, final Context context) throws Exception {
|
||||
InputStream is;
|
||||
OutputStream os;
|
||||
|
||||
if(src.startsWith("content://")){
|
||||
is = context.getContentResolver().openInputStream(Uri.parse(src));
|
||||
}else is = new FileInputStream(new java.io.File(src));
|
||||
|
||||
if(dest.startsWith("content://")){
|
||||
os = context.getContentResolver().openOutputStream(Uri.parse(dest));
|
||||
}else os = new FileOutputStream(new java.io.File(dest));
|
||||
|
||||
return copySync(is, os, context);
|
||||
}
|
||||
|
||||
public static boolean copySync(final InputStream src, final OutputStream dest, final Object context) throws Exception {
|
||||
ReadableByteChannel isc = java.nio.channels.Channels.newChannel(src);
|
||||
WritableByteChannel osc = java.nio.channels.Channels.newChannel(dest);
|
||||
|
||||
int size = src.available();
|
||||
|
||||
int written = fastChannelCopy(isc, osc);
|
||||
|
||||
return size == written;
|
||||
}
|
||||
|
||||
public static void copy(final String src, final String dest, final CompleteCallback callback, final Context context) {
|
||||
try {
|
||||
InputStream is;
|
||||
OutputStream os;
|
||||
|
||||
if(src.startsWith("content://")){
|
||||
is = context.getContentResolver().openInputStream(Uri.parse(src));
|
||||
}else is = new FileInputStream(new java.io.File(src));
|
||||
|
||||
if(dest.startsWith("content://")){
|
||||
os = context.getContentResolver().openOutputStream(Uri.parse(dest));
|
||||
}else os = new FileOutputStream(new java.io.File(dest));
|
||||
|
||||
copy(is, os, callback, context);
|
||||
}catch (Exception exception){
|
||||
callback.onError(exception.getMessage(), context);
|
||||
}
|
||||
}
|
||||
|
||||
private static int fastChannelCopy(final ReadableByteChannel src,
|
||||
final WritableByteChannel dest) throws IOException {
|
||||
int written = 0;
|
||||
final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
|
||||
while (src.read(buffer) != -1) {
|
||||
// prepare the buffer to be drained
|
||||
buffer.flip();
|
||||
// write to the channel, may block
|
||||
written += dest.write(buffer);
|
||||
// If partial transfer, shift remainder down
|
||||
// If buffer is empty, same as doing clear()
|
||||
buffer.compact();
|
||||
}
|
||||
// EOF will leave buffer in fill state
|
||||
buffer.flip();
|
||||
// make sure the buffer is fully drained.
|
||||
while (buffer.hasRemaining()) {
|
||||
written += dest.write(buffer);
|
||||
}
|
||||
return written;
|
||||
}
|
||||
|
||||
|
||||
public static void copy(final InputStream src, final OutputStream dest, final CompleteCallback callback, final Object context) {
|
||||
final android.os.Handler mHandler = new android.os.Handler(Looper.myLooper());
|
||||
threadPoolExecutor().execute((Runnable) () -> {
|
||||
|
||||
try (InputStream is = src; OutputStream os = dest){
|
||||
ReadableByteChannel isc = java.nio.channels.Channels.newChannel(is);
|
||||
WritableByteChannel osc = java.nio.channels.Channels.newChannel(os);
|
||||
|
||||
int size = src.available();
|
||||
|
||||
int written = fastChannelCopy(isc, osc);
|
||||
|
||||
mHandler.post(() -> callback.onComplete(size == written, context));
|
||||
|
||||
} catch (Exception e) {
|
||||
mHandler.post(() -> callback.onError(e.getMessage(), context));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void readText(final String path, final String encoding, final CompleteCallback callback, final Object context) {
|
||||
final android.os.Handler mHandler = new android.os.Handler(Looper.myLooper());
|
||||
threadPoolExecutor().execute(new Runnable() {
|
||||
|
||||
Reference in New Issue
Block a user