Method OpenReadAsync
OpenReadAsync(FileAccessPattern, CancellationToken)
Opens a read-only, seekable stream over the content of the file.
Declaration
Task<Stream> OpenReadAsync(FileAccessPattern accessPattern, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| FileAccessPattern | accessPattern | How the caller is going to read the stream: from start to end, or by seeking within it. This is a hint that the implementation may use to choose how it reads the content; the stream can be read either way. |
| CancellationToken | cancellationToken | A cancellation token that cancels opening the stream. It does not apply to reads from the returned stream. |
Returns
| Type | Description |
|---|---|
| Task<Stream> | A readable, seekable stream positioned at the start of the content, which the caller owns and must dispose. |
Remarks
The stream is readable and seekable, and cannot be written to: CanSeek is true, and Length, Position and Seek(long, SeekOrigin) work, with the length equal to the size of the file. The stream may support only asynchronous reads, so callers read it with ReadAsync(Memory<byte>, CancellationToken) or CopyToAsync(Stream, CancellationToken) and must not rely on Read(byte[], int, int). The local implementation returns a FileStream, which supports both. An implementation that reads over a network can provide seeking with range requests.
The access pattern is a hint about the caller's intent. Implementations may use it to choose buffering, read-ahead or their request strategy, for example prefetching ahead of a sequential reader but fetching only the ranges a random reader asks for, and must return the same, correct content whichever pattern is passed. The local implementation opens the file with SequentialScan or RandomAccess.
An open stream must not prevent the file from being deleted. The local implementation opens the file as RepoFileInfo.Open does, with
Read | Delete: other readers can open the file concurrently, and the repository can delete the file
while the stream is still open.
Exceptions
| Type | Condition |
|---|---|
| IOException | The content is no longer available, for example because a cleanup pass physically removed the file after it was fetched. |
| OperationCanceledException | The operation was canceled. |