@@ -519,15 +519,15 @@ static void mkdir(String path, Promise promise) {
519519 * @param callback JS context callback
520520 */
521521 static void cp (String path , String dest , Callback callback ) {
522- path = ReactNativeBlobUtilUtils .normalizePath (path );
523522 dest = ReactNativeBlobUtilUtils .normalizePath (dest );
524523 InputStream in = null ;
525524 OutputStream out = null ;
526525 String message = "" ;
527526
528527 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" );
531531 return ;
532532 }
533533 if (!new File (dest ).exists ()) {
@@ -538,7 +538,6 @@ static void cp(String path, String dest, Callback callback) {
538538 }
539539 }
540540
541- in = inputStreamFromPath (path );
542541 out = new FileOutputStream (dest );
543542
544543 byte [] buf = new byte [10240 ];
@@ -681,37 +680,36 @@ static void ls(String path, Promise promise) {
681680 */
682681 static void slice (String path , String dest , int start , int end , String encode , Promise promise ) {
683682 try {
684- path = ReactNativeBlobUtilUtils .normalizePath (path );
685683 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+ }
690691 }
691- if (!source .exists ()) {
692+
693+ InputStream in = inputStreamFromPath (path );
694+ if (in == null ) {
692695 promise .reject ("ENOENT" , "No such file '" + path + "'" );
693696 return ;
694697 }
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 ));
700698 FileOutputStream out = new FileOutputStream (new File (dest ));
701699 int skipped = (int ) in .skip (start );
702700 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" );
704702 return ;
705703 }
706704 byte [] buffer = new byte [10240 ];
707- while (now < expected ) {
705+ int remain = end - start ;
706+ while (remain > 0 ) {
708707 int read = in .read (buffer , 0 , 10240 );
709- int remain = expected - now ;
710708 if (read <= 0 ) {
711709 break ;
712710 }
713711 out .write (buffer , 0 , (int ) Math .min (remain , read ));
714- now + = read ;
712+ remain - = read ;
715713 }
716714 in .close ();
717715 out .flush ();
@@ -847,31 +845,27 @@ static void hash(String path, String algorithm, Promise promise) {
847845 return ;
848846 }
849847
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+ }
857854 }
858855
859- if (!file .exists ()) {
856+ MessageDigest md = MessageDigest .getInstance (algorithms .get (algorithm ));
857+
858+ InputStream inputStream = inputStreamFromPath (path );
859+ if (inputStream == null ) {
860860 promise .reject ("ENOENT" , "No such file '" + path + "'" );
861861 return ;
862862 }
863-
864- MessageDigest md = MessageDigest .getInstance (algorithms .get (algorithm ));
865-
866- FileInputStream inputStream = new FileInputStream (path );
867863 int chunkSize = 4096 * 256 ; // 1Mb
868864 byte [] buffer = new byte [chunkSize ];
869865
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 );
875869 }
876870
877871 StringBuilder hexString = new StringBuilder ();
@@ -1018,8 +1012,10 @@ protected Integer doInBackground(ReadableArray... paths) {
10181012 }
10191013
10201014 /**
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.
10231019 *
10241020 * @param path The file to open stream
10251021 * @return InputStream instance
@@ -1029,7 +1025,10 @@ private static InputStream inputStreamFromPath(String path) throws IOException {
10291025 if (path .startsWith (ReactNativeBlobUtilConst .FILE_PREFIX_BUNDLE_ASSET )) {
10301026 return ReactNativeBlobUtilImpl .RCTContext .getAssets ().open (path .replace (ReactNativeBlobUtilConst .FILE_PREFIX_BUNDLE_ASSET , "" ));
10311027 }
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 )));
10331032 }
10341033
10351034 /**
0 commit comments