简体   繁体   English

对NSInputStream进行子类化以从本地文件路径上载特定部分的数据

[英]Subclassing NSInputStream to upload a particular portion of data from a local filepath

I am having a 100 MB of data in a single file. 我在一个文件中有100 MB的数据。 This 100 mb data will be divided virtually. 这个100 MB的数据将被虚拟划分。 ie, I need to create an NSInputStream which points to different 5MB chunks. 即,我需要创建一个指向不同5MB块的NSInputStream。 This is possible by creating the stream with NSData. 这可以通过使用NSData创建流来实现。

But rather I'd like to know if I can have a NSInputStream which points to a range of data in the file ? 但我想知道我是否可以拥有指向文件中一系列数据的NSInputStream?

If you want to upload a file by passing a NSInputStream instance to NSURLRequest.HTTPBodyStream you indeed have to create a subclass of NSInputStream and only stream the bytes you want to upload. 如果要通过将NSInputStream实例传递给NSURLRequest.HTTPBodyStream来上载文件,则必须创建NSInputStream的子类,并仅传输要上载的字节。 Using NSFileHandle is not an option here. 在这里不能使用NSFileHandle

Creating a subclass of NSInputStream that works with NSURLRequest is quite tricky, but fortunately here is a great blog post about how it can be accomplished. 创建一个与NSURLRequest一起使用的NSInputStream的子类是非常棘手的,但幸运的是这里有一篇关于如何实现它的博客文章

Here you can find a ready to use subclass of NSInputStream for this purpose . 在这里,您可以为此找到NSInputStream的现成可用子类

You can feed ChunkInputStream with another NSInputStream reading a file and pass start position and number of bytes to read. 您可以使用另一个读取文件的NSInputStream来提供ChunkInputStream ,并传递开始位置和要读取的字节数。

Swift example: Swift示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM