|
|
|
|
503
|
// encoding will somehow break the UTF8 string format, to encode UTF8
|
503
|
// encoding will somehow break the UTF8 string format, to encode UTF8
|
504
|
// string correctly, we should do URL encoding before BASE64.
|
504
|
// string correctly, we should do URL encoding before BASE64.
|
505
|
byte[] b = resp.body().bytes();
|
505
|
byte[] b = resp.body().bytes();
|
506
|
- CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
|
|
|
507
|
if(responseFormat == ResponseFormat.BASE64) {
|
506
|
if(responseFormat == ResponseFormat.BASE64) {
|
508
|
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
|
507
|
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
|
509
|
return;
|
508
|
return;
|
510
|
}
|
509
|
}
|
511
|
- try {
|
|
|
512
|
- encoder.encode(ByteBuffer.wrap(b).asCharBuffer());
|
|
|
513
|
- // if the data contains invalid characters the following lines will be
|
|
|
514
|
- // skipped.
|
|
|
515
|
- String utf8 = new String(b);
|
|
|
516
|
- callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_UTF8, utf8);
|
|
|
517
|
- }
|
|
|
518
|
- // This usually mean the data is contains invalid unicode characters, it's
|
|
|
519
|
- // binary data
|
|
|
520
|
- catch(CharacterCodingException ignored) {
|
|
|
521
|
- if(responseFormat == ResponseFormat.UTF8) {
|
|
|
522
|
- callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_UTF8, "");
|
|
|
523
|
- }
|
|
|
524
|
- else {
|
|
|
525
|
- callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
|
|
|
526
|
- }
|
|
|
527
|
- }
|
|
|
|
|
510
|
+ String utf8 = new String(b);
|
|
|
511
|
+ callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_UTF8, utf8);
|
528
|
}
|
512
|
}
|
529
|
} catch (IOException e) {
|
513
|
} catch (IOException e) {
|
530
|
callback.invoke("RNFetchBlob failed to encode response data to BASE64 string.", null);
|
514
|
callback.invoke("RNFetchBlob failed to encode response data to BASE64 string.", null);
|