---
title: Convert a stream to a byte array
summary: Materialize a stream into an in-memory byte array.
status: draft
owner: saif-docgen
last_reviewed: 2026-09-15
library: SAIF.Framework.Common
reference_type: package
operation_id: OP-036
trigger: Caller materialises a read/uploaded stream for hashing, storage, or header stripping
---

# Convert a stream to a byte array

Materialize a stream into an in-memory byte array, triggered when caller materialises a read/uploaded stream for hashing, storage, or header stripping.

| Field | Value |
| --- | --- |
| **Operation ID** | OP-036 |
| **Trigger** | Caller materialises a read/uploaded stream for hashing, storage, or header stripping |
| **Inputs** | `Stream` |
| **Outputs** | `byte[]` |
| **Primary components** | ``FileExtensions.ToByteArray(this Stream)`` |
| **Entry point** | `Common/SAIF.Common/Extensions/FileExtensions.cs` |

## Steps

1. Create a `MemoryStream` inside a `using` block. ([BR-057](../business-rules.md#br-057-disposable-cryptographic-and-stream-resources-are-created-inside-using-blocks-so-they-are-released-deterministically))
2. Copy the source stream into it with `Stream.CopyTo` (the BCL method on .NET 4.6.2; the library's own `DotNet35Extensions.CopyTo` shim exists on disk but is excluded from compilation).
3. Return `memoryStream.ToArray()`. The source stream is not rewound beforehand and not disposed. ([BR-089](../business-rules.md#br-089-converting-a-stream-to-a-byte-array-copies-from-the-stream's-current-position---the-caller-owns-rewinding---and-does-not-dispose-the-source-stream))

## Process flow

The following diagram visualizes this operation's control flow.

```mermaid
flowchart TD
	Start([ToByteArray called]) --> S1["Create MemoryStream<br>inside using block"]
	S1 --> S2["Copy source stream into it<br>with Stream.CopyTo"]
	S2 --> S3["Return memoryStream.ToArray();<br>source stream not disposed"]
	S3 --> End([Return byte array])
```

## Related documentation
- [Operations and processes](../operations-processes.md)
- [Business rules](../business-rules.md)

## Sources
- `Common/SAIF.Common/Extensions/FileExtensions.cs`
