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

Commit 5378807

Browse files
committed
Add Android fs.writeChunk and code refactor #124
1 parent e2dae6e commit 5378807

File tree

3 files changed

+106
-30
lines changed

3 files changed

+106
-30
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.content.Intent;
55
import android.net.Uri;
66
import android.support.annotation.Nullable;
7-
import android.util.Log;
87

98
import com.RNFetchBlob.Utils.EncodingResolver;
109
import com.RNFetchBlob.Utils.RNFBCookieJar;
@@ -17,12 +16,10 @@
1716
import com.facebook.react.bridge.ReactMethod;
1817
import com.facebook.react.bridge.ReadableArray;
1918
import com.facebook.react.bridge.ReadableMap;
20-
import com.facebook.react.bridge.WritableArray;
2119
import com.facebook.react.bridge.WritableMap;
2220

2321
import java.util.HashMap;
2422
import java.util.Map;
25-
import java.util.UUID;
2623
import java.util.concurrent.LinkedBlockingQueue;
2724
import java.util.concurrent.ThreadPoolExecutor;
2825
import java.util.concurrent.TimeUnit;
@@ -175,7 +172,7 @@ public void writeStream(String path, String encode, boolean append, Callback cal
175172

176173
@ReactMethod
177174
public void writeChunk(String streamId, String data, Callback callback) {
178-
RNFetchBlobFS.writeChunk(streamId, data, callback);
175+
RNFetchBlobFS.writeStreamChunk(streamId, data, callback);
179176
}
180177

181178
@ReactMethod
@@ -348,6 +345,22 @@ public void run() {
348345

349346
}
350347

348+
@ReactMethod
349+
public void writeChunk(final String path, final String encoding, final String data, final int offset, final int length, final Promise promise) {
350+
fsThreadPool.execute(new Runnable() {
351+
@Override
352+
public void run() {
353+
try {
354+
RNFetchBlobFS.writeChunk(path, encoding, data, offset);
355+
promise.resolve(null);
356+
} catch (Exception e) {
357+
e.printStackTrace();
358+
promise.reject(e.getMessage(), e.getMessage()) ;
359+
}
360+
}
361+
});
362+
}
363+
351364

352365
@ReactMethod
353366
public void enableUploadProgressReport(String taskId, int interval, int count) {

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

Lines changed: 71 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,14 @@ static public void writeFile(String path, String encoding, String data, final bo
7979
FileOutputStream fout = new FileOutputStream(f, append);
8080
// write data from a file
8181
if(encoding.equalsIgnoreCase(RNFetchBlobConst.DATA_ENCODE_URI)) {
82-
data = normalizePath(data);
83-
File src = new File(data);
84-
if(!src.exists()) {
85-
promise.reject("RNfetchBlob writeFileError", "source file : " + data + "not exists");
86-
fout.close();
87-
return ;
82+
try {
83+
written = writeFileToFileWithOffset(data, path, 0, append);
84+
promise.resolve(written);
8885
}
89-
FileInputStream fin = new FileInputStream(src);
90-
byte [] buffer = new byte [10240];
91-
int read;
92-
written = 0;
93-
while((read = fin.read(buffer)) > 0) {
94-
fout.write(buffer, 0, read);
95-
written += read;
86+
catch(Exception ex) {
87+
ex.printStackTrace();
88+
promise.reject("RNfetchBlob writeFileError", ex.getMessage());
9689
}
97-
fin.close();
9890
}
9991
else {
10092
byte[] bytes = stringToBytes(data, encoding);
@@ -108,6 +100,22 @@ static public void writeFile(String path, String encoding, String data, final bo
108100
}
109101
}
110102

103+
private static int writeFileToFileWithOffset(String source, String dest, int offset, boolean append) throws IOException {
104+
105+
source = normalizePath(source);
106+
FileOutputStream fout = new FileOutputStream(dest, append);
107+
FileInputStream fin = new FileInputStream(source);
108+
byte [] buffer = new byte [10240];
109+
int read;
110+
int written = 0;
111+
while((read = fin.read(buffer)) > 0) {
112+
fout.write(buffer, offset + written, read);
113+
written += read;
114+
}
115+
fin.close();
116+
return written;
117+
}
118+
111119
/**
112120
* Write array of bytes into file
113121
* @param path Destination file path.
@@ -123,11 +131,7 @@ static public void writeFile(String path, ReadableArray data, final boolean appe
123131
if(!dir.exists())
124132
dir.mkdirs();
125133
FileOutputStream os = new FileOutputStream(f, append);
126-
byte [] bytes = new byte[data.size()];
127-
for(int i=0;i<data.size();i++) {
128-
bytes[i] = (byte) data.getInt(i);
129-
}
130-
os.write(bytes);
134+
os.write(DataConverter.RCTArrayToBytes(data));
131135
os.close();
132136
promise.resolve(data.size());
133137
} catch (Exception e) {
@@ -337,7 +341,7 @@ public void writeStream(String path, String encoding, boolean append, Callback c
337341
* @param data Data chunk in string format
338342
* @param callback JS context callback
339343
*/
340-
static void writeChunk(String streamId, String data, Callback callback) {
344+
static void writeStreamChunk(String streamId, String data, Callback callback) {
341345

342346
RNFetchBlobFS fs = fileStreams.get(streamId);
343347
OutputStream stream = fs.writeStreamInstance;
@@ -900,24 +904,65 @@ static String normalizePath(String path) {
900904
return PathResolver.getRealPathFromURI(RNFetchBlob.RCTContext, uri);
901905
}
902906

907+
/**
908+
* Read {length} bytes from {path} from {offset} bytes.
909+
* @param path Source file URI
910+
* @param encoding The encoding of output data
911+
* @param offset Offset of the file.
912+
* @param length Length of data to read.
913+
* @return Result of the operation.
914+
* @throws Exception
915+
*/
903916
static Object readChunk(String path, String encoding, int offset, int length) throws Exception {
904917
path = normalizePath(path);
905918
if(path == null)
906919
return null;
907920
byte [] buffer = new byte[length];
908-
InputStream in = new FileInputStream(path);
921+
FileInputStream in = new FileInputStream(path);
909922
int read = in.read(buffer, offset, length);
910-
923+
Object result = null;
911924
if(encoding.equalsIgnoreCase(RNFetchBlobConst.RNFB_RESPONSE_BASE64)) {
912-
return DataConverter.byteToBase64(buffer, read);
925+
result = DataConverter.byteToBase64(buffer, read);
913926
}
914927
else if(encoding.equalsIgnoreCase(RNFetchBlobConst.RNFB_RESPONSE_UTF8)) {
915-
return DataConverter.byteToUTF8(buffer, read);
928+
result = DataConverter.byteToUTF8(buffer, read);
916929
}
917930
else if(encoding.equalsIgnoreCase(RNFetchBlobConst.RNFB_RESPONSE_ASCII)) {
918-
return DataConverter.byteToRCTArray(buffer, read);
931+
result = DataConverter.byteToRCTArray(buffer, read);
919932
}
920-
return null;
933+
in.close();
934+
return result;
935+
936+
}
921937

938+
/**
939+
* Write specified data to destination start from {offset} bytes.
940+
* @param path Destination file path.
941+
* @param encoding Encoding of input data.
942+
* @param data Data to be written to file.
943+
* @param offset Offset of the operation.
944+
*/
945+
static void writeChunk(String path, String encoding, Object data, int offset) throws Exception {
946+
if(path == null)
947+
return;
948+
FileOutputStream out = new FileOutputStream(path);
949+
byte [] bytes = null;
950+
switch (encoding) {
951+
case RNFetchBlobConst.DATA_ENCODE_BASE64 :
952+
bytes = Base64.decode((String)data, 0);
953+
break;
954+
case RNFetchBlobConst.DATA_ENCODE_UTF8 :
955+
bytes = ((String)data).getBytes();
956+
break;
957+
case RNFetchBlobConst.DATA_ENCODE_ASCII :
958+
bytes = DataConverter.RCTArrayToBytes((ReadableArray) data);
959+
break;
960+
case RNFetchBlobConst.DATA_ENCODE_URI :
961+
writeFileToFileWithOffset((String)data, path, offset, false);
962+
return;
963+
}
964+
if(bytes != null)
965+
out.write(bytes, offset, bytes.length);
922966
}
967+
923968
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
package com.RNFetchBlob.Utils;
22

3+
import android.net.Uri;
34
import android.os.SystemClock;
5+
import android.provider.ContactsContract;
46
import android.util.Base64;
57

8+
import com.RNFetchBlob.RNFetchBlob;
9+
import com.RNFetchBlob.RNFetchBlobConst;
10+
import com.RNFetchBlob.RNFetchBlobFS;
611
import com.facebook.react.bridge.Arguments;
12+
import com.facebook.react.bridge.ReadableArray;
713
import com.facebook.react.bridge.WritableArray;
814

15+
import java.io.File;
16+
import java.io.FileInputStream;
17+
import java.io.FileNotFoundException;
918
import java.nio.ByteBuffer;
1019
import java.nio.charset.CharacterCodingException;
1120
import java.nio.charset.Charset;
@@ -37,4 +46,13 @@ public static WritableArray byteToRCTArray(byte [] data, int length) {
3746
}
3847
return chunk;
3948
}
49+
50+
public static byte[] RCTArrayToBytes(ReadableArray data) {
51+
byte [] bytes = new byte[data.size()];
52+
for(int i=0;i<data.size();i++) {
53+
bytes[i] = (byte) data.getInt(i);
54+
}
55+
return bytes;
56+
}
57+
4058
}

0 commit comments

Comments
 (0)