Class

NSInput​Stream

NSInput​Stream is a subclass of NSStream that provides read-only stream functionality.

Overview

NSInput​Stream is “toll-free bridged” with its Core Foundation counterpart, CFRead​Stream​Ref. For more information on toll-free bridging, see Toll-Free Bridging.

Subclassing Notes

NSInput​Stream is an abstract superclass of a class cluster consisting of concrete subclasses of NSStream that provide standard read-only access to stream data. Although NSInput​Stream is probably sufficient for most situations requiring access to stream data, you can create a subclass of NSInput​Stream 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 NSInput​Stream you may have to implement initializers for the type of stream data supported and suitably re-implement existing initializers. You must also provide complete implementations of the following methods:

  • read:​max​Length:​

    From the current read index, take up to the number of bytes specified in the second parameter from the stream and place them in the client-supplied buffer (first parameter). The buffer must be of the size specified by the second parameter. Return the actual number of bytes placed in the buffer; if there is nothing left in the stream, return 0. Reset the index into the stream for the next read operation.

  • get​Buffer:​length:​

    Return in 0(1) a pointer to the subclass-allocated buffer (first parameter). Return by reference in the second parameter the number of bytes actually put into the buffer. The buffer’s contents are valid only until the next stream operation. Return NO if you cannot access data in the buffer; otherwise, return YES. If this method is not appropriate for your type of stream, you may return NO.

  • has​Bytes​Available

    Return YES if there is more data to read in the stream, NO if there is not. If you want to be semantically compatible with NSInput​Stream, return YES if a read must be attempted to determine if bytes are available.

Symbols

Creating Streams

+ input​Stream​With​Data:​​

Creates and returns an initialized NSInput​Stream object for reading from a given NSData object.

+ input​Stream​With​File​At​Path:​​

Creates and returns an initialized NSInput​Stream object that reads data from the file at a given path.

+ input​Stream​With​URL:​​

Creates and returns an initialized NSInput​Stream object that reads data from the file at a given URL.

- init​With​Data:​​

Initializes and returns an NSInput​Stream object for reading from a given NSData object.

- init​With​File​At​Path:​​

Initializes and returns an NSInput​Stream object that reads data from the file at a given path.

- init​With​URL:​​

Initializes and returns an NSInput​Stream object that reads data from the file at a given URL.

Using Streams

- read:​​max​Length:​​

Reads up to a given number of bytes into a given buffer.

- get​Buffer:​​length:​​

Returns by reference a pointer to a read buffer and, by reference, the number of bytes available, and returns a Boolean value that indicates whether the buffer is available.

has​Bytes​Available

A Boolean value that indicates whether the receiver has bytes available to read.

Relationships

Inherits From