Browse Source

Fix Android fs.slice() offset logic

Ben Hsieh 8 years ago
parent
commit
6ad1a14d82
1 changed files with 8 additions and 8 deletions
  1. 8
    8
      src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

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

@@ -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();