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
         RNFetchBlobFS.createFile(path, content, encode, callback);
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
     @ReactMethod
67
     @ReactMethod
58
     public void unlink(String path, Callback callback) {
68
     public void unlink(String path, Callback callback) {
59
         RNFetchBlobFS.unlink(path, callback);
69
         RNFetchBlobFS.unlink(path, callback);

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

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