Bläddra i källkod

Revert "Merge pull request #353 from thinkproductivity/mjmasn-patch-1"

This reverts commit 6bbebd3429, reversing
changes made to 90ce0c401a.
Travis Nuttall 4 år sedan
förälder
incheckning
00c12e32e1

+ 4
- 0
android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java Visa fil

21
 import com.facebook.react.modules.core.DeviceEventManagerModule;
21
 import com.facebook.react.modules.core.DeviceEventManagerModule;
22
 
22
 
23
 import java.io.*;
23
 import java.io.*;
24
+import java.nio.ByteBuffer;
24
 import java.nio.charset.Charset;
25
 import java.nio.charset.Charset;
26
+import java.nio.charset.CharsetEncoder;
25
 import java.security.MessageDigest;
27
 import java.security.MessageDigest;
26
 import java.util.ArrayList;
28
 import java.util.ArrayList;
27
 import java.util.HashMap;
29
 import java.util.HashMap;
339
             boolean error = false;
341
             boolean error = false;
340
 
342
 
341
             if (encoding.equalsIgnoreCase("utf8")) {
343
             if (encoding.equalsIgnoreCase("utf8")) {
344
+                CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
342
                 while ((cursor = fs.read(buffer)) != -1) {
345
                 while ((cursor = fs.read(buffer)) != -1) {
346
+                    encoder.encode(ByteBuffer.wrap(buffer).asCharBuffer());
343
                     String chunk = new String(buffer, 0, cursor);
347
                     String chunk = new String(buffer, 0, cursor);
344
                     emitStreamEvent(streamId, "data", chunk);
348
                     emitStreamEvent(streamId, "data", chunk);
345
                     if(tick > 0)
349
                     if(tick > 0)

+ 22
- 2
android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java Visa fil

36
 import java.net.SocketException;
36
 import java.net.SocketException;
37
 import java.net.SocketTimeoutException;
37
 import java.net.SocketTimeoutException;
38
 import java.net.URL;
38
 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;
39
 import java.security.KeyStore;
43
 import java.security.KeyStore;
40
 import java.util.ArrayList;
44
 import java.util.ArrayList;
41
 import java.util.Arrays;
45
 import java.util.Arrays;
498
                         // encoding will somehow break the UTF8 string format, to encode UTF8
502
                         // encoding will somehow break the UTF8 string format, to encode UTF8
499
                         // string correctly, we should do URL encoding before BASE64.
503
                         // string correctly, we should do URL encoding before BASE64.
500
                         byte[] b = resp.body().bytes();
504
                         byte[] b = resp.body().bytes();
505
+                        CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
501
                         if(responseFormat == ResponseFormat.BASE64) {
506
                         if(responseFormat == ResponseFormat.BASE64) {
502
                             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));
503
                             return;
508
                             return;
504
                         }
509
                         }
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
+                        }
507
                     }
527
                     }
508
                 } catch (IOException e) {
528
                 } catch (IOException e) {
509
                     callback.invoke("RNFetchBlob failed to encode response data to BASE64 string.", null);
529
                     callback.invoke("RNFetchBlob failed to encode response data to BASE64 string.", null);