<!--
{
  "availability" : [
    "iOS: 9.0.0 -",
    "iPadOS: 9.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.11.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/URLSessionStreamTask/readData(ofMinLength:maxLength:timeout:completionHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSURLSessionStreamTask(im)readDataOfMinLength:maxLength:timeout:completionHandler:"
  },
  "title" : "readData(ofMinLength:maxLength:timeout:completionHandler:)"
}
-->

# readData(ofMinLength:maxLength:timeout:completionHandler:)

Asynchronously reads a number of bytes from the stream, and calls a handler upon completion.

```
func readData(ofMinLength minBytes: Int, maxLength maxBytes: Int, timeout: TimeInterval, completionHandler: @escaping @Sendable (Data?, Bool, (any Error)?) -> Void)
```

## Parameters

`minBytes`

The minimum number of bytes to read.

`maxBytes`

The maximum number of bytes to read.

`timeout`

A timeout for reading bytes. If the read is not completed within the specified interval, the read is canceled and the `completionHandler` is called with an error. Pass `0` to prevent a read from timing out.

`completionHandler`

The completion handler to call when all bytes are read, or an error occurs. This handler is executed on the delegate queue.

    This completion handler takes the following parameters:

- `data`: The data read from the stream.
- `atEOF`: Whether or not the stream reached end-of-file (EOF), such that no more data can be read.
- `error`: An error object that indicates why the read failed, or `nil` if the read was successful.

---

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)