Browse Source

Fix out-of-bound error when using fs.readStream with UTF8 encoding on Android #321

Ben Hsieh 7 years ago
parent
commit
61d7ab098a
1 changed files with 4 additions and 2 deletions
  1. 4
    2
      android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

+ 4
- 2
android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

249
                 while ((cursor = fs.read(buffer)) != -1) {
249
                 while ((cursor = fs.read(buffer)) != -1) {
250
                     encoder.encode(ByteBuffer.wrap(buffer).asCharBuffer());
250
                     encoder.encode(ByteBuffer.wrap(buffer).asCharBuffer());
251
                     String chunk = new String(buffer);
251
                     String chunk = new String(buffer);
252
-                    if(cursor != bufferSize)
252
+                    if(cursor != bufferSize) {
253
                         chunk = chunk.substring(0, cursor);
253
                         chunk = chunk.substring(0, cursor);
254
+                    }
254
                     emitStreamEvent(streamId, "data", chunk);
255
                     emitStreamEvent(streamId, "data", chunk);
255
                     if(tick > 0)
256
                     if(tick > 0)
256
                         SystemClock.sleep(tick);
257
                         SystemClock.sleep(tick);
292
             buffer = null;
293
             buffer = null;
293
 
294
 
294
         } catch (Exception err) {
295
         } catch (Exception err) {
295
-            emitStreamEvent(streamId, "error", "Failed to convert data to "+encoding+" encoded string, this might due to the source data is not able to convert using this encoding.");
296
+            emitStreamEvent(streamId, "warn", "Failed to convert data to "+encoding+" encoded string, this might due to the source data is not able to convert using this encoding.");
297
+            err.printStackTrace();
296
         }
298
         }
297
     }
299
     }
298
 
300