Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/Foundation.framework |
Availability | Available in Mac OS X v10.3 and later. |
Companion guide | |
Declared in | NSStream.h |
Related sample code |
The NSOutputStream
class is a subclass of NSStream
that provides write-only stream functionality.
The 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).
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:
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 there was an error writing to the stream, return -1.
If the stream is of a fixed length and has reached its capacity, return zero.
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.
+ outputStreamToMemory
+ outputStreamToBuffer:capacity:
+ outputStreamToFileAtPath:append:
– initToMemory
– initToBuffer:capacity:
– initToFileAtPath:append:
Creates and returns an initialized output stream that can write to a provided buffer.
+ (id)outputStreamToBuffer:(uint8_t *)buffer capacity:(NSUInteger)capacity
The buffer the output stream will write to.
The size of the buffer in bytes.
An initialized output stream that can write to buffer.
The stream must be opened before it can be used.
When the number of bytes written to buffer has reached capacity, the stream’s streamStatus
will return NSStreamStatusAtEnd
.
NSStream.h
Creates and returns an initialized output stream for writing to a specified file.
+ (id)outputStreamToFileAtPath:(NSString *)path append:(BOOL)shouldAppend
The path to the file the output stream will write to.
YES
if newly written data should be appended to any existing file contents, NO
otherwise.
An initialized output stream that can write to path.
The stream must be opened before it can be used.
NSStream.h
Creates and returns an initialized output stream that will write stream data to memory.
+ (id)outputStreamToMemory
An initialized output stream that will write stream data to memory.
The stream must be opened before it can be used.
You retrieve the contents of the memory stream by sending the message propertyForKey:
to the receiver with an argument of NSStreamDataWrittenToMemoryStreamKey
.
NSStream.h
Returns whether the receiver can be written to.
- (BOOL)hasSpaceAvailable
YES
if the receiver can be written to or if a write must be attempted in order to determine if space is available, NO
otherwise.
NSStream.h
Returns an initialized output stream that can write to a provided buffer.
- (id)initToBuffer:(uint8_t *)buffer capacity:(NSUInteger)capacity
The buffer the output stream will write to.
The size of the buffer in bytes.
An initialized output stream that can write to buffer.
The stream must be opened before it can be used.
When the number of bytes written to buffer has reached capacity, the stream’s streamStatus
will return NSStreamStatusAtEnd
.
NSStream.h
Returns an initialized output stream for writing to a specified file.
- (id)initToFileAtPath:(NSString *)path append:(BOOL)shouldAppend
The path to the file the output stream will write to.
YES
if newly written data should be appended to any existing file contents, NO
otherwise.
An initialized output stream that can write to path.
The stream must be opened before it can be used.
NSStream.h
Returns an initialized output stream that will write to memory.
- (id)initToMemory
An initialized output stream that will write stream data to memory.
The stream must be opened before it can be used.
The contents of the memory stream are retrieved by passing the constant NSStreamDataWrittenToMemoryStreamKey
to propertyForKey:
.
NSStream.h
Writes the contents of a provided data buffer to the receiver.
- (NSInteger)write:(const uint8_t *)buffer maxLength:(NSUInteger)length
The data to write.
The length of the data buffer, in bytes.
The number of bytes actually written, or -1
if an error occurs. More information about the error can be obtained with streamError
. If the receiver is a fixed-length stream and has reached its capacity, 0
is returned.
NSStream.h
© 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-01-06)