Buffering NSOutputStream used as NSInputStream?

前端 未结 4 936
花落未央
花落未央 2020-12-28 08:36

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

4条回答
  •  别那么骄傲
    2020-12-28 09:16

    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];
    

提交回复
热议问题