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

Commit 6473764

Browse files
committed
Add Android writeChunk implementation #124
Add Android writeChunk #124
1 parent c4d5594 commit 6473764

File tree

5 files changed

+101
-125
lines changed

5 files changed

+101
-125
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.support.annotation.Nullable;
77
import android.util.Log;
88

9+
import com.RNFetchBlob.Utils.EncodingResolver;
910
import com.RNFetchBlob.Utils.RNFBCookieJar;
1011
import com.facebook.react.bridge.ActivityEventListener;
1112
import com.facebook.react.bridge.Callback;
@@ -330,6 +331,23 @@ public void run() {
330331
});
331332
}
332333

334+
@ReactMethod
335+
public void readChunk(final String path, final String encoding, final int offset, final int length, final Promise promise) {
336+
fsThreadPool.execute(new Runnable() {
337+
@Override
338+
public void run() {
339+
try {
340+
Object result = RNFetchBlobFS.readChunk(path, encoding, offset, length);
341+
EncodingResolver.resolve(promise, encoding, result);
342+
} catch (Exception e) {
343+
e.printStackTrace();
344+
promise.reject(e.getMessage(), e.getMessage()) ;
345+
}
346+
}
347+
});
348+
349+
}
350+
333351

334352
@ReactMethod
335353
public void enableUploadProgressReport(String taskId, int interval, int count) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class RNFetchBlobConst {
1414
public static final String DATA_ENCODE_ASCII = "ascii";
1515
public static final String RNFB_RESPONSE_BASE64 = "base64";
1616
public static final String RNFB_RESPONSE_UTF8 = "utf8";
17+
public static final String RNFB_RESPONSE_ASCII = "ascii";
1718
public static final String RNFB_RESPONSE_PATH = "path";
1819
public static final Integer GET_CONTENT_INTENT = 99900;
1920

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

Lines changed: 17 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import android.support.annotation.Nullable;
1414
import android.util.Base64;
1515

16+
import com.RNFetchBlob.Utils.DataConverter;
1617
import com.RNFetchBlob.Utils.PathResolver;
1718
import com.facebook.react.bridge.Arguments;
1819
import com.facebook.react.bridge.Callback;
@@ -23,9 +24,10 @@
2324
import com.facebook.react.bridge.WritableMap;
2425
import com.facebook.react.modules.core.DeviceEventManagerModule;
2526

26-
import java.io.ByteArrayOutputStream;
27+
import java.io.ByteArrayInputStream;
2728
import java.io.File;
2829
import java.io.FileInputStream;
30+
import java.io.FileNotFoundException;
2931
import java.io.FileOutputStream;
3032
import java.io.IOException;
3133
import java.io.InputStream;
@@ -897,134 +899,24 @@ static String normalizePath(String path) {
897899
return PathResolver.getRealPathFromURI(RNFetchBlob.RCTContext, uri);
898900
}
899901

900-
/**
901-
* Read file with range
902-
* @param path The path of file to read
903-
* @param encoding Encoding of the result data should be one of `utf8` | `ascii` | `base64` | `uri`
904-
* @param offset The position the process will start to read
905-
* @param length Length of result data, in byte.
906-
* @param dest Optional, if this argument is given, the result data will be write to the `dest` as a file
907-
* @param promise JS context promise
908-
*/
909-
public void read(String path, String encoding, int offset, int length, @Nullable String dest, Promise promise) {
910-
try {
911-
File f = new File(normalizePath(path));
912-
File destFile = null;
913-
boolean destExists = true;
914-
// if `dest` is given, create destination file.
915-
if(dest != null) {
916-
destFile = new File(dest);
917-
if(!destFile.exists()) {
918-
destExists = destFile.createNewFile();
919-
}
920-
encoding = RNFetchBlobConst.DATA_ENCODE_URI;
921-
}
922-
if(!f.exists()) {
923-
promise.reject("RNFB read fail", "path : " + path + "does not exists.");
924-
return;
925-
}
926-
else if (!destExists) {
927-
promise.reject("RNFB read fail", "could not create destination file : " + dest);
928-
return;
929-
}
930-
FileInputStream in = new FileInputStream(f);
931-
OutputStream os;
932-
long bufferSize = 409500;
933-
long done = 0;
934-
int read = 0;
935-
length = length > 0 ? length : in.available() - offset;
936-
byte[] buffer = new byte[(int) bufferSize];
937-
938-
in.skip(offset);
939-
os = destFile == null ? new ByteArrayOutputStream() : new FileOutputStream(destFile);
940-
941-
942-
while((length = length - (read = in.read(buffer))) > 0) {
943-
os.write(buffer, (int) done, read);
944-
done += read;
945-
}
946-
947-
if(bufferSize + length > 0) {
948-
os.write(buffer, (int)done, (int)bufferSize + length);
949-
}
950-
951-
switch (encoding) {
952-
case RNFetchBlobConst.DATA_ENCODE_UTF8 :
953-
promise.resolve(((ByteArrayOutputStream) os).toString("UTF-8"));
954-
break;
955-
case RNFetchBlobConst.DATA_ENCODE_BASE64 :
956-
promise.resolve(Base64.encode(((ByteArrayOutputStream) os).toByteArray(), 0));
957-
break;
958-
case RNFetchBlobConst.DATA_ENCODE_ASCII :
959-
WritableArray byteArrary = Arguments.createArray();
960-
for(byte b : ((ByteArrayOutputStream) os).toByteArray()) {
961-
byteArrary.pushInt((int)b);
962-
}
963-
promise.resolve(byteArrary);
964-
break;
965-
case RNFetchBlobConst.DATA_ENCODE_URI :
966-
promise.resolve(dest);
967-
break;
968-
}
969-
in.close();
970-
os.close();
971-
902+
static Object readChunk(String path, String encoding, int offset, int length) throws Exception {
903+
path = normalizePath(path);
904+
if(path == null)
905+
return null;
906+
byte [] buffer = new byte[length];
907+
InputStream in = new FileInputStream(path);
908+
int read = in.read(buffer, offset, length);
972909

910+
if(encoding.equalsIgnoreCase(RNFetchBlobConst.RNFB_RESPONSE_BASE64)) {
911+
return DataConverter.byteToBase64(buffer, read);
973912
}
974-
catch(Exception ex) {
975-
promise.reject("RNFB read error", ex.getMessage());
976-
}
977-
}
978-
979-
/**
980-
* Write file to specific offset
981-
* @param path Path of the file to write.
982-
* @param encoding Encoding of the input data.
983-
* @param data Data to write to `path`.
984-
* @param offset Offset
985-
* @param promise JS context promise
986-
*/
987-
public void write(String path, String encoding, String data, int offset, int length, Promise promise) {
988-
989-
try {
990-
File f = new File(path);
991-
992-
if(!f.exists()) {
993-
promise.reject("RNFB write failed", " path : " + path + "does not exists");
994-
return;
995-
}
996-
byte[] bytes;
997-
OutputStream os = new FileOutputStream(f);
998-
switch (encoding) {
999-
case RNFetchBlobConst.DATA_ENCODE_UTF8 :
1000-
bytes = data.getBytes();
1001-
os.write(bytes, offset, length);
1002-
break;
1003-
case RNFetchBlobConst.DATA_ENCODE_BASE64 :
1004-
bytes = Base64.decode(data.getBytes(),0);
1005-
os.write(bytes, offset, length);
1006-
break;
1007-
case RNFetchBlobConst.DATA_ENCODE_URI :
1008-
FileInputStream in = new FileInputStream(new File(data));
1009-
1010-
int bufferSize = 102400;
1011-
int read = 0;
1012-
byte[] buffer = new byte[bufferSize];
1013-
int done = 0;
1014-
while((length = length - (read = in.read(buffer))) > 0) {
1015-
os.write(buffer, done, read);
1016-
done += read;
1017-
}
1018-
if(bufferSize + length > 0 ){
1019-
os.write(buffer, done, bufferSize + length );
1020-
}
1021-
os.close();
1022-
}
1023-
promise.resolve(length);
913+
else if(encoding.equalsIgnoreCase(RNFetchBlobConst.RNFB_RESPONSE_UTF8)) {
914+
return DataConverter.byteToUTF8(buffer, read);
1024915
}
1025-
catch (Exception ex) {
1026-
promise.reject("RNFB write error", ex.getMessage());
916+
else if(encoding.equalsIgnoreCase(RNFetchBlobConst.RNFB_RESPONSE_ASCII)) {
917+
return DataConverter.byteToRCTArray(buffer, read);
1027918
}
919+
return null;
1028920

1029921
}
1030922
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.RNFetchBlob.Utils;
2+
3+
import android.os.SystemClock;
4+
import android.util.Base64;
5+
6+
import com.facebook.react.bridge.Arguments;
7+
import com.facebook.react.bridge.WritableArray;
8+
9+
import java.nio.ByteBuffer;
10+
import java.nio.charset.CharacterCodingException;
11+
import java.nio.charset.Charset;
12+
import java.nio.charset.CharsetEncoder;
13+
14+
/**
15+
* Created by wkh237 on 2017/5/17.
16+
*/
17+
18+
public class DataConverter {
19+
20+
private static CharsetEncoder UTF8Encoder = Charset.forName("UTF-8").newEncoder();
21+
22+
public static String byteToBase64(byte [] data, int length) {
23+
return Base64.encodeToString(data, 0, length, 0);
24+
}
25+
26+
public static String byteToUTF8(byte [] data, int length) throws CharacterCodingException {
27+
28+
UTF8Encoder.encode(ByteBuffer.wrap(data).asCharBuffer());
29+
return new String(data);
30+
}
31+
32+
public static WritableArray byteToRCTArray(byte [] data, int length) {
33+
WritableArray chunk = Arguments.createArray();
34+
for(int i =0;i<length;i++)
35+
{
36+
chunk.pushInt((int)data[i]);
37+
}
38+
return chunk;
39+
}
40+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.RNFetchBlob.Utils;
2+
3+
import com.RNFetchBlob.RNFetchBlobConst;
4+
import com.facebook.react.bridge.Promise;
5+
import com.facebook.react.bridge.WritableArray;
6+
7+
/**
8+
* Created by wkh237 on 2017/5/17.
9+
*/
10+
11+
public class EncodingResolver {
12+
13+
public static void resolve(Promise promise, String encoding, Object data) {
14+
if(encoding.equalsIgnoreCase(RNFetchBlobConst.RNFB_RESPONSE_BASE64) ||
15+
encoding.equalsIgnoreCase(RNFetchBlobConst.RNFB_RESPONSE_UTF8)) {
16+
promise.resolve((String) data);
17+
}
18+
else if(encoding.equalsIgnoreCase(RNFetchBlobConst.RNFB_RESPONSE_ASCII)) {
19+
promise.resolve((WritableArray) data);
20+
}
21+
else
22+
promise.resolve(null);
23+
}
24+
25+
}

0 commit comments

Comments
 (0)