Skip to content

Commit 6bbebd3

Browse files
authored
Merge pull request #353 from thinkproductivity/mjmasn-patch-1
(Android) Fix UTF-8 related crashes
2 parents 90ce0c4 + bfc51af commit 6bbebd3

File tree

2 files changed

+2
-26
lines changed

2 files changed

+2
-26
lines changed

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

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

2323
import java.io.*;
24-
import java.nio.ByteBuffer;
2524
import java.nio.charset.Charset;
26-
import java.nio.charset.CharsetEncoder;
2725
import java.security.MessageDigest;
2826
import java.util.ArrayList;
2927
import java.util.HashMap;
@@ -341,9 +339,7 @@ else if(resolved == null) {
341339
boolean error = false;
342340

343341
if (encoding.equalsIgnoreCase("utf8")) {
344-
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
345342
while ((cursor = fs.read(buffer)) != -1) {
346-
encoder.encode(ByteBuffer.wrap(buffer).asCharBuffer());
347343
String chunk = new String(buffer, 0, cursor);
348344
emitStreamEvent(streamId, "data", chunk);
349345
if(tick > 0)

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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@
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;
4339
import java.security.KeyStore;
4440
import java.util.ArrayList;
4541
import java.util.Arrays;
@@ -502,28 +498,12 @@ private void done(Response resp) {
502498
// encoding will somehow break the UTF8 string format, to encode UTF8
503499
// string correctly, we should do URL encoding before BASE64.
504500
byte[] b = resp.body().bytes();
505-
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
506501
if(responseFormat == ResponseFormat.BASE64) {
507502
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
508503
return;
509504
}
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-
}
505+
String utf8 = new String(b);
506+
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_UTF8, utf8);
527507
}
528508
} catch (IOException e) {
529509
callback.invoke("RNFetchBlob failed to encode response data to BASE64 string.", null);

0 commit comments

Comments
 (0)