缓冲NSOutputStream用作NSInputStream? [英] Buffering NSOutputStream used as NSInputStream?

查看:221
本文介绍了缓冲NSOutputStream用作NSInputStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个消费类,它接受一个NSInputStream作为参数,将被异步处理,我想推送来自生产者类的数据,要求它有一个NSOutputStream作为其输出源。现在我如何设置一个缓冲(或透明)流作为输出流的生产者,同时为我的消费类的NSInputStream?

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?

我看了一下NSOutputStream + outputStreamToMemory和+ outputStreamToBuffer:capacity:但是没有真正搞清楚如何使用它作为NSInputSource的输入。

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.

我有一个想法,设置一个中间人类,保存实际的缓冲区,然后创建两个子类(每个NSInput / OutputStream一个),它保存对这个缓冲类的引用,并有这些子类委托大多数调用该类,例如输出子类方法hasSpaceAvailable,write:maxLength :,对于输入,hasBytesAvailable,read:maxLength:等。

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.

谢谢。

推荐答案

一种方法是使用Apple开发者网站上的示例代码。
SimpleURLConnection示例

One way to accomplish this would be to use the example code on the apple developer site. SimpleURLConnection example

这是怎么做的,可以在PostController.m代码中看到

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.

这篇关于缓冲NSOutputStream用作NSInputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆