@@ -542,33 +542,38 @@ + (void)slice:(NSString *)path
542542 // abort for the source file not exists
543543 if ([fm fileExistsAtPath: path] == NO )
544544 {
545- reject (@" RNFetchBlob slice failed" , @" the file does not exists" , path);
545+ reject (@" RNFetchBlob slice failed : the file does not exists" , path, nil );
546546 return ;
547547 }
548548 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+
555551 if (![fm fileExistsAtPath: dest]) {
556552 [fm createFileAtPath: dest contents: @" " attributes: nil ];
557553 }
558554 [handle seekToFileOffset: [start longValue ]];
559555 while (read < expected)
560556 {
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 )
565561 {
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];
567565 }
568566 else
569567 {
570- [os write: [chunk bytes ] maxLength: 10240 ];
568+ NSLog (@" read chunk %lu " , 10240 );
569+ chunkSize = 10240 ;
570+ chunk = [handle readDataOfLength: 10240 ];
571571 }
572+ if ([chunk length ] <= 0 )
573+ break ;
574+ long remain = expected - read;
575+
576+ [os write: [chunk bytes ] maxLength: chunkSize];
572577 read += [chunk length ];
573578 }
574579 [handle closeFile ];
0 commit comments