4
4
import android .app .DownloadManager ;
5
5
import android .content .Intent ;
6
6
import android .net .Uri ;
7
- import android .support .annotation .Nullable ;
8
7
9
- import com .RNFetchBlob .Utils .EncodingResolver ;
10
- import com .RNFetchBlob .Utils .RNFBCookieJar ;
8
+ import com .RNFetchBlob .Utils .DataConverter ;
11
9
import com .facebook .react .bridge .ActivityEventListener ;
12
10
import com .facebook .react .bridge .Callback ;
13
11
import com .facebook .react .bridge .LifecycleEventListener ;
18
16
import com .facebook .react .bridge .ReadableArray ;
19
17
import com .facebook .react .bridge .ReadableMap ;
20
18
import com .facebook .react .bridge .WritableMap ;
21
- import com .facebook .react .modules .network .ForwardingCookieHandler ;
22
19
import com .facebook .react .modules .network .CookieJarContainer ;
20
+ import com .facebook .react .modules .network .ForwardingCookieHandler ;
23
21
import com .facebook .react .modules .network .OkHttpClientProvider ;
24
- import okhttp3 .OkHttpClient ;
25
- import okhttp3 .JavaNetCookieJar ;
26
22
27
23
import java .util .HashMap ;
28
24
import java .util .Map ;
29
25
import java .util .concurrent .LinkedBlockingQueue ;
30
26
import java .util .concurrent .ThreadPoolExecutor ;
31
27
import java .util .concurrent .TimeUnit ;
32
28
29
+ import okhttp3 .JavaNetCookieJar ;
30
+ import okhttp3 .OkHttpClient ;
31
+
33
32
import static android .app .Activity .RESULT_OK ;
34
33
import static com .RNFetchBlob .RNFetchBlobConst .GET_CONTENT_INTENT ;
34
+ import static com .RNFetchBlob .RNFetchBlobConst .RNFB_RESPONSE_ASCII ;
35
+ import static com .RNFetchBlob .RNFetchBlobConst .RNFB_RESPONSE_BASE64 ;
36
+ import static com .RNFetchBlob .RNFetchBlobConst .RNFB_RESPONSE_UTF8 ;
35
37
36
38
public class RNFetchBlob extends ReactContextBaseJavaModule {
37
39
@@ -315,39 +317,6 @@ public void run() {
315
317
});
316
318
}
317
319
318
- @ ReactMethod
319
- public void readChunk (final String path , final String encoding , final int offset , final int length , final Promise promise ) {
320
- fsThreadPool .execute (new Runnable () {
321
- @ Override
322
- public void run () {
323
- try {
324
- Object result = RNFetchBlobFS .readChunk (path , encoding , offset , length );
325
- EncodingResolver .resolve (promise , encoding , result );
326
- } catch (Exception e ) {
327
- e .printStackTrace ();
328
- promise .reject (e .getMessage (), e .getMessage ()) ;
329
- }
330
- }
331
- });
332
-
333
- }
334
-
335
- @ ReactMethod
336
- public void writeChunk (final String path , final String encoding , final String data , final int offset , final int length , final Promise promise ) {
337
- fsThreadPool .execute (new Runnable () {
338
- @ Override
339
- public void run () {
340
- try {
341
- RNFetchBlobFS .writeChunk (path , encoding , data , offset );
342
- promise .resolve (null );
343
- } catch (Exception e ) {
344
- e .printStackTrace ();
345
- promise .reject (e .getMessage (), e .getMessage ()) ;
346
- }
347
- }
348
- });
349
- }
350
-
351
320
352
321
@ ReactMethod
353
322
public void enableUploadProgressReport (String taskId , int interval , int count ) {
@@ -366,27 +335,99 @@ public void fetchBlobForm(ReadableMap options, String taskId, String method, Str
366
335
}
367
336
368
337
@ ReactMethod
369
- public void read (final String path , final String encoding , final int offset , final int length , final @ Nullable String dest , final Promise promise ) {
338
+ public void openFileHandle (final String path , final int mode , final Promise promise ) {
370
339
fsThreadPool .execute (new Runnable () {
371
340
@ Override
372
341
public void run () {
373
- RNFetchBlobFS fs = new RNFetchBlobFS (RCTContext );
374
- fs .read (path , encoding , offset , length , dest , promise );
342
+ RNFetchBlobOpenFile .Mode accessMode = RNFetchBlobOpenFile .Mode .READ ;
343
+ if (mode == 1 )
344
+ accessMode = RNFetchBlobOpenFile .Mode .READ ;
345
+ else if (mode == 2 )
346
+ accessMode = RNFetchBlobOpenFile .Mode .WRITE ;
347
+ else {
348
+ accessMode = RNFetchBlobOpenFile .Mode .READWRITE ;
349
+ }
350
+ try {
351
+ Integer id = RNFetchBlobFS .openFile (path , accessMode );
352
+ promise .resolve (id );
353
+ }
354
+ catch (Exception ex ) {
355
+ promise .reject (
356
+ "RNFetchBlob failed to open file handle" ,
357
+ DataConverter .exceptionToStringStackTrace (ex )
358
+ );
359
+ }
375
360
}
376
361
});
377
362
}
378
363
379
364
@ ReactMethod
380
- public void write (final String path , final String data , final String encoding , final int offset , final int length , final Promise promise ) {
365
+ public void readFromHandle (final int id , final String encoding , final int offset , final int length , final Promise promise ) {
381
366
fsThreadPool .execute (new Runnable () {
382
367
@ Override
383
368
public void run () {
384
- RNFetchBlobFS fs = new RNFetchBlobFS (RCTContext );
385
- fs .write (path , encoding , data , offset , length , promise );
369
+ try {
370
+ RNFetchBlobOpenFile handle = RNFetchBlobFS .fileHandles .get (id );
371
+ Object result = handle .read (encoding , offset , length );
372
+ switch (encoding ) {
373
+ case RNFB_RESPONSE_BASE64 :
374
+ promise .resolve ((String ) result );
375
+ break ;
376
+ case RNFB_RESPONSE_UTF8 :
377
+ promise .resolve ((String ) result );
378
+ break ;
379
+ case RNFB_RESPONSE_ASCII :
380
+ promise .resolve ((ReadableArray ) result );
381
+ break ;
382
+ }
383
+ }
384
+ catch (Exception ex ) {
385
+ promise .reject (
386
+ "RNFetchBlob failed to read from file handle" ,
387
+ DataConverter .exceptionToStringStackTrace (ex )
388
+ );
389
+ }
390
+
386
391
}
387
392
});
388
393
}
389
394
395
+ @ ReactMethod
396
+ public void writeToHandle (final int id , final String encoding , final int offset , final String data , final Promise promise ) {
397
+
398
+ fsThreadPool .execute (new Runnable () {
399
+ @ Override
400
+ public void run () {
401
+ try {
402
+ RNFetchBlobOpenFile handle = RNFetchBlobFS .fileHandles .get (id );
403
+ handle .write (encoding , data , offset );
404
+ promise .resolve (null );
405
+ }
406
+ catch (Exception ex ) {
407
+ promise .reject (
408
+ "RNFetchBlob failed to write tofile handle" ,
409
+ DataConverter .exceptionToStringStackTrace (ex )
410
+ );
411
+ }
412
+ }
413
+ });
414
+ }
415
+
416
+ @ ReactMethod
417
+ public void closeHandle (final int id , Promise promise ) {
418
+ try {
419
+ RNFetchBlobOpenFile handle = RNFetchBlobFS .fileHandles .get (id );
420
+ handle .close ();
421
+ promise .resolve (null );
422
+ }
423
+ catch (Exception ex ) {
424
+ promise .reject (
425
+ "RNFetchBlob failed to close handle" ,
426
+ DataConverter .exceptionToStringStackTrace (ex )
427
+ );
428
+ }
429
+ }
430
+
390
431
public void getContentIntent (String mime , Promise promise ) {
391
432
Intent i = new Intent (Intent .ACTION_GET_CONTENT );
392
433
if (mime != null )
0 commit comments