Browse Source

Add implementation of Android ASCII reade and write

Ben Hsieh 8 years ago
parent
commit
90176637c5

+ 10
- 0
src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java View File

@@ -54,6 +54,16 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
54 54
         RNFetchBlobFS.createFile(path, content, encode, callback);
55 55
     }
56 56
 
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
+
57 67
     @ReactMethod
58 68
     public void unlink(String path, Callback callback) {
59 69
         RNFetchBlobFS.unlink(path, callback);

+ 54
- 2
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

@@ -103,7 +103,14 @@ public class RNFetchBlobFS {
103 103
                         }
104 104
                     } else if (encoding.equalsIgnoreCase("ascii")) {
105 105
                         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 + "]";
107 114
                             emitStreamEvent(eventName, "data", chunk);
108 115
                         }
109 116
                     } else if (encoding.equalsIgnoreCase("base64")) {
@@ -127,7 +134,7 @@ public class RNFetchBlobFS {
127 134
                     if(!error)
128 135
                         emitStreamEvent(eventName, "end", "");
129 136
                     fs.close();
130
-
137
+                    buffer = null;
131 138
 
132 139
                 } catch (Exception err) {
133 140
                     emitStreamEvent(eventName, "error", err.getLocalizedMessage());
@@ -180,6 +187,30 @@ public class RNFetchBlobFS {
180 187
         try {
181 188
             stream.write(chunk);
182 189
             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;
183 214
         } catch (Exception e) {
184 215
             callback.invoke(e.getLocalizedMessage());
185 216
         }
@@ -343,6 +374,27 @@ public class RNFetchBlobFS {
343 374
         }
344 375
     }
345 376
 
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
+
346 398
     static void removeSession(ReadableArray paths, Callback callback) {
347 399
 
348 400
         AsyncTask<ReadableArray, Integer, Integer> task = new AsyncTask<ReadableArray, Integer, Integer>() {