@@ -542,33 +542,38 @@ + (void)slice:(NSString *)path
542
542
// abort for the source file not exists
543
543
if ([fm fileExistsAtPath: path] == NO )
544
544
{
545
- reject (@" RNFetchBlob slice failed" , @" the file does not exists" , path);
545
+ reject (@" RNFetchBlob slice failed : the file does not exists" , path, nil );
546
546
return ;
547
547
}
548
548
long size = [fm attributesOfItemAtPath: path error: nil ].fileSize ;
549
- // abort for the file size is less than start
550
- if (size < [start longValue ])
551
- {
552
- reject (@" RNFetchBlob slice failed" , @" start is greater than file size" , @" start is greater than file size" );
553
- return ;
554
- }
549
+ long max = MIN (size, [end longValue ]);
550
+
555
551
if (![fm fileExistsAtPath: dest]) {
556
552
[fm createFileAtPath: dest contents: @" " attributes: nil ];
557
553
}
558
554
[handle seekToFileOffset: [start longValue ]];
559
555
while (read < expected)
560
556
{
561
-
562
- NSData * chunk = [handle readDataOfLength: 10240 ] ;
563
- long remain = expected - read ;
564
- if (remain < 10240 )
557
+
558
+ NSData * chunk;
559
+ long chunkSize = 0 ;
560
+ if ([start longValue ] + read + 10240 > max )
565
561
{
566
- [os write: [chunk bytes ] maxLength: remain];
562
+ NSLog (@" read chunk %lu " , max - read - [start longValue ]);
563
+ chunkSize = max - read - [start longValue ];
564
+ chunk = [handle readDataOfLength: chunkSize];
567
565
}
568
566
else
569
567
{
570
- [os write: [chunk bytes ] maxLength: 10240 ];
568
+ NSLog (@" read chunk %lu " , 10240 );
569
+ chunkSize = 10240 ;
570
+ chunk = [handle readDataOfLength: 10240 ];
571
571
}
572
+ if ([chunk length ] <= 0 )
573
+ break ;
574
+ long remain = expected - read;
575
+
576
+ [os write: [chunk bytes ] maxLength: chunkSize];
572
577
read += [chunk length ];
573
578
}
574
579
[handle closeFile ];
0 commit comments