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

Commit 9017663

Browse files
committed
Add implementation of Android ASCII reade and write
1 parent 3591b9f commit 9017663

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ public void createFile(String path, String content, String encode, Callback call
5454
RNFetchBlobFS.createFile(path, content, encode, callback);
5555
}
5656

57+
@ReactMethod
58+
public void createFileASCII(String path, ReadableArray dataArray, Callback callback) {
59+
RNFetchBlobFS.createFileASCII(path, dataArray, callback);
60+
}
61+
62+
@ReactMethod
63+
public void writeArrayChunk(String streamId, ReadableArray dataArray, Callback callback) {
64+
RNFetchBlobFS.writeArrayChunk(streamId, dataArray, callback);
65+
}
66+
5767
@ReactMethod
5868
public void unlink(String path, Callback callback) {
5969
RNFetchBlobFS.unlink(path, callback);

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

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,14 @@ protected Integer doInBackground(String ... args) {
103103
}
104104
} else if (encoding.equalsIgnoreCase("ascii")) {
105105
while ((cursor = fs.read(buffer)) != -1) {
106-
String chunk = EncodingUtils.getAsciiString(buffer, 0, cursor);
106+
String chunk = "[";
107+
for(int i =0;i<cursor;i++)
108+
{
109+
chunk += (int)buffer[i];
110+
if(i+1 < cursor)
111+
chunk += ",";
112+
}
113+
chunk = chunk + "]";
107114
emitStreamEvent(eventName, "data", chunk);
108115
}
109116
} else if (encoding.equalsIgnoreCase("base64")) {
@@ -127,7 +134,7 @@ protected Integer doInBackground(String ... args) {
127134
if(!error)
128135
emitStreamEvent(eventName, "end", "");
129136
fs.close();
130-
137+
buffer = null;
131138

132139
} catch (Exception err) {
133140
emitStreamEvent(eventName, "error", err.getLocalizedMessage());
@@ -180,6 +187,30 @@ static void writeChunk(String streamId, String data, Callback callback) {
180187
try {
181188
stream.write(chunk);
182189
callback.invoke();
190+
chunk = null;
191+
} catch (Exception e) {
192+
callback.invoke(e.getLocalizedMessage());
193+
}
194+
}
195+
196+
/**
197+
* Write data using ascii array
198+
* @param streamId File stream ID
199+
* @param data Data chunk in ascii array format
200+
* @param callback JS context callback
201+
*/
202+
static void writeArrayChunk(String streamId, ReadableArray data, Callback callback) {
203+
204+
RNFetchBlobFS fs = fileStreams.get(streamId);
205+
OutputStream stream = fs.writeStreamInstance;
206+
byte [] chunk = new byte[data.size()];
207+
for(int i =0; i< data.size();i++) {
208+
chunk[i] = (byte) data.getInt(i);
209+
}
210+
try {
211+
stream.write(chunk);
212+
callback.invoke();
213+
chunk = null;
183214
} catch (Exception e) {
184215
callback.invoke(e.getLocalizedMessage());
185216
}
@@ -343,6 +374,27 @@ static void createFile(String path, String data, String encoding, Callback callb
343374
}
344375
}
345376

377+
static void createFileASCII(String path, ReadableArray data, Callback callback) {
378+
try {
379+
File dest = new File(path);
380+
boolean created = dest.createNewFile();
381+
if(!created) {
382+
callback.invoke("failed to create file at path `" + path + "` for its parent path may not exists");
383+
return;
384+
}
385+
OutputStream ostream = new FileOutputStream(dest);
386+
byte [] chunk = new byte[data.size()];
387+
for(int i =0; i<data.size();i++) {
388+
chunk[i] = (byte) data.getInt(i);
389+
}
390+
ostream.write(chunk);
391+
chunk = null;
392+
callback.invoke(null, path);
393+
} catch(Exception err) {
394+
callback.invoke(err.getLocalizedMessage());
395+
}
396+
}
397+
346398
static void removeSession(ReadableArray paths, Callback callback) {
347399

348400
AsyncTask<ReadableArray, Integer, Integer> task = new AsyncTask<ReadableArray, Integer, Integer>() {

0 commit comments

Comments
 (0)