Class

NSOutputStream

A stream that provides write-only stream functionality.

Overview

NSOutputStream is “toll-free bridged” with its Core Foundation counterpart, CFWriteStreamRef. For more information on toll-free bridging, see Toll-Free Bridging.

Subclassing Notes

NSOutputStream is a concrete subclass of NSStream that lets you write data to a stream. Although NSOutputStream is probably sufficient for most situations requiring this capability, you can create a subclass of NSOutputStream if you want more specialized behavior (for example, you want to record statistics on the data in a stream).

Methods to Override

To create a subclass of NSOutputStream you may have to implement initializers for the type of stream data supported and suitably reimplement existing initializers. You must also provide complete implementations of the following methods:

  • write:maxLength:

    From the current write pointer, take up to the number of bytes specified in the maxLength: parameter from the client-supplied buffer (first parameter) and put them onto the stream. The buffer must be of the size specified by the second parameter. To prepare for the next operation, offset the write pointer by the number of bytes written. Return a signed integer based on the outcome of the current operation:

    • If the write operation is successful, return the actual number of bytes put onto the stream.

    • If the stream is of a fixed length and has reached its capacity, return 0.

    • If there was an error writing to the stream, return -1.

  • hasSpaceAvailable

    Return YES if the stream can currently accept more data, NO if it cannot. If you want to be semantically compatible with NSOutputStream, return YES if a write must be attempted to determine if space is available.

Topics

Creating Streams

outputStreamToMemory

Creates and returns an initialized output stream that will write stream data to memory.

outputStreamToBuffer:capacity:

Creates and returns an initialized output stream that can write to a provided buffer.

outputStreamToFileAtPath:append:

Creates and returns an initialized output stream for writing to a specified file.

outputStreamWithURL:append:

Creates and returns an initialized output stream for writing to a specified URL.

initToMemory

Returns an initialized output stream that will write to memory.

initToBuffer:capacity:

Returns an initialized output stream that can write to a provided buffer.

initToFileAtPath:append:

Returns an initialized output stream for writing to a specified file.

initWithURL:append:

Returns an initialized output stream for writing to a specified URL.

Using Streams

hasSpaceAvailable

A boolean value that indicates whether the receiver can be written to.

write:maxLength:

Writes the contents of a provided data buffer to the receiver.

Relationships

Inherits From

See Also

Streams

NSStream

An abstract class representing a stream.

NSInputStream

A stream that provides read-only stream functionality.

NSStreamDelegate

An interface that delegates of a stream instance use to handle events on the stream.