Buffering NSOutputStream used as NSInputStream?

2020-02-19 08:58发布

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.

4条回答
家丑人穷心不美
2楼-- · 2020-02-19 09:18

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.

查看更多
ゆ 、 Hurt°
3楼-- · 2020-02-19 09:24

Here is an already implemented class that does exactly what you want

BufferOutputStreamToInputStream

// initialize
self.bufferWriter = [[BufferOutputStreamToInputStream alloc] init];
[self.bufferWriter openOutputStream];

// later you want to set the delegate of the inputStream and shedule it in runloop
// remember, you are responsible for the inputStream, the outputStream is taken care off;)
self.bufferWriter.inputStream.delegate = self;
[self.bufferWriter.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.bufferWriter.inputStream open]

// fill with data when desired on some event      
[self.bufferWriter addDataToBuffer:someData];
查看更多
冷血范
4楼-- · 2020-02-19 09:28

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

@interface NSStream (BoundPairAdditions)
+ (void)createBoundInputStream:(NSInputStream **)inputStreamPtr outputStream:(NSOutputStream **)outputStreamPtr bufferSize:(NSUInteger)bufferSize;
@end

@implementation NSStream (BoundPairAdditions)

+ (void)createBoundInputStream:(NSInputStream **)inputStreamPtr outputStream:(NSOutputStream **)outputStreamPtr bufferSize:(NSUInteger)bufferSize
{
    CFReadStreamRef     readStream;
    CFWriteStreamRef    writeStream;

    assert( (inputStreamPtr != NULL) || (outputStreamPtr != NULL) );

    readStream = NULL;
    writeStream = NULL;

    CFStreamCreateBoundPair(
        NULL, 
        ((inputStreamPtr  != nil) ? &readStream : NULL),
        ((outputStreamPtr != nil) ? &writeStream : NULL), 
        (CFIndex) bufferSize);

    if (inputStreamPtr != NULL) {
        *inputStreamPtr  = [NSMakeCollectable(readStream) autorelease];
    }
    if (outputStreamPtr != NULL) {
        *outputStreamPtr = [NSMakeCollectable(writeStream) autorelease];
    }
}
@end

Basically you attach the ends of two streams together with a buffer.

查看更多
兄弟一词,经得起流年.
5楼-- · 2020-02-19 09:28

Anyone still using Objecive C, as of iOS 8 this is the canonical way to do it:

NSStream:getBoundStreamWithBufferSize:inputStream:outputStream:

查看更多
登录 后发表回答