Skip to content

Commit 2146ac9

Browse files
PeterStaevmanoldonev
authored andcommitted
feat(file-system): async read/write (#7671)
1 parent 7908701 commit 2146ac9

File tree

14 files changed

+849
-29
lines changed

14 files changed

+849
-29
lines changed

tests/app/file-system/file-system-tests.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,46 @@ export var testFileReadWriteBinary = function () {
201201
// << file-system-read-binary
202202
};
203203

204+
export var testFileReadWriteBinaryAsync = function () {
205+
// >> file-system-read-binary-async
206+
var fileName = "logo.png";
207+
208+
var sourceFile = fs.File.fromPath(__dirname + "/assets/" + fileName);
209+
var destinationFile = fs.knownFolders.documents().getFile(fileName);
210+
211+
// Read the file
212+
sourceFile.read()
213+
.then(function (source) {
214+
// Succeeded in reading the file
215+
// >> (hide)
216+
destinationFile.write(source).then(function () {
217+
// Succeded in writing the file
218+
destinationFile.read()
219+
.then(function (destination) {
220+
if (platform.device.os === platform.platformNames.ios) {
221+
TKUnit.assertTrue(source.isEqualToData(destination));
222+
} else {
223+
TKUnit.assertEqual(new java.io.File(sourceFile.path).length(), new java.io.File(destinationFile.path).length());
224+
}
225+
226+
destinationFile.removeSync();
227+
}, function (error) {
228+
TKUnit.assert(false, "Failed to read destination binary async");
229+
});
230+
}, function (error) {
231+
// Failed to write the file.
232+
TKUnit.assert(false, "Failed to write binary async");
233+
});
234+
// << (hide)
235+
}, function (error) {
236+
// Failed to read the file.
237+
// >> (hide)
238+
TKUnit.assert(false, "Failed to read binary async");
239+
// << (hide)
240+
});
241+
// << file-system-read-binary-async
242+
};
243+
204244
export var testGetKnownFolders = function () {
205245
// >> file-system-known-folders
206246
// Getting the application's 'documents' folder.

tns-core-modules-widgets/android/widgets/src/main/java/org/nativescript/widgets/Async.java

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,22 @@
1111
import java.io.BufferedInputStream;
1212
import java.io.ByteArrayOutputStream;
1313
import java.io.Closeable;
14+
import java.io.DataInputStream;
15+
import java.io.FileOutputStream;
16+
import java.io.FileInputStream;
17+
import java.io.FileNotFoundException;
1418
import java.io.IOException;
1519
import java.io.InputStream;
20+
import java.io.InputStreamReader;
1621
import java.io.OutputStream;
1722
import java.io.OutputStreamWriter;
23+
import java.io.UnsupportedEncodingException;
1824
import java.net.CookieHandler;
1925
import java.net.CookieManager;
2026
import java.net.HttpURLConnection;
2127
import java.net.MalformedURLException;
2228
import java.net.URL;
29+
import java.nio.CharBuffer;
2330
import java.util.ArrayList;
2431
import java.util.List;
2532
import java.util.Locale;
@@ -585,4 +592,278 @@ private void closeOpenedStreams(Stack<Closeable> streams) throws IOException {
585592
}
586593
}
587594
}
595+
596+
public static class File {
597+
598+
public static void readText(final String path, final String encoding, final CompleteCallback callback, final Object context) {
599+
final android.os.Handler mHandler = new android.os.Handler();
600+
threadPoolExecutor().execute(new Runnable() {
601+
@Override
602+
public void run() {
603+
final ReadTextTask task = new ReadTextTask(callback, context);
604+
final String result = task.doInBackground(path, encoding);
605+
mHandler.post(new Runnable() {
606+
@Override
607+
public void run() {
608+
task.onPostExecute(result);
609+
}
610+
});
611+
}
612+
});
613+
}
614+
615+
public static void read(final String path, final CompleteCallback callback, final Object context) {
616+
final android.os.Handler mHandler = new android.os.Handler();
617+
threadPoolExecutor().execute(new Runnable() {
618+
@Override
619+
public void run() {
620+
final ReadTask task = new ReadTask(callback, context);
621+
final byte[] result = task.doInBackground(path);
622+
mHandler.post(new Runnable() {
623+
@Override
624+
public void run() {
625+
task.onPostExecute(result);
626+
}
627+
});
628+
}
629+
});
630+
}
631+
632+
public static void writeText(final String path, final String content, final String encoding, final CompleteCallback callback, final Object context) {
633+
final android.os.Handler mHandler = new android.os.Handler();
634+
threadPoolExecutor().execute(new Runnable() {
635+
@Override
636+
public void run() {
637+
final WriteTextTask task = new WriteTextTask(callback, context);
638+
final boolean result = task.doInBackground(path, content, encoding);
639+
mHandler.post(new Runnable() {
640+
@Override
641+
public void run() {
642+
task.onPostExecute(result);
643+
}
644+
});
645+
}
646+
});
647+
}
648+
649+
public static void write(final String path, final byte[] content, final CompleteCallback callback, final Object context) {
650+
final android.os.Handler mHandler = new android.os.Handler();
651+
threadPoolExecutor().execute(new Runnable() {
652+
@Override
653+
public void run() {
654+
final WriteTask task = new WriteTask(callback, context);
655+
final boolean result = task.doInBackground(path, content);
656+
mHandler.post(new Runnable() {
657+
@Override
658+
public void run() {
659+
task.onPostExecute(result);
660+
}
661+
});
662+
}
663+
});
664+
}
665+
666+
static class ReadTextTask {
667+
private CompleteCallback callback;
668+
private Object context;
669+
670+
public ReadTextTask(CompleteCallback callback, Object context) {
671+
this.callback = callback;
672+
this.context = context;
673+
}
674+
675+
protected String doInBackground(String... params) {
676+
java.io.File javaFile = new java.io.File(params[0]);
677+
FileInputStream stream = null;
678+
679+
try {
680+
stream = new FileInputStream(javaFile);
681+
682+
InputStreamReader reader = new InputStreamReader(stream, params[1]);
683+
684+
CharBuffer buffer = CharBuffer.allocate(81920);
685+
StringBuilder sb = new StringBuilder();
686+
687+
while (reader.read(buffer) != -1) {
688+
buffer.flip();
689+
sb.append(buffer);
690+
buffer.clear();
691+
}
692+
693+
reader.close();
694+
695+
return sb.toString();
696+
} catch (FileNotFoundException e) {
697+
Log.e(TAG, "Failed to read file, FileNotFoundException: " + e.getMessage());
698+
return null;
699+
} catch (UnsupportedEncodingException e) {
700+
Log.e(TAG, "Failed to read file, UnsupportedEncodingException: " + e.getMessage());
701+
return null;
702+
} catch (IOException e) {
703+
Log.e(TAG, "Failed to read file, IOException: " + e.getMessage());
704+
return null;
705+
} finally {
706+
if (stream != null) {
707+
try {
708+
stream.close();
709+
} catch (IOException e) {
710+
Log.e(TAG, "Failed to close stream, IOException: " + e.getMessage());
711+
}
712+
}
713+
}
714+
}
715+
716+
protected void onPostExecute(final String result) {
717+
if (result != null) {
718+
this.callback.onComplete(result, this.context);
719+
} else {
720+
this.callback.onError("ReadTextTask returns no result.", this.context);
721+
}
722+
}
723+
}
724+
725+
static class ReadTask {
726+
private CompleteCallback callback;
727+
private Object context;
728+
729+
public ReadTask(CompleteCallback callback, Object context) {
730+
this.callback = callback;
731+
this.context = context;
732+
}
733+
734+
protected byte[] doInBackground(String... params) {
735+
java.io.File javaFile = new java.io.File(params[0]);
736+
FileInputStream stream = null;
737+
738+
try {
739+
stream = new FileInputStream(javaFile);
740+
741+
byte[] result = new byte[(int)javaFile.length()];
742+
743+
DataInputStream dataInputStream = new DataInputStream(stream);
744+
dataInputStream.readFully(result);
745+
746+
return result;
747+
} catch (FileNotFoundException e) {
748+
Log.e(TAG, "Failed to read file, FileNotFoundException: " + e.getMessage());
749+
return null;
750+
} catch (IOException e) {
751+
Log.e(TAG, "Failed to read file, IOException: " + e.getMessage());
752+
return null;
753+
} finally {
754+
if (stream != null) {
755+
try {
756+
stream.close();
757+
} catch (IOException e) {
758+
Log.e(TAG, "Failed to close stream, IOException: " + e.getMessage());
759+
}
760+
}
761+
}
762+
}
763+
764+
protected void onPostExecute(final byte[] result) {
765+
if (result != null) {
766+
this.callback.onComplete(result, this.context);
767+
} else {
768+
this.callback.onError("ReadTask returns no result.", this.context);
769+
}
770+
}
771+
}
772+
773+
static class WriteTextTask {
774+
private CompleteCallback callback;
775+
private Object context;
776+
777+
public WriteTextTask(CompleteCallback callback, Object context) {
778+
this.callback = callback;
779+
this.context = context;
780+
}
781+
782+
protected boolean doInBackground(String... params) {
783+
java.io.File javaFile = new java.io.File(params[0]);
784+
FileOutputStream stream = null;
785+
try {
786+
stream = new FileOutputStream(javaFile);
787+
788+
OutputStreamWriter writer = new OutputStreamWriter(stream, params[2]);
789+
790+
writer.write(params[1]);
791+
writer.close();
792+
793+
return true;
794+
} catch (FileNotFoundException e) {
795+
Log.e(TAG, "Failed to write file, FileNotFoundException: " + e.getMessage());
796+
return false;
797+
} catch (UnsupportedEncodingException e) {
798+
Log.e(TAG, "Failed to write file, UnsupportedEncodingException: " + e.getMessage());
799+
return false;
800+
} catch (IOException e) {
801+
Log.e(TAG, "Failed to write file, IOException: " + e.getMessage());
802+
return false;
803+
} finally {
804+
if (stream != null) {
805+
try {
806+
stream.close();
807+
} catch (IOException e) {
808+
Log.e(TAG, "Failed to close stream, IOException: " + e.getMessage());
809+
}
810+
}
811+
}
812+
}
813+
814+
protected void onPostExecute(final boolean result) {
815+
if (result) {
816+
this.callback.onComplete(null, this.context);
817+
} else {
818+
this.callback.onError("WriteTextTask returns no result.", this.context);
819+
}
820+
}
821+
}
822+
823+
static class WriteTask {
824+
private CompleteCallback callback;
825+
private Object context;
826+
827+
public WriteTask(CompleteCallback callback, Object context) {
828+
this.callback = callback;
829+
this.context = context;
830+
}
831+
832+
protected boolean doInBackground(Object... params) {
833+
java.io.File javaFile = new java.io.File((String)params[0]);
834+
FileOutputStream stream = null;
835+
byte[] content = (byte[])params[1];
836+
837+
try {
838+
stream = new FileOutputStream(javaFile);
839+
stream.write(content, 0, content.length);
840+
841+
return true;
842+
} catch (FileNotFoundException e) {
843+
Log.e(TAG, "Failed to write file, FileNotFoundException: " + e.getMessage());
844+
return false;
845+
} catch (IOException e) {
846+
Log.e(TAG, "Failed to write file, IOException: " + e.getMessage());
847+
return false;
848+
} finally {
849+
if (stream != null) {
850+
try {
851+
stream.close();
852+
} catch (IOException e) {
853+
Log.e(TAG, "Failed to close stream, IOException: " + e.getMessage());
854+
}
855+
}
856+
}
857+
}
858+
859+
protected void onPostExecute(final boolean result) {
860+
if (result) {
861+
this.callback.onComplete(null, this.context);
862+
} else {
863+
this.callback.onError("WriteTask returns no result.", this.context);
864+
}
865+
}
866+
}
867+
868+
}
588869
}

0 commit comments

Comments
 (0)