37
37
import com .facebook .react .modules .core .DeviceEventManagerModule .RCTDeviceEventEmitter ;
38
38
39
39
import java .io .ByteArrayOutputStream ;
40
+ import java .io .Closeable ;
40
41
import java .io .File ;
41
42
import java .io .FileInputStream ;
42
43
import java .io .FileNotFoundException ;
@@ -63,6 +64,18 @@ public class ReactNativeFsModule extends ReactNativeFsSpec {
63
64
private ArrayDeque <Promise > pendingPickFilePromises = new ArrayDeque <Promise >();
64
65
private ActivityResultLauncher <String []> pickFileLauncher ;
65
66
67
+ /**
68
+ * Attempts to close given object, discarting possible exception. Does nothing
69
+ * if given argument is null.
70
+ * @param closeable
71
+ */
72
+ static private void closeIgnoringException (Closeable closeable ) {
73
+ if (closeable != null ) {
74
+ try { closeable .close (); }
75
+ catch (Exception e ) {}
76
+ }
77
+ }
78
+
66
79
ReactNativeFsModule (ReactApplicationContext context ) {
67
80
super (context );
68
81
@@ -137,12 +150,9 @@ public void addListener(String eventName) {
137
150
138
151
@ ReactMethod
139
152
public void appendFile (String filepath , String base64Content , Promise promise ) {
140
- try {
153
+ try ( OutputStream outputStream = getOutputStream ( filepath , true )) {
141
154
byte [] bytes = Base64 .decode (base64Content , Base64 .DEFAULT );
142
-
143
- OutputStream outputStream = getOutputStream (filepath , true );
144
155
outputStream .write (bytes );
145
- outputStream .close ();
146
156
147
157
promise .resolve (null );
148
158
} catch (Exception ex ) {
@@ -333,19 +343,10 @@ public void existsAssets(String filepath, Promise promise) {
333
343
}
334
344
335
345
// Attempt to open file (win = exists)
336
- InputStream fileStream = null ;
337
- try {
338
- fileStream = assetManager .open (filepath );
346
+ try (InputStream fileStream = assetManager .open (filepath )) {
339
347
promise .resolve (true );
340
348
} catch (Exception ex ) {
341
349
promise .resolve (false ); // don't throw an error, resolve false
342
- } finally {
343
- if (fileStream != null ) {
344
- try {
345
- fileStream .close ();
346
- } catch (Exception ignored ) {
347
- }
348
- }
349
350
}
350
351
} catch (Exception ex ) {
351
352
ex .printStackTrace ();
@@ -409,6 +410,7 @@ public void getFSInfo(Promise promise) {
409
410
410
411
@ ReactMethod
411
412
public void hash (String filepath , String algorithm , Promise promise ) {
413
+ FileInputStream inputStream = null ;
412
414
try {
413
415
Map <String , String > algorithms = new HashMap <>();
414
416
@@ -435,7 +437,7 @@ public void hash(String filepath, String algorithm, Promise promise) {
435
437
436
438
MessageDigest md = MessageDigest .getInstance (algorithms .get (algorithm ));
437
439
438
- FileInputStream inputStream = new FileInputStream (filepath );
440
+ inputStream = new FileInputStream (filepath );
439
441
byte [] buffer = new byte [1024 * 10 ]; // 10 KB Buffer
440
442
441
443
int read ;
@@ -451,6 +453,8 @@ public void hash(String filepath, String algorithm, Promise promise) {
451
453
} catch (Exception ex ) {
452
454
ex .printStackTrace ();
453
455
reject (promise , filepath , ex );
456
+ } finally {
457
+ closeIgnoringException (inputStream );
454
458
}
455
459
}
456
460
@@ -539,8 +543,7 @@ public void read(
539
543
double position ,
540
544
Promise promise
541
545
) {
542
- try {
543
- InputStream inputStream = getInputStream (filepath );
546
+ try (InputStream inputStream = getInputStream (filepath )) {
544
547
byte [] buffer = new byte [(int )length ];
545
548
inputStream .skip ((int )position );
546
549
int bytesRead = inputStream .read (buffer , 0 , (int )length );
@@ -624,8 +627,7 @@ public void readDirAssets(String directory, Promise promise) {
624
627
625
628
@ ReactMethod
626
629
public void readFile (String filepath , Promise promise ) {
627
- try {
628
- InputStream inputStream = getInputStream (filepath );
630
+ try (InputStream inputStream = getInputStream (filepath )) {
629
631
byte [] inputData = getInputStreamBytes (inputStream );
630
632
String base64Content = Base64 .encodeToString (inputData , Base64 .NO_WRAP );
631
633
@@ -656,12 +658,7 @@ public void readFileAssets(String filepath, Promise promise) {
656
658
ex .printStackTrace ();
657
659
reject (promise , filepath , ex );
658
660
} finally {
659
- if (stream != null ) {
660
- try {
661
- stream .close ();
662
- } catch (IOException ignored ) {
663
- }
664
- }
661
+ closeIgnoringException (stream );
665
662
}
666
663
}
667
664
@@ -684,12 +681,7 @@ public void readFileRes(String filename, Promise promise) {
684
681
ex .printStackTrace ();
685
682
reject (promise , filename , ex );
686
683
} finally {
687
- if (stream != null ) {
688
- try {
689
- stream .close ();
690
- } catch (IOException ignored ) {
691
- }
692
- }
684
+ closeIgnoringException (stream );
693
685
}
694
686
}
695
687
@@ -794,7 +786,7 @@ public void touch(String filepath, ReadableMap options, Promise promise) {
794
786
promise .resolve (file .setLastModified ((long ) mtime ));
795
787
} catch (Exception ex ) {
796
788
ex .printStackTrace ();
797
- reject (promise , filepath , ex );
789
+ reject (promise , filepath , ex );
798
790
}
799
791
}
800
792
@@ -899,35 +891,35 @@ public void write(
899
891
double position ,
900
892
Promise promise
901
893
) {
894
+ OutputStream outputStream = null ;
895
+ RandomAccessFile file = null ;
902
896
try {
903
897
byte [] bytes = Base64 .decode (base64Content , Base64 .DEFAULT );
904
898
905
899
if (position < 0 ) {
906
- OutputStream outputStream = getOutputStream (filepath , true );
900
+ outputStream = getOutputStream (filepath , true );
907
901
outputStream .write (bytes );
908
- outputStream .close ();
909
902
} else {
910
- RandomAccessFile file = new RandomAccessFile (filepath , "rw" );
903
+ file = new RandomAccessFile (filepath , "rw" );
911
904
file .seek ((long )position );
912
905
file .write (bytes );
913
- file .close ();
914
906
}
915
907
916
908
promise .resolve (null );
917
909
} catch (Exception ex ) {
918
910
ex .printStackTrace ();
919
911
reject (promise , filepath , ex );
912
+ } finally {
913
+ closeIgnoringException (outputStream );
914
+ closeIgnoringException (file );
920
915
}
921
916
}
922
917
923
918
@ ReactMethod
924
919
public void writeFile (String filepath , String base64Content , ReadableMap options , Promise promise ) {
925
- try {
920
+ try ( OutputStream outputStream = getOutputStream ( filepath , false )) {
926
921
byte [] bytes = Base64 .decode (base64Content , Base64 .DEFAULT );
927
-
928
- OutputStream outputStream = getOutputStream (filepath , false );
929
922
outputStream .write (bytes );
930
- outputStream .close ();
931
923
932
924
promise .resolve (null );
933
925
} catch (Exception ex ) {
@@ -938,24 +930,27 @@ public void writeFile(String filepath, String base64Content, ReadableMap options
938
930
939
931
private class CopyFileTask extends AsyncTask <String , Void , Exception > {
940
932
protected Exception doInBackground (String ... paths ) {
933
+ InputStream in = null ;
934
+ OutputStream out = null ;
941
935
try {
942
936
String filepath = paths [0 ];
943
937
String destPath = paths [1 ];
944
938
945
- InputStream in = getInputStream (filepath );
946
- OutputStream out = getOutputStream (destPath , false );
939
+ in = getInputStream (filepath );
940
+ out = getOutputStream (destPath , false );
947
941
948
942
byte [] buffer = new byte [1024 ];
949
943
int length ;
950
944
while ((length = in .read (buffer )) > 0 ) {
951
945
out .write (buffer , 0 , length );
952
946
Thread .yield ();
953
947
}
954
- in .close ();
955
- out .close ();
956
948
return null ;
957
949
} catch (Exception ex ) {
958
950
return ex ;
951
+ } finally {
952
+ closeIgnoringException (in );
953
+ closeIgnoringException (out );
959
954
}
960
955
}
961
956
}
@@ -984,18 +979,8 @@ private void copyInputStream(InputStream in, String source, String destination,
984
979
} catch (Exception ex ) {
985
980
reject (promise , source , new Exception (String .format ("Failed to copy '%s' to %s (%s)" , source , destination , ex .getLocalizedMessage ())));
986
981
} finally {
987
- if (in != null ) {
988
- try {
989
- in .close ();
990
- } catch (IOException ignored ) {
991
- }
992
- }
993
- if (out != null ) {
994
- try {
995
- out .close ();
996
- } catch (IOException ignored ) {
997
- }
998
- }
982
+ closeIgnoringException (in );
983
+ closeIgnoringException (out );
999
984
}
1000
985
}
1001
986
@@ -1038,20 +1023,14 @@ private InputStream getInputStream(String filepath) throws IORejectionException
1038
1023
1039
1024
private static byte [] getInputStreamBytes (InputStream inputStream ) throws IOException {
1040
1025
byte [] bytesResult ;
1041
- ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream ();
1042
1026
int bufferSize = 1024 ;
1043
1027
byte [] buffer = new byte [bufferSize ];
1044
- try {
1028
+ try ( ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream ()) {
1045
1029
int len ;
1046
1030
while ((len = inputStream .read (buffer )) != -1 ) {
1047
1031
byteBuffer .write (buffer , 0 , len );
1048
1032
}
1049
1033
bytesResult = byteBuffer .toByteArray ();
1050
- } finally {
1051
- try {
1052
- byteBuffer .close ();
1053
- } catch (IOException ignored ) {
1054
- }
1055
1034
}
1056
1035
return bytesResult ;
1057
1036
}
@@ -1127,57 +1106,3 @@ private void sendEvent(ReactContext reactContext, String eventName, WritableMap
1127
1106
emitter .emit (eventName , params );
1128
1107
}
1129
1108
}
1130
-
1131
- /**
1132
- * TODO: This is the original module file
1133
- *
1134
- -
1135
- -
1136
- -
1137
- -
1138
- -
1139
- -
1140
- -
1141
- -
1142
- -
1143
- -
1144
- -
1145
- -
1146
- -
1147
- -import com.facebook.react.bridge.Promise;
1148
- -import com.facebook.react.bridge.ReactApplicationContext;
1149
- -
1150
- -import com.facebook.react.bridge.ReactContextBaseJavaModule;
1151
- -import com.facebook.react.bridge.ReactMethod;
1152
- -
1153
-
1154
- -
1155
- -
1156
- -import com.facebook.react.module.annotations.ReactModule;
1157
- -
1158
- -
1159
- -
1160
- -
1161
- -
1162
- -import java.util.HashMap;
1163
- -import java.util.Map;
1164
- -
1165
- - private static final String RNFSDocumentDirectoryPath = "RNFSDocumentDirectoryPath";
1166
- - private static final String RNFSExternalDirectoryPath = "RNFSExternalDirectoryPath";
1167
- - private static final String RNFSExternalStorageDirectoryPath = "RNFSExternalStorageDirectoryPath";
1168
- - private static final String RNFSPicturesDirectoryPath = "RNFSPicturesDirectoryPath";
1169
- - private static final String RNFSDownloadDirectoryPath = "RNFSDownloadDirectoryPath";
1170
- - private static final String RNFSTemporaryDirectoryPath = "RNFSTemporaryDirectoryPath";
1171
- - private static final String RNFSCachesDirectoryPath = "RNFSCachesDirectoryPath";
1172
- - private static final String RNFSExternalCachesDirectoryPath = "RNFSExternalCachesDirectoryPath";
1173
- - private static final String RNFSDocumentDirectory = "RNFSDocumentDirectory";
1174
- -
1175
- - private static final String RNFSFileTypeRegular = "RNFSFileTypeRegular";
1176
- - private static final String RNFSFileTypeDirectory = "RNFSFileTypeDirectory";
1177
- -
1178
- - private SparseArray<Downloader> downloaders = new SparseArray<>();
1179
- - private SparseArray<Uploader> uploaders = new SparseArray<>();
1180
- -
1181
- - private ReactApplicationContext reactContext;
1182
- -
1183
- */
0 commit comments