浏览代码

Fix Android fs.slice() offset logic

Ben Hsieh 8 年前
父节点
当前提交
6ad1a14d82
共有 1 个文件被更改,包括 8 次插入8 次删除
  1. 8
    8
      src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

+ 8
- 8
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java 查看文件

@@ -535,11 +535,16 @@ public class RNFetchBlobFS {
535 535
      * @param start Start byte offset in source file
536 536
      * @param end   End byte offset
537 537
      * @param encode
538
-     * @param callback
539 538
      */
540 539
     public static void slice(String src, String dest, int start, int end, String encode, Promise promise) {
541 540
         try {
542
-            long expected = end - start;
541
+            File source = new File(src);
542
+            if(!source.exists()) {
543
+                promise.reject("RNFetchBlob.slice error", "source file : " + src + " not exists");
544
+            }
545
+            long size = source.length();
546
+            long max = Math.min(size, end);
547
+            long expected = max - start;
543 548
             long now = 0;
544 549
             FileInputStream in = new FileInputStream(new File(src));
545 550
             FileOutputStream out = new FileOutputStream(new File(dest));
@@ -551,13 +556,8 @@ public class RNFetchBlobFS {
551 556
                 if(read <= 0) {
552 557
                     break;
553 558
                 }
554
-                if(remain < 10240) {
555
-                    out.write(buffer, 0, (int) remain);
556
-                }
557
-                else
558
-                    out.write(buffer, 0, (int) read);
559
+                out.write(buffer, 0, (int) Math.min(remain, read));
559 560
                 now += read;
560
-
561 561
             }
562 562
             in.close();
563 563
             out.flush();