Skip to content

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).
  3. If it is Nothing, skip allocation entirely and return Nothing to the caller (BR-005).
  4. When a stream is present, allocate the buffer with ReDim bytes(CInt(inStream.Length)).
  5. Read inStream.Length bytes starting from the stream's current position into the allocated array.
  6. Return the populated byte array to the caller.

Process flow

The following diagram visualizes this operation's control flow.

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.

Sources

  • SAIF.Framework.Mail\MailServices.vb