---
title: Read a stream into bytes
summary: Copies an incoming stream into a byte array for attachment dispatch, or returns `Nothing` when no stream is supplied.
status: draft
owner: SAIF Documentation Generator
last_reviewed: 2026-07-30
library: SAIF.Framework.Mail
reference_type: package
operation_id: MAIL-OP-006
trigger: Attachment-sending code calls `GetStreamAsBytes(inStream)`.
---

# Read a stream into bytes

This internal helper materializes an already-open `Stream` as a `Byte()` array so the stream overloads of `SendMailWithAttachment` can pass it to the SOAP proxy. Unlike `GetFileAsBytes`, it treats a missing stream as a normal, non-exceptional case: a `Nothing` input simply produces a `Nothing` result.

| Field | Value |
| --- | --- |
| **Operation ID** | MAIL-OP-006 |
| **Trigger** | Attachment-sending code calls `GetStreamAsBytes(inStream)`. |
| **Inputs** | `inStream`. |
| **Outputs** | A `Byte()` payload, or `Nothing`. |
| **Primary components** | `Mail.GetStreamAsBytes`, `System.IO.Stream` |
| **Entry point** | `SAIF.Framework.Mail\MailServices.vb` |

## Steps

1. Initialize the local `bytes` variable to `Nothing`.
2. Test whether `inStream Is Nothing` ([BR-005](../business-rules.md#br-005-a-nothing-stream-input-yields-a-nothing-byte-array-instead-of-an-exception)).
   - If it is `Nothing`, skip allocation entirely and return `Nothing` to the caller ([BR-005](../business-rules.md#br-005-a-nothing-stream-input-yields-a-nothing-byte-array-instead-of-an-exception)).
3. When a stream is present, allocate the buffer with `ReDim bytes(CInt(inStream.Length))`.
4. Read `inStream.Length` bytes starting from the stream's current position into the allocated array.
5. Return the populated byte array to the caller.

## Process flow

The following diagram visualizes this operation's control flow.

```mermaid
flowchart TD
    Start([GetStreamAsBytes call]) --> Check{Stream is Nothing?}
    Check -->|Yes| ReturnNothing[Return Nothing]
    Check -->|No| Allocate[Allocate byte array]
    Allocate --> Read[Read stream into array]
    Read --> ReturnBytes[Return byte array]
```

## Failure and edge-case behavior

- A `Nothing` input stream is an accepted, expected case rather than an error condition; no exception is raised.
- The helper never rewinds the stream to position zero before reading, so whatever the stream's current read position happens to be determines which bytes end up in the returned array — a previously partially-read stream yields a partially-populated payload.
- `ReDim bytes(CInt(inStream.Length))` allocates one element more than `inStream.Length`, because VB array declarations use an inclusive upper bound rather than a count.

## Related documentation
- [Operations and processes](../operations-processes.md)
- [Overview](../overview.md)
- [Architecture](../architecture.md)
- [Dependencies](../dependencies.md)
- [Business rules](../business-rules.md)
- [Configuration](../configuration.md)
- [API reference](../api/index.md)

## Sources
- SAIF.Framework.Mail\MailServices.vb
