Search code examples
iphoneiosfile-ionsfilemanagerfseek

Is there any way in NSFileManager to get contents of a file only for some range of bytes


I have some files that are of size 6.7GB, and more (these are video files). I want to get chunks of file data to send to my server, so what I currently do is :

contents = [fileManager contentsAtPath:path];
if (mFileOffset<[contents length]) {
NSRange range = NSMakeRange(mFileOffset, (allowedSize>[contents length]?[contents length]:allowedSize);
contents =[contents subdataWithRange:range];

However, this produces a memory issue:

malloc: *** mmap(size=616927232) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
APP(2700,0x4136000) malloc: *** mmap(size=616927232) failed (error code=12)
*** error: can't allocate region

Is there a way like fseek in c++ so that I read bytes of the file that come in the specified range only?


Solution

  • There is a method in NSFileHandle which synchronously reads data up to the specified number of bytes.

    -[NSFileHandle readDataOfLength:]
    

    For seeking :

    – offsetInFile
    – seekToEndOfFile
    – seekToFileOffset: