NSData Behaving as a Consumable Data Stream. Possible?
I am currently doing a lot of data wrangling. I ingest a lloonngg NSData byte streams and then parse that d开发者_C百科ata. The parsing is trivial. However, I have to simulate consumption of the data as I parse via not particularly elegant bookkeeping. Here is what a typical method looks like in the category of NData I have implemented:
// Grab a little-endian 32-bit number - (uint32_t)getInt32OffsetIncrement:(NSUInteger *)offset {
uint32_t unused;
NSRange myRange = NSMakeRange(*offset, sizeof(unused));
[self getBytes:&unused range:myRange];
*offset += sizeof(unused);
return CFSwapInt32LittleToHost(unused);
}
As you can see, I retrieve the data and then advance to NSRange "pointer" into the data stream. When I'm done I have consumed the entire data stream.
Have I overlooked any methods on NSData that can simultaneously retrieve data and advance a pointer along the length of the data stream?
Cheers, Doug
I just wrote code very similar to this. I don't believe there is any built-in NSData method to help with this. It looks like you're already doing this as an NSData category. I think that's the best you can do unless you want to subclass and keep the offset in a member.
精彩评论