Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 00c12e3

Browse files
committedSep 27, 2019
Revert "Merge pull request joltup#353 from thinkproductivity/mjmasn-patch-1"
This reverts commit 6bbebd3, reversing changes made to 90ce0c4.
1 parent fb60952 commit 00c12e3

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed
 

‎android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import com.facebook.react.modules.core.DeviceEventManagerModule;
2222

2323
import java.io.*;
24+
import java.nio.ByteBuffer;
2425
import java.nio.charset.Charset;
26+
import java.nio.charset.CharsetEncoder;
2527
import java.security.MessageDigest;
2628
import java.util.ArrayList;
2729
import java.util.HashMap;
@@ -339,7 +341,9 @@ else if(resolved == null) {
339341
boolean error = false;
340342

341343
if (encoding.equalsIgnoreCase("utf8")) {
344+
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
342345
while ((cursor = fs.read(buffer)) != -1) {
346+
encoder.encode(ByteBuffer.wrap(buffer).asCharBuffer());
343347
String chunk = new String(buffer, 0, cursor);
344348
emitStreamEvent(streamId, "data", chunk);
345349
if(tick > 0)

‎android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
import java.net.SocketException;
3737
import java.net.SocketTimeoutException;
3838
import java.net.URL;
39+
import java.nio.ByteBuffer;
40+
import java.nio.charset.CharacterCodingException;
41+
import java.nio.charset.Charset;
42+
import java.nio.charset.CharsetEncoder;
3943
import java.security.KeyStore;
4044
import java.util.ArrayList;
4145
import java.util.Arrays;
@@ -498,12 +502,28 @@ private void done(Response resp) {
498502
// encoding will somehow break the UTF8 string format, to encode UTF8
499503
// string correctly, we should do URL encoding before BASE64.
500504
byte[] b = resp.body().bytes();
505+
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
501506
if(responseFormat == ResponseFormat.BASE64) {
502507
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
503508
return;
504509
}
505-
String utf8 = new String(b);
506-
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_UTF8, utf8);
510+
try {
511+
encoder.encode(ByteBuffer.wrap(b).asCharBuffer());
512+
// if the data contains invalid characters the following lines will be
513+
// skipped.
514+
String utf8 = new String(b);
515+
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_UTF8, utf8);
516+
}
517+
// This usually mean the data is contains invalid unicode characters, it's
518+
// binary data
519+
catch(CharacterCodingException ignored) {
520+
if(responseFormat == ResponseFormat.UTF8) {
521+
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_UTF8, "");
522+
}
523+
else {
524+
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
525+
}
526+
}
507527
}
508528
} catch (IOException e) {
509529
callback.invoke("RNFetchBlob failed to encode response data to BASE64 string.", null);

0 commit comments

Comments
 (0)
Please sign in to comment.