mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
feat(files): read & write using js buffers (#10093)
This commit is contained in:
@@ -26,7 +26,9 @@ import java.net.CookieHandler;
|
||||
import java.net.CookieManager;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
@@ -627,6 +629,23 @@ public class Async {
|
||||
});
|
||||
}
|
||||
|
||||
public static void readBuffer(final String path, final CompleteCallback callback, final Object context) {
|
||||
final android.os.Handler mHandler = new android.os.Handler(Looper.myLooper());
|
||||
threadPoolExecutor().execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final ReadBufferTask task = new ReadBufferTask(callback, context);
|
||||
final ByteBuffer result = task.doInBackground(path);
|
||||
mHandler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
task.onPostExecute(result);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void writeText(final String path, final String content, final String encoding, final CompleteCallback callback, final Object context) {
|
||||
final android.os.Handler mHandler = new android.os.Handler(Looper.myLooper());
|
||||
threadPoolExecutor().execute(new Runnable() {
|
||||
@@ -661,6 +680,23 @@ public class Async {
|
||||
});
|
||||
}
|
||||
|
||||
public static void writeBuffer(final String path, final ByteBuffer content, final CompleteCallback callback, final Object context) {
|
||||
final android.os.Handler mHandler = new android.os.Handler(Looper.myLooper());
|
||||
threadPoolExecutor().execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final WriteBufferTask task = new WriteBufferTask(callback, context);
|
||||
final boolean result = task.doInBackground(path, content);
|
||||
mHandler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
task.onPostExecute(result);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static class ReadTextTask {
|
||||
private final CompleteCallback callback;
|
||||
private final Object context;
|
||||
@@ -768,6 +804,55 @@ public class Async {
|
||||
}
|
||||
}
|
||||
|
||||
static class ReadBufferTask {
|
||||
private final CompleteCallback callback;
|
||||
private final Object context;
|
||||
|
||||
public ReadBufferTask(CompleteCallback callback, Object context) {
|
||||
this.callback = callback;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
protected ByteBuffer doInBackground(String... params) {
|
||||
java.io.File javaFile = new java.io.File(params[0]);
|
||||
FileInputStream stream = null;
|
||||
|
||||
try {
|
||||
stream = new FileInputStream(javaFile);
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.allocateDirect((int) javaFile.length());
|
||||
|
||||
FileChannel channel = stream.getChannel();
|
||||
channel.read(buffer);
|
||||
buffer.rewind();
|
||||
|
||||
return buffer;
|
||||
} catch (FileNotFoundException e) {
|
||||
Log.e(TAG, "Failed to read file, FileNotFoundException: " + e.getMessage());
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Failed to read file, IOException: " + e.getMessage());
|
||||
return null;
|
||||
} finally {
|
||||
if (stream != null) {
|
||||
try {
|
||||
stream.close();
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Failed to close stream, IOException: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void onPostExecute(final ByteBuffer result) {
|
||||
if (result != null) {
|
||||
this.callback.onComplete(result, this.context);
|
||||
} else {
|
||||
this.callback.onError("ReadTask returns no result.", this.context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static class WriteTextTask {
|
||||
private final CompleteCallback callback;
|
||||
private final Object context;
|
||||
@@ -784,7 +869,6 @@ public class Async {
|
||||
stream = new FileOutputStream(javaFile);
|
||||
|
||||
OutputStreamWriter writer = new OutputStreamWriter(stream, params[2]);
|
||||
|
||||
writer.write(params[1]);
|
||||
writer.close();
|
||||
|
||||
@@ -863,5 +947,52 @@ public class Async {
|
||||
}
|
||||
}
|
||||
|
||||
static class WriteBufferTask {
|
||||
private final CompleteCallback callback;
|
||||
private final Object context;
|
||||
|
||||
public WriteBufferTask(CompleteCallback callback, Object context) {
|
||||
this.callback = callback;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
protected boolean doInBackground(Object... params) {
|
||||
java.io.File javaFile = new java.io.File((String) params[0]);
|
||||
FileOutputStream stream = null;
|
||||
ByteBuffer content = (ByteBuffer) params[1];
|
||||
|
||||
try {
|
||||
stream = new FileOutputStream(javaFile);
|
||||
FileChannel channel = stream.getChannel();
|
||||
content.rewind();
|
||||
channel.write(content);
|
||||
content.rewind();
|
||||
return true;
|
||||
} catch (FileNotFoundException e) {
|
||||
Log.e(TAG, "Failed to write file, FileNotFoundException: " + e.getMessage());
|
||||
return false;
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Failed to write file, IOException: " + e.getMessage());
|
||||
return false;
|
||||
} finally {
|
||||
if (stream != null) {
|
||||
try {
|
||||
stream.close();
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Failed to close stream, IOException: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void onPostExecute(final boolean result) {
|
||||
if (result) {
|
||||
this.callback.onComplete(null, this.context);
|
||||
} else {
|
||||
this.callback.onError("WriteTask returns no result.", this.context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,11 @@ import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
@@ -302,8 +307,7 @@ public class FileHelper {
|
||||
}
|
||||
return context.getContentResolver().openInputStream(uri);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private OutputStream getOutputStream(Context context, Uri uri) throws Exception {
|
||||
if (Build.VERSION.SDK_INT >= 19) {
|
||||
if (isExternalStorageDocument(uri)) {
|
||||
@@ -332,6 +336,16 @@ public class FileHelper {
|
||||
return ret.buf();
|
||||
}
|
||||
|
||||
private ByteBuffer readBufferSyncInternal(Context context) throws Exception {
|
||||
InputStream is = getInputStream(context, uri);
|
||||
|
||||
ReadableByteChannel channel = Channels.newChannel(is);
|
||||
ByteBuffer buffer = ByteBuffer.allocateDirect(is.available());
|
||||
channel.read(buffer);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public @Nullable
|
||||
byte[] readSync(Context context, @Nullable Callback callback) {
|
||||
try {
|
||||
@@ -355,6 +369,29 @@ public class FileHelper {
|
||||
});
|
||||
}
|
||||
|
||||
public @Nullable
|
||||
ByteBuffer readBufferSync(Context context, @Nullable Callback callback) {
|
||||
try {
|
||||
return readBufferSyncInternal(context);
|
||||
} catch (Exception e) {
|
||||
if (callback != null) {
|
||||
callback.onError(e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void readBuffer(Context context, Callback callback) {
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
ByteBuffer result = readBufferSyncInternal(context);
|
||||
handler.post(() -> callback.onSuccess(result));
|
||||
} catch (Exception e) {
|
||||
handler.post(() -> callback.onError(e));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private String readTextSyncInternal(Context context, @Nullable String encoding) throws Exception {
|
||||
String characterSet = encoding;
|
||||
if (characterSet == null) {
|
||||
@@ -400,6 +437,15 @@ public class FileHelper {
|
||||
updateInternal(context);
|
||||
}
|
||||
|
||||
private void writeBufferSyncInternal(Context context,ByteBuffer content) throws Exception {
|
||||
OutputStream os = getOutputStream(context, uri);
|
||||
WritableByteChannel channel = Channels.newChannel(os);
|
||||
channel.write(content);
|
||||
os.flush();
|
||||
os.close();
|
||||
updateInternal(context);
|
||||
}
|
||||
|
||||
public void writeSync(Context context, byte[] content, @Nullable Callback callback) {
|
||||
try {
|
||||
writeSyncInternal(context, content);
|
||||
@@ -421,6 +467,27 @@ public class FileHelper {
|
||||
});
|
||||
}
|
||||
|
||||
public void writeBufferSync(Context context, ByteBuffer content, @Nullable Callback callback) {
|
||||
try {
|
||||
writeBufferSyncInternal(context, content);
|
||||
} catch (Exception e) {
|
||||
if (callback != null) {
|
||||
callback.onError(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void writeBuffer(Context context, ByteBuffer content, Callback callback) {
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
writeBufferSyncInternal(context, content);
|
||||
handler.post(() -> callback.onSuccess(null));
|
||||
} catch (Exception e) {
|
||||
handler.post(() -> callback.onError(e));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void writeTextSyncInternal(Context context, String content, @Nullable String encoding) throws Exception {
|
||||
OutputStream os = getOutputStream(context, uri);
|
||||
String characterSet = encoding;
|
||||
|
||||
Reference in New Issue
Block a user