<!--
{
  "availability" : [
    "iOS: 13.4.0 -",
    "iPadOS: 13.4.0 -",
    "macCatalyst: 13.4.0 -",
    "macOS: 10.15.4 -",
    "tvOS: 13.4.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.2.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/FileHandle/read(upToCount:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:So12NSFileHandleC10FoundationE4read9upToCountAC4DataVSgSi_tKF"
  },
  "title" : "read(upToCount:)"
}
-->

# read(upToCount:)

Reads data synchronously up to the specified number of bytes.

```
func read(upToCount count: Int) throws -> Data?
```

## Parameters

`count`

The number of bytes to read from the file handle.

## Return Value

The data available through the receiver up to a maximum of `length` bytes, or the maximum size that can be represented by an [`NSData`](/documentation/Foundation/NSData) object, whichever is the smaller.

## Discussion

If the handle represents a file, this method returns the data obtained by reading `length` bytes starting at the current file pointer. If `length` bytes aren’t available, this method returns the data from the current file pointer to the end of the file. If the handle is a communications channel, the method reads up to `length` bytes from the channel. Returns an empty [`NSData`](/documentation/Foundation/NSData) object if the handle is at the file’s end or if the communications channel returns an end-of-file indicator.

This method throws an error if attempts to determine the file-handle type fail or if attempts to read from the file or channel fail.

---

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)