|
@@ -9,6 +9,7 @@ import com.facebook.react.bridge.WritableMap;
|
9
|
9
|
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
10
|
10
|
import com.loopj.android.http.Base64;
|
11
|
11
|
|
|
12
|
+import java.io.File;
|
12
|
13
|
import java.io.FileInputStream;
|
13
|
14
|
|
14
|
15
|
import cz.msebera.android.httpclient.util.EncodingUtils;
|
|
@@ -31,50 +32,63 @@ public class RNFetchBlobFS {
|
31
|
32
|
this.emitter = ctx.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
|
32
|
33
|
}
|
33
|
34
|
|
34
|
|
- // TODO : make it an AsyncTask
|
35
|
|
- public void readStream(final String path, String encoding) {
|
|
35
|
+ public void readStream( String path, String encoding, String bufferSize) {
|
36
|
36
|
AsyncTask<String, Integer, Integer> task = new AsyncTask<String, Integer, Integer>() {
|
37
|
37
|
@Override
|
38
|
38
|
protected Integer doInBackground(String ... args) {
|
39
|
39
|
String path = args[0];
|
40
|
40
|
String encoding = args[1];
|
|
41
|
+ int bufferSize = Integer.parseInt(args[2]);
|
41
|
42
|
String eventName = "RNFetchBlobStream+" + path;
|
42
|
43
|
try {
|
43
|
|
- FileInputStream fs = mCtx.openFileInput(mCtx.getFilesDir() + "/"+ path);
|
44
|
|
- byte[] buffer = new byte[1024];
|
|
44
|
+
|
|
45
|
+ int chunkSize = encoding.equalsIgnoreCase("base64") ? 1026 : 1024;
|
|
46
|
+ if(bufferSize > 0)
|
|
47
|
+ chunkSize = bufferSize;
|
|
48
|
+ FileInputStream fs = new FileInputStream(new File(path));
|
|
49
|
+ byte[] buffer = new byte[chunkSize];
|
45
|
50
|
int cursor = 0;
|
46
|
51
|
boolean error = false;
|
47
|
52
|
|
48
|
|
- if (encoding.toLowerCase() == "utf8") {
|
|
53
|
+ if (encoding.equalsIgnoreCase("utf8")) {
|
49
|
54
|
while ((cursor = fs.read(buffer)) != -1) {
|
50
|
55
|
String chunk = new String(buffer, 0, cursor, "UTF-8");
|
51
|
|
- emitFSData(eventName, "data", chunk);
|
|
56
|
+ emitStreamEvent(eventName, "data", chunk);
|
52
|
57
|
}
|
53
|
|
- } else if (encoding.toLowerCase() == "ascii") {
|
|
58
|
+ } else if (encoding.equalsIgnoreCase("ascii")) {
|
54
|
59
|
while ((cursor = fs.read(buffer)) != -1) {
|
55
|
60
|
String chunk = EncodingUtils.getAsciiString(buffer, 0, cursor);
|
56
|
|
- emitFSData(eventName, "data", chunk);
|
|
61
|
+ emitStreamEvent(eventName, "data", chunk);
|
57
|
62
|
}
|
58
|
|
- } else if (encoding.toLowerCase() == "base64") {
|
|
63
|
+ } else if (encoding.equalsIgnoreCase("base64")) {
|
59
|
64
|
while ((cursor = fs.read(buffer)) != -1) {
|
60
|
|
- emitFSData(eventName, "data", Base64.encodeToString(buffer, Base64.NO_WRAP));
|
|
65
|
+ emitStreamEvent(eventName, "data", Base64.encodeToString(buffer, Base64.NO_WRAP));
|
61
|
66
|
}
|
62
|
67
|
} else {
|
63
|
68
|
String msg = "unrecognized encoding `" + encoding + "`";
|
64
|
|
- emitFSData(eventName, "error", msg);
|
|
69
|
+ emitStreamEvent(eventName, "error", msg);
|
65
|
70
|
error = true;
|
66
|
71
|
}
|
67
|
72
|
|
68
|
73
|
if(!error)
|
69
|
|
- emitFSData(eventName, "end", "");
|
|
74
|
+ emitStreamEvent(eventName, "end", "");
|
|
75
|
+ fs.close();
|
|
76
|
+
|
70
|
77
|
|
71
|
78
|
} catch (Exception err) {
|
72
|
|
- emitFSData(eventName, "error", err.getLocalizedMessage());
|
|
79
|
+ emitStreamEvent(eventName, "error", err.getLocalizedMessage());
|
73
|
80
|
}
|
74
|
81
|
return null;
|
75
|
82
|
}
|
76
|
83
|
};
|
77
|
|
- task.execute(path, encoding);
|
|
84
|
+ task.execute(path, encoding, bufferSize);
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ void emitStreamEvent(String streamName, String event, String data) {
|
|
88
|
+ WritableMap eventData = Arguments.createMap();
|
|
89
|
+ eventData.putString("event", event);
|
|
90
|
+ eventData.putString("detail", data);
|
|
91
|
+ this.emitter.emit(streamName, eventData);
|
78
|
92
|
}
|
79
|
93
|
|
80
|
94
|
void emitFSData(String taskId, String event, String data) {
|