Skip to content

Commit

Permalink
Update:
Browse files Browse the repository at this point in the history
* Fixes import cycles
* Fixes problems for streaming files in combination with utf-8 encoding

Fixes RonRadtke#58
Fixes RonRadtke#51

Merge remote-tracking branch 'origin/develop' into master
  • Loading branch information
Ron Radtke committed Aug 29, 2021
2 parents 45dd327 + 602d8eb commit 7d0d9d1
Show file tree
Hide file tree
Showing 34 changed files with 9,467 additions and 2,352 deletions.
64 changes: 0 additions & 64 deletions .eslintrc

This file was deleted.

30 changes: 30 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module.exports = {
root: true,
rules: {
'prettier/prettier': 0,
"eqeqeq": 2,
"comma-dangle": 0,
"curly": 0,
"no-console": 1,
"no-debugger": 1,
"no-extra-semi": 2,
"no-extra-parens": 1,
"no-extra-boolean-cast": 1,
"no-cond-assign": 2,
"no-irregular-whitespace": 2,
"no-undef": 0,
"no-unused-vars": 0,
"semi": 2,
"semi-spacing": 2,
"valid-jsdoc": [
1,
{
"requireReturn": false,
"requireParamDescription": false,
"requireReturnDescription": false
}
],
"radix": 0
},
"parser": "babel-eslint"
};
26 changes: 13 additions & 13 deletions android.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.

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

const ReactNativeBlobUtil = NativeModules.ReactNativeBlobUtil
const ReactNativeBlobUtil = NativeModules.ReactNativeBlobUtil;

/**
* Send an intent to open the file.
Expand All @@ -16,37 +16,37 @@ const ReactNativeBlobUtil = NativeModules.ReactNativeBlobUtil
function actionViewIntent(path, mime, chooserTitle) {
if(typeof chooserTitle === 'undefined') chooserTitle = null;
if(Platform.OS === 'android')
return ReactNativeBlobUtil.actionViewIntent(path, mime, chooserTitle)
return ReactNativeBlobUtil.actionViewIntent(path, mime, chooserTitle);
else
return Promise.reject('ReactNativeBlobUtil.android.actionViewIntent only supports Android.')
return Promise.reject('ReactNativeBlobUtil.android.actionViewIntent only supports Android.');
}

function getContentIntent(mime) {
if(Platform.OS === 'android')
return ReactNativeBlobUtil.getContentIntent(mime)
return ReactNativeBlobUtil.getContentIntent(mime);
else
return Promise.reject('ReactNativeBlobUtil.android.getContentIntent only supports Android.')
return Promise.reject('ReactNativeBlobUtil.android.getContentIntent only supports Android.');
}

function addCompleteDownload(config) {
if(Platform.OS === 'android')
return ReactNativeBlobUtil.addCompleteDownload(config)
return ReactNativeBlobUtil.addCompleteDownload(config);
else
return Promise.reject('ReactNativeBlobUtil.android.addCompleteDownload only supports Android.')
return Promise.reject('ReactNativeBlobUtil.android.addCompleteDownload only supports Android.');
}

function getSDCardDir() {
if(Platform.OS === 'android')
return ReactNativeBlobUtil.getSDCardDir()
return ReactNativeBlobUtil.getSDCardDir();
else
return Promise.reject('ReactNativeBlobUtil.android.getSDCardDir only supports Android.')
return Promise.reject('ReactNativeBlobUtil.android.getSDCardDir only supports Android.');
}

function getSDCardApplicationDir() {
if(Platform.OS === 'android')
return ReactNativeBlobUtil.getSDCardApplicationDir()
return ReactNativeBlobUtil.getSDCardApplicationDir();
else
return Promise.reject('ReactNativeBlobUtil.android.getSDCardApplicationDir only supports Android.')
return Promise.reject('ReactNativeBlobUtil.android.getSDCardApplicationDir only supports Android.');
}


Expand All @@ -56,4 +56,4 @@ export default {
addCompleteDownload,
getSDCardDir,
getSDCardApplicationDir,
}
};
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 @@ -360,20 +358,25 @@ else if (resolved == null) {
fs = new FileInputStream(new File(path));
}

byte[] buffer = new byte[chunkSize];
int cursor = 0;
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);
InputStreamReader isr = new InputStreamReader(fs, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(isr, chunkSize);
char[] buffer = new char[chunkSize];
// read chunks of the string
while (reader.read(buffer, 0, chunkSize) != -1) {
String chunk = new String(buffer);
emitStreamEvent(streamId, "data", chunk);
if (tick > 0)
SystemClock.sleep(tick);
}

reader.close();
isr.close();
} else if (encoding.equalsIgnoreCase("ascii")) {
byte[] buffer = new byte[chunkSize];
while ((cursor = fs.read(buffer)) != -1) {
WritableArray chunk = Arguments.createArray();
for (int i = 0; i < cursor; i++) {
Expand All @@ -384,6 +387,7 @@ else if (resolved == null) {
SystemClock.sleep(tick);
}
} else if (encoding.equalsIgnoreCase("base64")) {
byte[] buffer = new byte[chunkSize];
while ((cursor = fs.read(buffer)) != -1) {
if (cursor < chunkSize) {
byte[] copy = new byte[cursor];
Expand All @@ -407,7 +411,7 @@ else if (resolved == null) {
if (!error)
emitStreamEvent(streamId, "end", "");
fs.close();
buffer = null;

} catch (FileNotFoundException err) {
emitStreamEvent(
streamId,
Expand Down Expand Up @@ -710,8 +714,8 @@ static void exists(String path, Callback callback) {
/**
* List content of folder
*
* @param path Target folder
* @param callback JS context callback
* @param path Target folder
* @param promise JS context promise
*/
static void ls(String path, Promise promise) {
try {
Expand Down
Loading

0 comments on commit 7d0d9d1

Please sign in to comment.