I am having a 100 MB of data in a single file. This 100 mb data will be divided virtually. i.e., I need to create an NSInputStream which points to different 5MB chunks. This is possible by creating the stream with NSData.
But rather I'd like to know if I can have a NSInputStream which points to a range of data in the file ?
If you want to upload a file by passing a
NSInputStreaminstance toNSURLRequest.HTTPBodyStreamyou indeed have to create a subclass ofNSInputStreamand only stream the bytes you want to upload. UsingNSFileHandleis not an option here.Creating a subclass of
NSInputStreamthat works withNSURLRequestis quite tricky, but fortunately here is a great blog post about how it can be accomplished.Here you can find a ready to use subclass of
NSInputStreamfor this purpose.You can feed
ChunkInputStreamwith anotherNSInputStreamreading a file and pass start position and number of bytes to read.Swift example: