Re: file reading/writing over 2 gig
Re: file reading/writing over 2 gig
- Subject: Re: file reading/writing over 2 gig
- From: Jaime Magiera <email@hidden>
- Date: Sun, 12 Aug 2007 11:41:31 -0400
On Aug 12, 2007, at 3:35 AM, Sherm Pendley wrote:
On Aug 12, 2007, at 12:07 AM, Jaime Magiera wrote:
What is the preferred method for positional reading/writing of
files in Cocoa? For small files, NSFileHandle and NSData work
great. However, NSData caps at 2 gig. Is there another Cocoa
solution?
NSFileHandle isn't "capped" at 2G - its -offsetInFile and -
seekToFileOffset: methods return and take unsigned long long ints,
respectively. So, one can read/write at any position within Files
of Unusual Size with no trouble.
... but looking at the API, it appears there is only a writeData
method, which takes an NSData. So, I am limited by NSData, and would
have to iterate through the bytes. Or is here another method for
writing?
NSFileHandle's -readDataOfLength: method, as well as NSData methods
that deal with offsets and sizes, do take and return unsigned
longs, because a 32-bit app can address "only" 4GB of memory at
most. That's what limits the amount of data you can read into
memory at one time, even though NSFileHandle will allow you to read
that 4GB from any position within a much larger file. That
limitation applies to all 32-bit apps, not just those that use Cocoa.
A 64-bit app can address much more memory, but deploying such a
beast has problems of its own. It requires a 64-bit processor,
which limits it to G5s and Mac Pros. Also, 64-bit support is
currently limited to BSD/POSIX functions only - neither Cocoa nor
Carbon are currently 64-bit. That means you'd need to factor your
app into separate 32-bit GUI and 64-bit "worker" processes, and
implement some form of IPC to communicate between them.
Due to time constraints, I originally wrote the project using NSData,
which worked great...
NSMutableData *firstChunk = [NSMutableData dataWithCapacity:1];
[firstChunk appendData:[parentData subdataWithRange:NSMakeRange(0,
index)]];
NSMutableData *secondChunk = [parentData subdataWithRange:NSMakeRange
(index, ([parentData length] - index))];
[firstChunk appendData:childData];
[firstChunk appendData:secondChunk];
Then realized this won't work for large files due to the factors
mentioned above.
Rethinking this, it might be better in general to iterate through the
bytes...
NSData *byteRead;
NSFIleHandle *sourceHandle;
NSFIleHandle *targetHandle;
while(byteRead = [sourceHandle readDataOfLength:1)
{
[targetHandle writeData: byteRead];
}
Which solves the problem overall and allows for more specific
debugging in the write/read process.
Jaime Magiera
Sensory Research
http://www.sensoryresearch.net
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden