Mac Developer Library

Developer

Foundation Framework Reference NSOutputStream Class Reference

Options
Deployment Target:

On This Page
Language:

NSOutputStream

Inheritance


Conforms To


Import Statement


Swift

import Foundation

Objective-C

@import Foundation;

Availability


Available in OS X v10.3 and later.

The NSOutputStream class is a subclass of NSStream that provides write-only stream functionality.

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 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.

  • hasSpaceAvailable

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

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

    Declaration

    Swift

    class func outputStreamToMemory() -> Self

    Objective-C

    + (instancetype)outputStreamToMemory

    Return Value

    An initialized output stream that will write stream data to memory.

    Discussion

    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.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.3 and later.

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

    Declaration

    Objective-C

    + (instancetype)outputStreamToBuffer:(uint8_t *)buffer capacity:(NSUInteger)capacity

    Parameters

    buffer

    The buffer the output stream will write to.

    capacity

    The size of the buffer in bytes.

    Return Value

    An initialized output stream that can write to buffer.

    Discussion

    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.

    Import Statement

    Objective-C

    @import Foundation;

    Availability

    Available in OS X v10.3 and later.

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

    Declaration

    Objective-C

    + (instancetype)outputStreamToFileAtPath:(NSString *)path append:(BOOL)shouldAppend

    Parameters

    path

    The path to the file the output stream will write to.

    shouldAppend

    YEStrue if newly written data should be appended to any existing file contents, NOfalse otherwise.

    Return Value

    An initialized output stream that can write to path.

    Discussion

    The stream must be opened before it can be used.

    Import Statement

    Objective-C

    @import Foundation;

    Availability

    Available in OS X v10.3 and later.

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

    Declaration

    Objective-C

    + (instancetype)outputStreamWithURL:(NSURL *)url append:(BOOL)shouldAppend

    Parameters

    url

    The URL to the file the output stream will write to.

    shouldAppend

    YEStrue if newly written data should be appended to any existing file contents, NOfalse otherwise.

    Return Value

    An initialized output stream that can write to url.

    Discussion

    The stream must be opened before it can be used.

    Import Statement

    Objective-C

    @import Foundation;

    Availability

    Available in OS X v10.6 and later.

  • init(toMemory:) - initToMemory Designated Initializer

    Returns an initialized output stream that will write to memory.

    Declaration

    Swift

    init(toMemory toMemory: ())

    Objective-C

    - (instancetype)initToMemory

    Return Value

    An initialized output stream that will write stream data to memory.

    Discussion

    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:.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.3 and later.

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

    Declaration

    Swift

    init(toBuffer buffer: UnsafeMutablePointer<UInt8>, capacity capacity: Int)

    Objective-C

    - (instancetype)initToBuffer:(uint8_t *)buffer capacity:(NSUInteger)capacity

    Parameters

    buffer

    The buffer the output stream will write to.

    capacity

    The size of the buffer in bytes.

    Return Value

    An initialized output stream that can write to buffer.

    Discussion

    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.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.3 and later.

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

    Declaration

    Swift

    convenience init?(toFileAtPath path: String, append shouldAppend: Bool)

    Objective-C

    - (instancetype)initToFileAtPath:(NSString *)path append:(BOOL)shouldAppend

    Parameters

    path

    The path to the file the output stream will write to.

    shouldAppend

    YEStrue if newly written data should be appended to any existing file contents, NOfalse otherwise.

    Return Value

    An initialized output stream that can write to path.

    Discussion

    The stream must be opened before it can be used.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.3 and later.

  • init(URL:append:) - initWithURL:append: Designated Initializer

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

    Declaration

    Swift

    init?(URL url: NSURL, append shouldAppend: Bool)

    Objective-C

    - (instancetype)initWithURL:(NSURL *)url append:(BOOL)shouldAppend

    Parameters

    url

    The URL to the file the output stream will write to.

    shouldAppend

    YEStrue if newly written data should be appended to any existing file contents, NOfalse otherwise.

    Return Value

    An initialized output stream that can write to url.

    Discussion

    The stream must be opened before it can be used.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.6 and later.

  • A boolean value that indicates whether the receiver can be written to. (read-only)

    Declaration

    Swift

    var hasSpaceAvailable: Bool { get }

    Objective-C

    @property(readonly) BOOL hasSpaceAvailable

    Discussion

    YEStrue if the receiver can be written to or if a write must be attempted in order to determine if space is available, NOfalse otherwise.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.3 and later.

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

    Declaration

    Swift

    func write(_ buffer: UnsafePointer<UInt8>, maxLength length: Int) -> Int

    Objective-C

    - (NSInteger)write:(const uint8_t *)buffer maxLength:(NSUInteger)length

    Parameters

    buffer

    The data to write.

    length

    The length of the data buffer, in bytes.

    Return Value

    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.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.3 and later.