@@ -70,7 +70,7 @@ static void writeFile(String path, String encoding, String data, final boolean a
70
70
71
71
// write data from a file
72
72
if (encoding .equalsIgnoreCase (ReactNativeBlobUtilConst .DATA_ENCODE_URI )) {
73
- String normalizedData = normalizePath (data );
73
+ String normalizedData = ReactNativeBlobUtilUtils . normalizePath (data );
74
74
File src = new File (normalizedData );
75
75
if (!src .exists ()) {
76
76
promise .reject ("ENOENT" , "No such file '" + path + "' " + "('" + normalizedData + "')" );
@@ -97,7 +97,7 @@ static void writeFile(String path, String encoding, String data, final boolean a
97
97
}
98
98
}
99
99
} else {
100
- byte [] bytes = stringToBytes (data , encoding );
100
+ byte [] bytes = ReactNativeBlobUtilUtils . stringToBytes (data , encoding );
101
101
FileOutputStream fout = new FileOutputStream (f , append );
102
102
try {
103
103
fout .write (bytes );
@@ -167,7 +167,7 @@ static void writeFile(String path, ReadableArray data, final boolean append, fin
167
167
* @param promise JS promise
168
168
*/
169
169
static void readFile (String path , String encoding , final Promise promise ) {
170
- String resolved = normalizePath (path );
170
+ String resolved = ReactNativeBlobUtilUtils . normalizePath (path );
171
171
if (resolved != null )
172
172
path = resolved ;
173
173
try {
@@ -337,7 +337,7 @@ static String getTmpPath(String taskId) {
337
337
* @param bufferSize Buffer size of read stream, default to 4096 (4095 when encode is `base64`)
338
338
*/
339
339
void readStream (String path , String encoding , int bufferSize , int tick , final String streamId ) {
340
- String resolved = normalizePath (path );
340
+ String resolved = ReactNativeBlobUtilUtils . normalizePath (path );
341
341
if (resolved != null )
342
342
path = resolved ;
343
343
@@ -480,7 +480,7 @@ void writeStream(String path, String encoding, boolean append, Callback callback
480
480
static void writeChunk (String streamId , String data , Callback callback ) {
481
481
ReactNativeBlobUtilFS fs = fileStreams .get (streamId );
482
482
OutputStream stream = fs .writeStreamInstance ;
483
- byte [] chunk = ReactNativeBlobUtilFS .stringToBytes (data , fs .encoding );
483
+ byte [] chunk = ReactNativeBlobUtilUtils .stringToBytes (data , fs .encoding );
484
484
try {
485
485
stream .write (chunk );
486
486
callback .invoke ();
@@ -537,7 +537,7 @@ static void closeStream(String streamId, Callback callback) {
537
537
*/
538
538
static void unlink (String path , Callback callback ) {
539
539
try {
540
- String normalizedPath = normalizePath (path );
540
+ String normalizedPath = ReactNativeBlobUtilUtils . normalizePath (path );
541
541
ReactNativeBlobUtilFS .deleteRecursive (new File (normalizedPath ));
542
542
callback .invoke (null , true );
543
543
} catch (Exception err ) {
@@ -595,7 +595,7 @@ static void mkdir(String path, Promise promise) {
595
595
* @param callback JS context callback
596
596
*/
597
597
static void cp (String path , String dest , Callback callback ) {
598
- path = normalizePath (path );
598
+ path = ReactNativeBlobUtilUtils . normalizePath (path );
599
599
InputStream in = null ;
600
600
OutputStream out = null ;
601
601
String message = "" ;
@@ -700,7 +700,7 @@ static void exists(String path, Callback callback) {
700
700
callback .invoke (false , false );
701
701
}
702
702
} else {
703
- path = normalizePath (path );
703
+ path = ReactNativeBlobUtilUtils . normalizePath (path );
704
704
if (path != null ) {
705
705
boolean exist = new File (path ).exists ();
706
706
boolean isDir = new File (path ).isDirectory ();
@@ -719,7 +719,7 @@ static void exists(String path, Callback callback) {
719
719
*/
720
720
static void ls (String path , Promise promise ) {
721
721
try {
722
- path = normalizePath (path );
722
+ path = ReactNativeBlobUtilUtils . normalizePath (path );
723
723
File src = new File (path );
724
724
if (!src .exists ()) {
725
725
promise .reject ("ENOENT" , "No such file '" + path + "'" );
@@ -754,7 +754,7 @@ static void ls(String path, Promise promise) {
754
754
*/
755
755
static void slice (String path , String dest , int start , int end , String encode , Promise promise ) {
756
756
try {
757
- path = normalizePath (path );
757
+ path = ReactNativeBlobUtilUtils . normalizePath (path );
758
758
File source = new File (path );
759
759
if (source .isDirectory ()) {
760
760
promise .reject ("EISDIR" , "Expecting a file but '" + path + "' is a directory" );
@@ -796,7 +796,7 @@ static void slice(String path, String dest, int start, int end, String encode, P
796
796
}
797
797
798
798
static void lstat (String path , final Callback callback ) {
799
- path = normalizePath (path );
799
+ path = ReactNativeBlobUtilUtils . normalizePath (path );
800
800
801
801
new AsyncTask <String , Integer , Integer >() {
802
802
@ Override
@@ -835,7 +835,7 @@ protected Integer doInBackground(String... args) {
835
835
*/
836
836
static void stat (String path , Callback callback ) {
837
837
try {
838
- path = normalizePath (path );
838
+ path = ReactNativeBlobUtilUtils . normalizePath (path );
839
839
WritableMap result = statFile (path );
840
840
if (result == null )
841
841
callback .invoke ("failed to stat path `" + path + "` because it does not exist or it is not a folder" , null );
@@ -854,7 +854,7 @@ static void stat(String path, Callback callback) {
854
854
*/
855
855
static WritableMap statFile (String path ) {
856
856
try {
857
- path = normalizePath (path );
857
+ path = ReactNativeBlobUtilUtils . normalizePath (path );
858
858
WritableMap stat = Arguments .createMap ();
859
859
if (isAsset (path )) {
860
860
String name = path .replace (ReactNativeBlobUtilConst .FILE_PREFIX_BUNDLE_ASSET , "" );
@@ -990,7 +990,7 @@ static void createFile(String path, String data, String encoding, Promise promis
990
990
return ;
991
991
}
992
992
OutputStream ostream = new FileOutputStream (dest );
993
- ostream .write (ReactNativeBlobUtilFS .stringToBytes (data , encoding ));
993
+ ostream .write (ReactNativeBlobUtilUtils .stringToBytes (data , encoding ));
994
994
}
995
995
promise .resolve (path );
996
996
} catch (Exception err ) {
@@ -1085,25 +1085,6 @@ protected Integer doInBackground(ReadableArray... paths) {
1085
1085
task .execute (paths );
1086
1086
}
1087
1087
1088
- /**
1089
- * String to byte converter method
1090
- *
1091
- * @param data Raw data in string format
1092
- * @param encoding Decoder name
1093
- * @return Converted data byte array
1094
- */
1095
- private static byte [] stringToBytes (String data , String encoding ) {
1096
- if (encoding .equalsIgnoreCase ("ascii" )) {
1097
- return data .getBytes (Charset .forName ("US-ASCII" ));
1098
- } else if (encoding .toLowerCase ().contains ("base64" )) {
1099
- return Base64 .decode (data , Base64 .NO_WRAP );
1100
-
1101
- } else if (encoding .equalsIgnoreCase ("utf8" )) {
1102
- return data .getBytes (Charset .forName ("UTF-8" ));
1103
- }
1104
- return data .getBytes (Charset .forName ("US-ASCII" ));
1105
- }
1106
-
1107
1088
/**
1108
1089
* Private method for emit read stream event.
1109
1090
*
@@ -1174,26 +1155,4 @@ static boolean isAsset(String path) {
1174
1155
return path != null && path .startsWith (ReactNativeBlobUtilConst .FILE_PREFIX_BUNDLE_ASSET );
1175
1156
}
1176
1157
1177
- /**
1178
- * Normalize the path, remove URI scheme (xxx://) so that we can handle it.
1179
- *
1180
- * @param path URI string.
1181
- * @return Normalized string
1182
- */
1183
- static String normalizePath (String path ) {
1184
- if (path == null )
1185
- return null ;
1186
- if (!path .matches ("\\ w+\\ :.*" ))
1187
- return path ;
1188
- if (path .startsWith ("file://" )) {
1189
- return path .replace ("file://" , "" );
1190
- }
1191
-
1192
- Uri uri = Uri .parse (path );
1193
- if (path .startsWith (ReactNativeBlobUtilConst .FILE_PREFIX_BUNDLE_ASSET )) {
1194
- return path ;
1195
- } else
1196
- return PathResolver .getRealPathFromURI (ReactNativeBlobUtil .RCTContext , uri );
1197
- }
1198
-
1199
1158
}
0 commit comments