2

I need a Cocoa class that can read and write from a memory stream and that supports seeking. In C#, MemoryStream supports the method seek, and in Java, ByteArrayInputStream supports the methods mark, skip, and reset.

In iOS development, what are the equivalent class and method?

I need the above functionality for my project, and if it is by default not supported by the iOS frameworks, what would be the best way of going about implementing my own? E.g. write my own stream subclass inheriting from NSInputStream/NSOutputStream which will internally contain custom code?

Danko Valkov
  • 1,038
  • 8
  • 17
Lopper
  • 3,499
  • 7
  • 38
  • 57

1 Answers1

5

An arbitrary NSInputStream and NSOutputStream don't appear to support random offset seeking, and creating subclasses of them is notoriously difficult.

If you're going to be doing this reading and writing to a local file on disk (which I think you'd have to be, since I'm not sure how you could seek to a random offset on a network connection...), then what you're probably looking for is NSFileHandle.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • I am actually trying to read and write to memory but not to a file on disk. Is there other alternative for this? – Lopper Aug 11 '11 at 06:10
  • @Lopper what does "read and write to memory" mean? All objects live on memory, and creating new ones counts as writing to memory, as does modifying properties. Are you maybe looking for `NSMutableData`? – Dave DeLong Aug 11 '11 at 13:24
  • @@Dave What I actually need for the iOS application that I am developing is a common set of method call (or sending messages to class instances in the iOS world) that I can make use of to read or write binary data to either a file or memory. NSFileHandle is used for files and NSMutableData is used for binary data but what would be an elegant way to combine the two such that a common set of methods can be used for both? This would include being able to use the seek functionality for both. – Lopper Aug 12 '11 at 01:23
  • @Lopper maybe a new class that abstracts a common API for `NSMutableData` and `NSFileHandle`, and creates the correct object appropriately? – Dave DeLong Aug 12 '11 at 02:38