Skip to content

Commit 7d0d9d1

Browse files
author
Ron Radtke
committed
Update:
* Fixes import cycles * Fixes problems for streaming files in combination with utf-8 encoding Fixes wkh237#58 Fixes wkh237#51 Merge remote-tracking branch 'origin/develop' into master
2 parents 45dd327 + 602d8eb commit 7d0d9d1

34 files changed

+9467
-2352
lines changed

.eslintrc

-64
This file was deleted.

.eslintrc.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module.exports = {
2+
root: true,
3+
rules: {
4+
'prettier/prettier': 0,
5+
"eqeqeq": 2,
6+
"comma-dangle": 0,
7+
"curly": 0,
8+
"no-console": 1,
9+
"no-debugger": 1,
10+
"no-extra-semi": 2,
11+
"no-extra-parens": 1,
12+
"no-extra-boolean-cast": 1,
13+
"no-cond-assign": 2,
14+
"no-irregular-whitespace": 2,
15+
"no-undef": 0,
16+
"no-unused-vars": 0,
17+
"semi": 2,
18+
"semi-spacing": 2,
19+
"valid-jsdoc": [
20+
1,
21+
{
22+
"requireReturn": false,
23+
"requireParamDescription": false,
24+
"requireReturnDescription": false
25+
}
26+
],
27+
"radix": 0
28+
},
29+
"parser": "babel-eslint"
30+
};

android.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// Use of this source code is governed by a MIT-style license that can be
33
// found in the LICENSE file.
44

5-
import { NativeModules, Platform } from 'react-native'
5+
import { NativeModules, Platform } from 'react-native';
66

7-
const ReactNativeBlobUtil = NativeModules.ReactNativeBlobUtil
7+
const ReactNativeBlobUtil = NativeModules.ReactNativeBlobUtil;
88

99
/**
1010
* Send an intent to open the file.
@@ -16,37 +16,37 @@ const ReactNativeBlobUtil = NativeModules.ReactNativeBlobUtil
1616
function actionViewIntent(path, mime, chooserTitle) {
1717
if(typeof chooserTitle === 'undefined') chooserTitle = null;
1818
if(Platform.OS === 'android')
19-
return ReactNativeBlobUtil.actionViewIntent(path, mime, chooserTitle)
19+
return ReactNativeBlobUtil.actionViewIntent(path, mime, chooserTitle);
2020
else
21-
return Promise.reject('ReactNativeBlobUtil.android.actionViewIntent only supports Android.')
21+
return Promise.reject('ReactNativeBlobUtil.android.actionViewIntent only supports Android.');
2222
}
2323

2424
function getContentIntent(mime) {
2525
if(Platform.OS === 'android')
26-
return ReactNativeBlobUtil.getContentIntent(mime)
26+
return ReactNativeBlobUtil.getContentIntent(mime);
2727
else
28-
return Promise.reject('ReactNativeBlobUtil.android.getContentIntent only supports Android.')
28+
return Promise.reject('ReactNativeBlobUtil.android.getContentIntent only supports Android.');
2929
}
3030

3131
function addCompleteDownload(config) {
3232
if(Platform.OS === 'android')
33-
return ReactNativeBlobUtil.addCompleteDownload(config)
33+
return ReactNativeBlobUtil.addCompleteDownload(config);
3434
else
35-
return Promise.reject('ReactNativeBlobUtil.android.addCompleteDownload only supports Android.')
35+
return Promise.reject('ReactNativeBlobUtil.android.addCompleteDownload only supports Android.');
3636
}
3737

3838
function getSDCardDir() {
3939
if(Platform.OS === 'android')
40-
return ReactNativeBlobUtil.getSDCardDir()
40+
return ReactNativeBlobUtil.getSDCardDir();
4141
else
42-
return Promise.reject('ReactNativeBlobUtil.android.getSDCardDir only supports Android.')
42+
return Promise.reject('ReactNativeBlobUtil.android.getSDCardDir only supports Android.');
4343
}
4444

4545
function getSDCardApplicationDir() {
4646
if(Platform.OS === 'android')
47-
return ReactNativeBlobUtil.getSDCardApplicationDir()
47+
return ReactNativeBlobUtil.getSDCardApplicationDir();
4848
else
49-
return Promise.reject('ReactNativeBlobUtil.android.getSDCardApplicationDir only supports Android.')
49+
return Promise.reject('ReactNativeBlobUtil.android.getSDCardApplicationDir only supports Android.');
5050
}
5151

5252

@@ -56,4 +56,4 @@ export default {
5656
addCompleteDownload,
5757
getSDCardDir,
5858
getSDCardApplicationDir,
59-
}
59+
};

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java

+14-10
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;
@@ -360,20 +358,25 @@ else if (resolved == null) {
360358
fs = new FileInputStream(new File(path));
361359
}
362360

363-
byte[] buffer = new byte[chunkSize];
364361
int cursor = 0;
365362
boolean error = false;
366363

367364
if (encoding.equalsIgnoreCase("utf8")) {
368-
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
369-
while ((cursor = fs.read(buffer)) != -1) {
370-
encoder.encode(ByteBuffer.wrap(buffer).asCharBuffer());
371-
String chunk = new String(buffer, 0, cursor);
365+
InputStreamReader isr = new InputStreamReader(fs, Charset.forName("UTF-8"));
366+
BufferedReader reader = new BufferedReader(isr, chunkSize);
367+
char[] buffer = new char[chunkSize];
368+
// read chunks of the string
369+
while (reader.read(buffer, 0, chunkSize) != -1) {
370+
String chunk = new String(buffer);
372371
emitStreamEvent(streamId, "data", chunk);
373372
if (tick > 0)
374373
SystemClock.sleep(tick);
375374
}
375+
376+
reader.close();
377+
isr.close();
376378
} else if (encoding.equalsIgnoreCase("ascii")) {
379+
byte[] buffer = new byte[chunkSize];
377380
while ((cursor = fs.read(buffer)) != -1) {
378381
WritableArray chunk = Arguments.createArray();
379382
for (int i = 0; i < cursor; i++) {
@@ -384,6 +387,7 @@ else if (resolved == null) {
384387
SystemClock.sleep(tick);
385388
}
386389
} else if (encoding.equalsIgnoreCase("base64")) {
390+
byte[] buffer = new byte[chunkSize];
387391
while ((cursor = fs.read(buffer)) != -1) {
388392
if (cursor < chunkSize) {
389393
byte[] copy = new byte[cursor];
@@ -407,7 +411,7 @@ else if (resolved == null) {
407411
if (!error)
408412
emitStreamEvent(streamId, "end", "");
409413
fs.close();
410-
buffer = null;
414+
411415
} catch (FileNotFoundException err) {
412416
emitStreamEvent(
413417
streamId,
@@ -710,8 +714,8 @@ static void exists(String path, Callback callback) {
710714
/**
711715
* List content of folder
712716
*
713-
* @param path Target folder
714-
* @param callback JS context callback
717+
* @param path Target folder
718+
* @param promise JS context promise
715719
*/
716720
static void ls(String path, Promise promise) {
717721
try {

0 commit comments

Comments
 (0)