<!--
{
  "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/write(_:maxLength:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSOutputStream(im)write:maxLength:"
  },
  "title" : "write(_:maxLength:)"
}
-->

# write(_:maxLength:)

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

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

## Parameters

`buffer`

The data to write.

`len`

The length of the data buffer, in bytes.
  
  > Important:
  > The behavior of this method is undefined if you pass a negative or zero number.

## Return Value

A number indicating the outcome of the operation:

- A positive number indicates the number of bytes written.
- `0` indicates that a fixed-length stream and has reached its capacity.
- `-1` means that the operation failed; more information about the error can be obtained with [`streamError`](/documentation/Foundation/Stream/streamError).

## Discussion

---

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)