Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit 3661446

Browse files
committed
Change Android fs.readStream implementation
Move readStream to thread pool
1 parent a3be3da commit 3661446

File tree

2 files changed

+60
-64
lines changed

2 files changed

+60
-64
lines changed

src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,17 @@ public void run() {
205205
/**
206206
* @param path Stream file path
207207
* @param encoding Stream encoding, should be one of `base64`, `ascii`, and `utf8`
208-
* @param bufferSize Stream buffer size, default to 1024 or 1026(base64).
208+
* @param bufferSize Stream buffer size, default to 4096 or 4095(base64).
209209
*/
210-
public void readStream(String path, String encoding, int bufferSize, String streamId) {
211-
RNFetchBlobFS fs = new RNFetchBlobFS(this.getReactApplicationContext());
212-
fs.readStream(path, encoding, bufferSize, streamId);
210+
public void readStream(final String path, final String encoding, final int bufferSize, final String streamId) {
211+
final ReactApplicationContext ctx = this.getReactApplicationContext();
212+
threadPool.execute(new Runnable() {
213+
@Override
214+
public void run() {
215+
RNFetchBlobFS fs = new RNFetchBlobFS(ctx);
216+
fs.readStream(path, encoding, bufferSize, streamId);
217+
}
218+
});
213219
}
214220

215221
@ReactMethod

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

Lines changed: 50 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -231,74 +231,64 @@ static public String getTmpPath(ReactApplicationContext ctx, String taskId) {
231231
*/
232232
public void readStream(String path, String encoding, int bufferSize, final String streamId) {
233233
path = normalizePath(path);
234-
AsyncTask<String, Integer, Integer> task = new AsyncTask<String, Integer, Integer>() {
235-
@Override
236-
protected Integer doInBackground(String ... args) {
237-
String path = args[0];
238-
String encoding = args[1];
239-
int bufferSize = Integer.parseInt(args[2]);
240-
try {
234+
try {
241235

242-
int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096;
243-
if(bufferSize > 0)
244-
chunkSize = bufferSize;
236+
int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096;
237+
if(bufferSize > 0)
238+
chunkSize = bufferSize;
245239

246-
InputStream fs;
247-
if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
248-
fs = RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
249-
}
250-
else {
251-
fs = new FileInputStream(new File(path));
252-
}
240+
InputStream fs;
241+
if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
242+
fs = RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
243+
}
244+
else {
245+
fs = new FileInputStream(new File(path));
246+
}
253247

254-
byte[] buffer = new byte[chunkSize];
255-
int cursor = 0;
256-
boolean error = false;
248+
byte[] buffer = new byte[chunkSize];
249+
int cursor = 0;
250+
boolean error = false;
257251

258-
if (encoding.equalsIgnoreCase("utf8")) {
259-
while ((cursor = fs.read(buffer)) != -1) {
260-
String chunk = new String(buffer, 0, cursor, "UTF-8");
261-
emitStreamEvent(streamId, "data", chunk);
262-
}
263-
} else if (encoding.equalsIgnoreCase("ascii")) {
264-
while ((cursor = fs.read(buffer)) != -1) {
265-
WritableArray chunk = Arguments.createArray();
266-
for(int i =0;i<cursor;i++)
267-
{
268-
chunk.pushInt((int)buffer[i]);
269-
}
270-
emitStreamEvent(streamId, "data", chunk);
271-
}
272-
} else if (encoding.equalsIgnoreCase("base64")) {
273-
while ((cursor = fs.read(buffer)) != -1) {
274-
if(cursor < chunkSize) {
275-
byte [] copy = new byte[cursor];
276-
for(int i =0;i<cursor;i++) {
277-
copy[i] = buffer[i];
278-
}
279-
emitStreamEvent(streamId, "data", Base64.encodeToString(copy, Base64.NO_WRAP));
280-
}
281-
else
282-
emitStreamEvent(streamId, "data", Base64.encodeToString(buffer, Base64.NO_WRAP));
252+
if (encoding.equalsIgnoreCase("utf8")) {
253+
while ((cursor = fs.read(buffer)) != -1) {
254+
String chunk = new String(buffer, 0, cursor, "UTF-8");
255+
emitStreamEvent(streamId, "data", chunk);
256+
}
257+
} else if (encoding.equalsIgnoreCase("ascii")) {
258+
while ((cursor = fs.read(buffer)) != -1) {
259+
WritableArray chunk = Arguments.createArray();
260+
for(int i =0;i<cursor;i++)
261+
{
262+
chunk.pushInt((int)buffer[i]);
263+
}
264+
emitStreamEvent(streamId, "data", chunk);
265+
}
266+
} else if (encoding.equalsIgnoreCase("base64")) {
267+
while ((cursor = fs.read(buffer)) != -1) {
268+
if(cursor < chunkSize) {
269+
byte [] copy = new byte[cursor];
270+
for(int i =0;i<cursor;i++) {
271+
copy[i] = buffer[i];
283272
}
284-
} else {
285-
String msg = "unrecognized encoding `" + encoding + "`";
286-
emitStreamEvent(streamId, "error", msg);
287-
error = true;
273+
emitStreamEvent(streamId, "data", Base64.encodeToString(copy, Base64.NO_WRAP));
288274
}
289-
290-
if(!error)
291-
emitStreamEvent(streamId, "end", "");
292-
fs.close();
293-
buffer = null;
294-
295-
} catch (Exception err) {
296-
emitStreamEvent(streamId, "error", err.getLocalizedMessage());
275+
else
276+
emitStreamEvent(streamId, "data", Base64.encodeToString(buffer, Base64.NO_WRAP));
297277
}
298-
return null;
278+
} else {
279+
String msg = "unrecognized encoding `" + encoding + "`";
280+
emitStreamEvent(streamId, "error", msg);
281+
error = true;
299282
}
300-
};
301-
task.execute(path, encoding, String.valueOf(bufferSize));
283+
284+
if(!error)
285+
emitStreamEvent(streamId, "end", "");
286+
fs.close();
287+
buffer = null;
288+
289+
} catch (Exception err) {
290+
emitStreamEvent(streamId, "error", err.getLocalizedMessage());
291+
}
302292
}
303293

304294
/**

0 commit comments

Comments
 (0)