View Source ExDicom.ByteStream (EX_DICOM v0.1.0)

Internal helper module to assist with parsing. Supports reading from a byte stream contained in a binary.

Example usage:

byte_array = <<1, 2, 3, 4>>
{:ok, stream} = ByteStream.new(byte_array_parser, byte_array)

Summary

Functions

Adds a warning message to the ByteStream's warnings list.

Returns the current position in the byte array

Returns the size of the byte array

Creates a new ByteStream struct.

Returns a new ByteStream struct from the current position containing the requested number of bytes

Reads a string of 8-bit characters up to the specified length or null terminator

Parses an unsigned int 16 from the byte array and advances the position by 2 bytes

Parses an unsigned int 32 from the byte array and advances the position by 4 bytes

Safely seeks through the byte stream. Returns error if attempt is made to seek outside of the byte array.

Types

t()

@type t() :: %ExDicom.ByteStream{
  byte_array: binary(),
  byte_array_parser: module(),
  position: non_neg_integer(),
  warnings: [String.t()]
}

Functions

add_warning(stream, warning)

@spec add_warning(t(), String.t()) :: t()

Adds a warning message to the ByteStream's warnings list.

Parameters

  • stream: ByteStream struct
  • warning: warning message to add

Returns

  • updated ByteStream struct with new warning added to warnings list

get_position(byte_stream)

@spec get_position(t()) :: non_neg_integer()

Returns the current position in the byte array

Parameters

  • stream: ByteStream struct

Returns

  • current position in bytes

get_size(byte_stream)

@spec get_size(t()) :: non_neg_integer()

Returns the size of the byte array

Parameters

  • stream: ByteStream struct

Returns

  • size of the byte array in bytes

new(byte_array_parser, byte_array, position \\ 0)

@spec new(module(), binary(), non_neg_integer()) :: {:ok, t()} | {:error, String.t()}

Creates a new ByteStream struct.

Parameters

  • byte_array_parser: module that implements parsing functions
  • byte_array: binary containing the byte stream
  • position: optional starting position (defaults to 0)

Returns

  • {:ok, stream} if successful
  • {:error, reason} if validation fails

read_byte_stream(stream, num_bytes)

@spec read_byte_stream(t(), non_neg_integer()) :: {:ok, t()} | {:error, String.t()}

Returns a new ByteStream struct from the current position containing the requested number of bytes

Parameters

  • stream: ByteStream struct
  • num_bytes: length of the binary for the new ByteStream

Returns

  • {:ok, new_stream} containing the requested bytes
  • {:error, reason} if buffer overread would occur

read_fixed_string(stream, length)

@spec read_fixed_string(t(), non_neg_integer()) ::
  {:ok, String.t(), t()} | {:error, String.t()}

Reads a string of 8-bit characters up to the specified length or null terminator

Parameters

  • stream: ByteStream struct
  • length: maximum number of bytes to parse

Returns

  • {:ok, string, new_stream} with parsed string and updated position
  • {:error, reason} if buffer overread would occur

read_uint16(stream)

@spec read_uint16(t()) :: {:ok, non_neg_integer(), t()} | {:error, String.t()}

Parses an unsigned int 16 from the byte array and advances the position by 2 bytes

Parameters

  • stream: ByteStream struct

Returns

  • {:ok, value, new_stream} with parsed uint16 and updated position
  • {:error, reason} if buffer overread would occur

read_uint32(stream)

@spec read_uint32(t()) :: {:ok, non_neg_integer(), t()} | {:error, String.t()}

Parses an unsigned int 32 from the byte array and advances the position by 4 bytes

Parameters

  • stream: ByteStream struct

Returns

  • {:ok, value, new_stream} with parsed uint32 and updated position
  • {:error, reason} if buffer overread would occur

seek(stream, offset)

@spec seek(t(), integer()) :: {:ok, t()} | {:error, String.t()}

Safely seeks through the byte stream. Returns error if attempt is made to seek outside of the byte array.

Parameters

  • stream: ByteStream struct
  • offset: number of bytes to add to the position

Returns

  • {:ok, new_stream} with updated position
  • {:error, reason} if seek would be invalid