@@ -519,15 +519,15 @@ static void mkdir(String path, Promise promise) {
519
519
* @param callback JS context callback
520
520
*/
521
521
static void cp (String path , String dest , Callback callback ) {
522
- path = ReactNativeBlobUtilUtils .normalizePath (path );
523
522
dest = ReactNativeBlobUtilUtils .normalizePath (dest );
524
523
InputStream in = null ;
525
524
OutputStream out = null ;
526
525
String message = "" ;
527
526
528
527
try {
529
- if (!isPathExists (path )) {
530
- callback .invoke ("Source file at path`" + path + "` does not exist" );
528
+ in = inputStreamFromPath (path );
529
+ if (in == null ) {
530
+ callback .invoke ("Source file at path`" + path + "` does not exist or can not be opened" );
531
531
return ;
532
532
}
533
533
if (!new File (dest ).exists ()) {
@@ -538,7 +538,6 @@ static void cp(String path, String dest, Callback callback) {
538
538
}
539
539
}
540
540
541
- in = inputStreamFromPath (path );
542
541
out = new FileOutputStream (dest );
543
542
544
543
byte [] buf = new byte [10240 ];
@@ -681,37 +680,36 @@ static void ls(String path, Promise promise) {
681
680
*/
682
681
static void slice (String path , String dest , int start , int end , String encode , Promise promise ) {
683
682
try {
684
- path = ReactNativeBlobUtilUtils .normalizePath (path );
685
683
dest = ReactNativeBlobUtilUtils .normalizePath (dest );
686
- File source = new File (path );
687
- if (source .isDirectory ()) {
688
- promise .reject ("EISDIR" , "Expecting a file but '" + path + "' is a directory" );
689
- return ;
684
+
685
+ if (!path .startsWith (ReactNativeBlobUtilConst .FILE_PREFIX_CONTENT )) {
686
+ File file = new File (ReactNativeBlobUtilUtils .normalizePath (path ));
687
+ if (file .isDirectory ()) {
688
+ promise .reject ("EISDIR" , "Expecting a file but '" + path + "' is a directory" );
689
+ return ;
690
+ }
690
691
}
691
- if (!source .exists ()) {
692
+
693
+ InputStream in = inputStreamFromPath (path );
694
+ if (in == null ) {
692
695
promise .reject ("ENOENT" , "No such file '" + path + "'" );
693
696
return ;
694
697
}
695
- int size = (int ) source .length ();
696
- int max = Math .min (size , end );
697
- int expected = max - start ;
698
- int now = 0 ;
699
- FileInputStream in = new FileInputStream (new File (path ));
700
698
FileOutputStream out = new FileOutputStream (new File (dest ));
701
699
int skipped = (int ) in .skip (start );
702
700
if (skipped != start ) {
703
- promise .reject ("EUNSPECIFIED" , "Skipped " + skipped + " instead of the specified " + start + " bytes, size is " + size );
701
+ promise .reject ("EUNSPECIFIED" , "Skipped " + skipped + " instead of the specified " + start + " bytes" );
704
702
return ;
705
703
}
706
704
byte [] buffer = new byte [10240 ];
707
- while (now < expected ) {
705
+ int remain = end - start ;
706
+ while (remain > 0 ) {
708
707
int read = in .read (buffer , 0 , 10240 );
709
- int remain = expected - now ;
710
708
if (read <= 0 ) {
711
709
break ;
712
710
}
713
711
out .write (buffer , 0 , (int ) Math .min (remain , read ));
714
- now + = read ;
712
+ remain - = read ;
715
713
}
716
714
in .close ();
717
715
out .flush ();
@@ -847,31 +845,27 @@ static void hash(String path, String algorithm, Promise promise) {
847
845
return ;
848
846
}
849
847
850
- path = ReactNativeBlobUtilUtils .normalizePath (path );
851
-
852
- File file = new File (path );
853
-
854
- if (file .isDirectory ()) {
855
- promise .reject ("EISDIR" , "Expecting a file but '" + path + "' is a directory" );
856
- return ;
848
+ if (!path .startsWith (ReactNativeBlobUtilConst .FILE_PREFIX_CONTENT )) {
849
+ File file = new File (ReactNativeBlobUtilUtils .normalizePath (path ));
850
+ if (file .isDirectory ()) {
851
+ promise .reject ("EISDIR" , "Expecting a file but '" + path + "' is a directory" );
852
+ return ;
853
+ }
857
854
}
858
855
859
- if (!file .exists ()) {
856
+ MessageDigest md = MessageDigest .getInstance (algorithms .get (algorithm ));
857
+
858
+ InputStream inputStream = inputStreamFromPath (path );
859
+ if (inputStream == null ) {
860
860
promise .reject ("ENOENT" , "No such file '" + path + "'" );
861
861
return ;
862
862
}
863
-
864
- MessageDigest md = MessageDigest .getInstance (algorithms .get (algorithm ));
865
-
866
- FileInputStream inputStream = new FileInputStream (path );
867
863
int chunkSize = 4096 * 256 ; // 1Mb
868
864
byte [] buffer = new byte [chunkSize ];
869
865
870
- if (file .length () != 0 ) {
871
- int bytesRead ;
872
- while ((bytesRead = inputStream .read (buffer )) != -1 ) {
873
- md .update (buffer , 0 , bytesRead );
874
- }
866
+ int bytesRead ;
867
+ while ((bytesRead = inputStream .read (buffer )) != -1 ) {
868
+ md .update (buffer , 0 , bytesRead );
875
869
}
876
870
877
871
StringBuilder hexString = new StringBuilder ();
@@ -1018,8 +1012,10 @@ protected Integer doInBackground(ReadableArray... paths) {
1018
1012
}
1019
1013
1020
1014
/**
1021
- * Get input stream of the given path, when the path is a string starts with bundle-assets://
1022
- * the stream is created by Assets Manager, otherwise use FileInputStream.
1015
+ * Get input stream of the given path.
1016
+ * When the path starts with bundle-assets:// the stream is created by Assets Manager
1017
+ * When the path starts with content:// the stream is created by ContentResolver
1018
+ * otherwise use FileInputStream.
1023
1019
*
1024
1020
* @param path The file to open stream
1025
1021
* @return InputStream instance
@@ -1029,7 +1025,10 @@ private static InputStream inputStreamFromPath(String path) throws IOException {
1029
1025
if (path .startsWith (ReactNativeBlobUtilConst .FILE_PREFIX_BUNDLE_ASSET )) {
1030
1026
return ReactNativeBlobUtilImpl .RCTContext .getAssets ().open (path .replace (ReactNativeBlobUtilConst .FILE_PREFIX_BUNDLE_ASSET , "" ));
1031
1027
}
1032
- return new FileInputStream (new File (path ));
1028
+ if (path .startsWith (ReactNativeBlobUtilConst .FILE_PREFIX_CONTENT )) {
1029
+ return ReactNativeBlobUtilImpl .RCTContext .getContentResolver ().openInputStream (Uri .parse (path ));
1030
+ }
1031
+ return new FileInputStream (new File (ReactNativeBlobUtilUtils .normalizePath (path )));
1033
1032
}
1034
1033
1035
1034
/**
0 commit comments