<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/OutputStream",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSOutputStream"
  },
  "title" : "OutputStream"
}
-->

# OutputStream

A stream that provides write-only stream functionality.

```
class OutputStream
```

## Overview

[`OutputStream`](/documentation/Foundation/OutputStream) is “toll-free bridged” with its Core Foundation counterpart, <doc://com.apple.documentation/documentation/CoreFoundation/CFWriteStream>. For more information on toll-free bridging, see [Toll-Free Bridging](https://developer.apple.com/library/archive/documentation/General/Conceptual/CocoaEncyclopedia/Toll-FreeBridgin/Toll-FreeBridgin.html#//apple_ref/doc/uid/TP40010810-CH2).

### 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:)`](/documentation/Foundation/OutputStream/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`](/documentation/Foundation/OutputStream/hasSpaceAvailable)

Return <doc://com.apple.documentation/documentation/Swift/true> if the stream can currently accept more data, <doc://com.apple.documentation/documentation/Swift/false> if it cannot. If you want to be semantically compatible with `NSOutputStream`, return <doc://com.apple.documentation/documentation/Swift/true> if a write must be attempted to determine if space is available.

## Topics

### Creating Streams

[`toMemory()`](/documentation/Foundation/OutputStream/toMemory())

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

[`outputStreamToBuffer:capacity:`](/documentation/Foundation/NSOutputStream/outputStreamToBuffer:capacity:)

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

[`outputStreamToFileAtPath:append:`](/documentation/Foundation/NSOutputStream/outputStreamToFileAtPath:append:)

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

[`init(toMemory:)`](/documentation/Foundation/OutputStream/init(toMemory:))

Returns an initialized output stream that will write to memory.

[`init(toBuffer:capacity:)`](/documentation/Foundation/OutputStream/init(toBuffer:capacity:))

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

[`init(toFileAtPath:append:)`](/documentation/Foundation/OutputStream/init(toFileAtPath:append:))

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

[`init(url:append:)`](/documentation/Foundation/OutputStream/init(url:append:)-5soau)

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

### Using Streams

[`hasSpaceAvailable`](/documentation/Foundation/OutputStream/hasSpaceAvailable)

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

[`write(_:maxLength:)`](/documentation/Foundation/OutputStream/write(_:maxLength:))

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



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)