I have this consumer class that takes an NSInputStream as argument which will be processed async, and I want to push data that comes from a producer class that requires that it has an NSOutputStream provided as its output source. Now how could I set up a buffering (or transparent) stream that acts as the output stream for the producer, and at the same time as the NSInputStream for my consumer class?
I've looked a bit at the NSOutputStream +outputStreamToMemory and +outputStreamToBuffer:capacity: but haven't really figured out how to use it as input for an NSInputSource.
I had some idea of setting up a middle-man class that holds the actual buffer, then creating two subclasses (one for each NSInput/OutputStream) which holds a reference to this buffering class, and having these subclasses delegate most calls to that class, e.g output subclass methods hasSpaceAvailable, write:maxLength:, and for the input, hasBytesAvailable, read:maxLength: etc.
Any tips on how to approach this situation are appreciated. Thanks.
You might want to consider subclassing NSInputStream, and wrapping the source stream in your new class that buffers and/or modifies the bytes as they pass through.
The main reason I found for doing this over the bound sockets approach is to support seeking. File based NSInputStreams use a stream property to seek within the file, and I couldn't easily arrange this without subclassing.
A problem with this approach is that it seems toll-free bridging won't work for you subclass - but there is a very nice article that will also give you a template subclass to start from if you need one:
http://bjhomer.blogspot.co.uk/2011/04/subclassing-nsinputstream.html
I got a buffering solution working using both approaches - although another issue I had with the subclass approach is that you need to take care to send events to listeners appropriately - for example, when your source stream sends you an EOF event, you won't pass it on to your consumer until they have emptied the buffer - so there is some messing about to do there.
Also - you might need to ensure that clients do their reading off the main run loop (I got it working with grand central dispatch) - because any observing you do in your subclass - on the source stream - will clash with the consumer otherwise. Although you appear to be able to pick any run loop to observe streams on, only the main one works.
So overall I'd say go with the paired streams unless you need to support seeking - or are particularly averse to the paired streams method.
Here is an already implemented class that does exactly what you want
BufferOutputStreamToInputStream
One way to accomplish this would be to use the example code on the apple developer site. SimpleURLConnection example
This is how to do it, as can be seen in the PostController.m code
Basically you attach the ends of two streams together with a buffer.
Anyone still using Objecive C, as of iOS 8 this is the canonical way to do it:
NSStream:getBoundStreamWithBufferSize:inputStream:outputStream: