Subclassing NSInputStream to load a specific piece of data from a local file path

I have 100 MB of data in one file. This 100 MB data will actually be shared. those. I need to create an NSInputStream that points to different 5 MB pieces. This is possible by creating a stream with NSData.

But I would like to know if I can have an NSInputStream that points to a range of data in a file?

+4
source share
1 answer

If you want to download the file by passing an instance of NSInputStream to NSURLRequest.HTTPBodyStream , you really need to subclass NSInputStream and only pass the bytes that you want to load. Using NSFileHandle is not an option here.

Subclassing NSInputStream to work with NSURLRequest is pretty tricky, but fortunately there’s a great blog post on how to do this.

Here you can find a ready-to-use subclass of NSInputStream .

You can send the ChunkInputStream other NSInputStream reading the file and pass in the starting position and the number of bytes read.

Swift example:

 let fileInputStream = NSInputStream(fileAtPath: "/tmp/readme") let inputStream = ChunkInputStream(inputStream: fileInputStream) inputStream.startPosition = 2097152 inputStream.readMax = 1048576 
+2
source

Source: https://habr.com/ru/post/1478982/


All Articles