Skip to content

(Android) Fix UTF-8 related crashes #353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
import com.facebook.react.modules.core.DeviceEventManagerModule;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -325,9 +323,7 @@ else if(resolved == null) {
boolean error = false;

if (encoding.equalsIgnoreCase("utf8")) {
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
while ((cursor = fs.read(buffer)) != -1) {
encoder.encode(ByteBuffer.wrap(buffer).asCharBuffer());
String chunk = new String(buffer, 0, cursor);
emitStreamEvent(streamId, "data", chunk);
if(tick > 0)
Expand Down
24 changes: 2 additions & 22 deletions android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -503,28 +499,12 @@ private void done(Response resp) {
// encoding will somehow break the UTF8 string format, to encode UTF8
// string correctly, we should do URL encoding before BASE64.
byte[] b = resp.body().bytes();
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
if(responseFormat == ResponseFormat.BASE64) {
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
return;
}
try {
encoder.encode(ByteBuffer.wrap(b).asCharBuffer());
// if the data contains invalid characters the following lines will be
// skipped.
String utf8 = new String(b);
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_UTF8, utf8);
}
// This usually mean the data is contains invalid unicode characters, it's
// binary data
catch(CharacterCodingException ignored) {
if(responseFormat == ResponseFormat.UTF8) {
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_UTF8, "");
}
else {
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
}
}
String utf8 = new String(b);
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_UTF8, utf8);
}
} catch (IOException e) {
callback.invoke("RNFetchBlob failed to encode response data to BASE64 string.", null);
Expand Down