NSData Behavior as a consumable data stream. Possible?

I am currently doing a lot of data shuffling. I swallow NSData lloonngg byte streams and then parse that data. Parsing is trivial. However, I have to simulate data consumption as I am analyzing not particularly elegant accounting records. This is what a typical method looks like in the NData category that I have implemented:

// Take a 32-bit mini-character 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 am fetching the data and then navigating to the NSRange "pointer" in the data stream. When I finished, I used the entire data stream.

Am I missing out on any NSData methods that can fetch data and advance a pointer along the length of the data stream at the same time?

Cheers, Doug

+2


source to share


1 answer


I just wrote a code very similar to this one. I don't believe there is a built-in NSData method in this. It looks like you are already doing this as an NSData category. I think the best thing you can do if you don't want to subclass and keep the offset in the member.



+2


source







All Articles