NSInputStream Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/Foundation.framework |
| Availability | Available in OS X v10.3 and later. |
| Companion guide | |
| Declared in | NSStream.h |
Overview
NSInputStream is a subclass of NSStream that provides read-only stream functionality.
NSInputStream is “toll-free bridged” with its Core Foundation counterpart, CFReadStreamRef. For more information on toll-free bridging, see “Toll-Free Bridging”.
Subclassing Notes
NSInputStream is a concrete subclass of NSStream that gives you standard read-only access to stream data. Although NSInputStream is probably sufficient for most situations requiring access to stream data, you can create a subclass of NSInputStream 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 NSInputStream 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:
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.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
NOif you cannot access data in the buffer; otherwise, returnYES. If this method is not appropriate for your type of stream, you may returnNO.Return
YESif there is more data to read in the stream,NOif there is not. If you want to be semantically compatible withNSInputStream, returnYESif a read must be attempted to determine if bytes are available.
Tasks
Creating Streams
-
+ inputStreamWithData: -
+ inputStreamWithFileAtPath: -
+ inputStreamWithURL: -
– initWithData: -
– initWithFileAtPath: -
– initWithURL:
Using Streams
Class Methods
inputStreamWithData:
Creates and returns an initialized NSInputStream object for reading from a given NSData object.
Parameters
- data
The data object from which to read. The contents of data are copied.
Return Value
An initialized NSInputStream object for reading from data. If data is not an NSData object, this method returns nil.
Availability
- Available in OS X v10.3 and later.
Declared In
NSStream.hinputStreamWithFileAtPath:
Creates and returns an initialized NSInputStream object that reads data from the file at a given path.
Parameters
- path
The path to the file.
Return Value
An initialized NSInputStream object that reads data from the file at path. If the file specified by path doesn’t exist or is unreadable, returns nil.
Availability
- Available in OS X v10.3 and later.
Declared In
NSStream.hinputStreamWithURL:
Creates and returns an initialized NSInputStream object that reads data from the file at a given URL.
Parameters
- url
The URL to the file.
Return Value
An initialized NSInputStream object that reads data from the URL at url. If the file specified by url doesn’t exist or is unreadable, returns nil.
Availability
- Available in OS X v10.6 and later.
See Also
Declared In
NSStream.hInstance Methods
getBuffer: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.
Parameters
- buffer
Upon return, contains a pointer to a read buffer. The buffer is only valid until the next stream operation is performed.
- len
Upon return, contains the number of bytes available.
Return Value
YES if the buffer is available, otherwise NO.
Subclasses of NSInputStream may return NO if this operation is not appropriate for the stream type.
Availability
- Available in OS X v10.3 and later.
Declared In
NSStream.hhasBytesAvailable
Returns a Boolean value that indicates whether the receiver has bytes available to read.
Return Value
YES if the receiver has bytes available to read, otherwise NO. May also return YES if a read must be attempted in order to determine the availability of bytes.
Availability
- Available in OS X v10.3 and later.
Declared In
NSStream.hinitWithData:
Initializes and returns an NSInputStream object for reading from a given NSData object.
Parameters
- data
The data object from which to read. The contents of data are copied.
Return Value
An initialized NSInputStream object for reading from data.
Availability
- Available in OS X v10.3 and later.
Declared In
NSStream.hinitWithFileAtPath:
Initializes and returns an NSInputStream object that reads data from the file at a given path.
Parameters
- path
The path to the file.
Return Value
An initialized NSInputStream object that reads data from the file at path. If the file specified by path doesn’t exist or is unreadable, returns nil.
Availability
- Available in OS X v10.3 and later.
Declared In
NSStream.hinitWithURL:
Initializes and returns an NSInputStream object that reads data from the file at a given URL.
Parameters
- url
The URL to the file.
Return Value
An initialized NSInputStream object that reads data from the file at url. If the file specified by url doesn’t exist or is unreadable, returns nil.
Availability
- Available in OS X v10.6 and later.
See Also
Declared In
NSStream.hread:maxLength:
Reads up to a given number of bytes into a given buffer.
Parameters
- buffer
A data buffer. The buffer must be large enough to contain the number of bytes specified by len.
- len
The maximum number of bytes to read.
Return Value
A number indicating the outcome of the operation:
A positive number indicates the number of bytes read;
0indicates that the end of the buffer was reached;A negative number means that the operation failed.
Availability
- Available in OS X v10.3 and later.
Declared In
NSStream.h© 2010 Apple Inc. All Rights Reserved. (Last updated: 2010-07-12)