<!--
{
  "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/InputStream",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSInputStream"
  },
  "title" : "InputStream"
}
-->

# InputStream

A stream that provides read-only stream functionality.

```
class InputStream
```

## Overview

[`InputStream`](/documentation/Foundation/InputStream) is “toll-free bridged” with its Core Foundation counterpart, <doc://com.apple.documentation/documentation/CoreFoundation/CFReadStream>. 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

`NSInputStream` is an abstract superclass of a *class cluster* consisting of concrete subclasses of `NSStream` that provide 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:

- [`read(_:maxLength:)`](/documentation/Foundation/InputStream/read(_:maxLength:))

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.

- [`getBuffer(_:length:)`](/documentation/Foundation/InputStream/getBuffer(_:length:))

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 <doc://com.apple.documentation/documentation/Swift/false> if you cannot access data in the buffer; otherwise, return <doc://com.apple.documentation/documentation/Swift/true>. If this method is not appropriate for your type of stream, you may return <doc://com.apple.documentation/documentation/Swift/false>.

- [`hasBytesAvailable`](/documentation/Foundation/InputStream/hasBytesAvailable)

Return <doc://com.apple.documentation/documentation/Swift/true> if there is more data to read in the stream, <doc://com.apple.documentation/documentation/Swift/false> if there is not. If you want to be semantically compatible with `NSInputStream`, return <doc://com.apple.documentation/documentation/Swift/true> if a read must be attempted to determine if bytes are available.

## Topics

### Creating Streams

[`+  inputStreamWithData:`](/documentation/Foundation/NSInputStream/inputStreamWithData:)

Creates and returns an initialized `NSInputStream` object for reading from a given `NSData` object.

[`+  inputStreamWithFileAtPath:`](/documentation/Foundation/NSInputStream/inputStreamWithFileAtPath:)

Creates and returns an initialized `NSInputStream` object that reads data from the file at a given path.

[`-  initWithData:`](/documentation/Foundation/InputStream/init(data:))

Initializes and returns an `NSInputStream` object for reading from a given `NSData` object.

[`-  initWithFileAtPath:`](/documentation/Foundation/InputStream/init(fileAtPath:))

Initializes and returns an `NSInputStream` object that reads data from the file at a given path.

[`-  initWithURL:`](/documentation/Foundation/InputStream/init(url:)-1lfmj)

Initializes and returns an `NSInputStream` object that reads data from the file at a given URL.

### Using Streams

[`-  read:maxLength:`](/documentation/Foundation/InputStream/read(_:maxLength:))

Reads up to a given number of bytes into a given buffer.

[`-  getBuffer:length:`](/documentation/Foundation/InputStream/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.

[`hasBytesAvailable`](/documentation/Foundation/InputStream/hasBytesAvailable)

A Boolean value that indicates whether the receiver has bytes available to read.

## Relationships

### Conforms To

[`Sendable`](/documentation/Swift/Sendable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`CVarArg`](/documentation/Swift/CVarArg)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`Equatable`](/documentation/Swift/Equatable)

[`Hashable`](/documentation/Swift/Hashable)

### Inherits From

[`Stream`](/documentation/Foundation/Stream)

---

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)