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
      * @param start Start byte offset in source file
535
      * @param start Start byte offset in source file
536
      * @param end   End byte offset
536
      * @param end   End byte offset
537
      * @param encode
537
      * @param encode
538
-     * @param callback
539
      */
538
      */
540
     public static void slice(String src, String dest, int start, int end, String encode, Promise promise) {
539
     public static void slice(String src, String dest, int start, int end, String encode, Promise promise) {
541
         try {
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
             long now = 0;
548
             long now = 0;
544
             FileInputStream in = new FileInputStream(new File(src));
549
             FileInputStream in = new FileInputStream(new File(src));
545
             FileOutputStream out = new FileOutputStream(new File(dest));
550
             FileOutputStream out = new FileOutputStream(new File(dest));
551
                 if(read <= 0) {
556
                 if(read <= 0) {
552
                     break;
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
                 now += read;
560
                 now += read;
560
-
561
             }
561
             }
562
             in.close();
562
             in.close();
563
             out.flush();
563
             out.flush();