package bytream

  1. Overview
  2. Docs

Module Bytream.InSource

Incoming byte stream module for processing byte sources, using chunk buffers when needed, with minimized memory allocations.

Sourcetype t

Incoming byte stream type.

Flatten bytes-oriented Bigarray buffer.

Note. The rationale for using bytes instead of BA is to transparently transfer memory between the OCaml runtime and external functions, which are necessary for working with input/output.

Sourceand chunk = buffer:buffer * offset:int * length:int

A byte chunk is a non-empty consecutive range of bytes in a buffer value.

Construction

The section explains how you can create a new incoming byte stream.

Sourceval make : ?overlap_size:int -> (unit -> buffer) -> t

make ?overlap_size reader

Construct incoming byte stream from reader.

  • parameter reader

    This is a function that reads chunks from a source. It raises the End_of_file exception when the end of the source is reached.

  • parameter ?overlap_size

    By default, a buffer of 0xFF bytes is allocated to resolve data gaps between chunks, which are used to provide linear memory buffers.

Note. The buffer within a chunk is made available to third parties for a limited period of time during which the chunk is considered valid for reading or writing (or both).

See also, the make' function allows you to return a subview of the buffer.

Example

This example illustrates the basic concept of chunking.

(* Queue as a byte chunk source. *)
let queue =
  let queue = Queue.create () in
  Queue.add "he" queue;
  (* ... *)
  Queue.add "d!" queue;
  queue
in

(* Reader function that returns chunks of text from the source. *)
let reader () =
  match Queue.take_opt queue with
  | None ->
    (** For close incoming byte stream, the reader
        should raise an End_of_file exception.  *)
    raise End_of_file
  | Some chunk -> Bstr.of_string chunk
in

Bytream.In.make reader
Sourceval make' : ?overlap_size:int -> (unit -> chunk) -> t

make_intf ?overlap_size reader

Same as the make function, but the reader returns chunks instead of buffers.

Example

match In_channel.input_bigarray ic buffer 0 io_buffer_size with
| (* ... *)
| length -> (~buffer, ~offset:0, ~length)
Sourceval of_buffer : buffer -> t

of_buffer buffer

Construct a byte stream from a previously prepared buffer.

Example

let buffer = mmap_file "bigfile.iso" in
let in_stream = Bytream.In.of_buffer buffer in
(* ... *)
Sourceval of_string : string -> t

of_string string

Same as of_buffer, but for string value.

  • parameter string

    The string will be copied to transform it into the buffer value.

Sourceval of_channel : ?io_buffer_size:int -> in_channel -> t

of_channel ?io_buffer_size ic

Construct an incoming byte stream from ic using the In_channel.input_bigarray function to get chunks from the source of the channel.

  • parameter ?buffer_size

    by default is 4096 bytes

Example pattern for your modules

module Tar_archive = struct
  (* ... *)
  let of_channel ic =
    Bytream.In.of_channel ic
    |> Tar_reader.input_archive in_stream

Buffering mechanism

The section explains how to work with incoming byte stream's buffering mechanism for effective processing bytes massive.

You should understand, an incoming byte stream not copying incoming chunks gotten from reader. The stream use it until not be gotten new chunk when it be needed.

Sourceval available_to_read : t -> int

available_to_read in_stream

  • returns

    remaining bytes of current chunk that available to read.

Sourceval consume_bytes : t -> int -> unit

consume_bytes in_stream len

Consume len bytes from incoming bytes stream with offset shifting.

  • raises End_of_file

    if you try consume more bytes than a source provide.

Sourceval ensure_buffer : t -> int -> buffer

ensure_chunk in_stream len

  • returns

    buffer value that guarantee have len bytes.

See. ensure_chunk function that have most fast allocation.

Sourceval ensure_chunk : t -> int -> chunk

ensure_chunk in_stream len

Same as the make function, but returns chunk.

Note. Maybe be most performance than ensure_buffer because buffer subbing is more expressive. Recommended to use.

Sourceval position : t -> int

position in_stream

  • returns

    Total bytes number read from incoming byte stream.

Input

Sourceval input : t -> buffer -> int -> int -> int

input in_stream buffer off len

Input streams's bytes into buffer and return actually batched bytes. Similar to Stdlib.input channel's function.

Note. Try use ensure_ functions instead it for escape unnecessary allocations.

  • parameter off

    The is buffer's offset.

  • parameter len

    The is buffer's length.

Sourceval input_bytes : t -> bytes -> int -> int -> int

input_bytes in_stream bytes off len s

Same as the Input function, but working with bytes value.

Sourceval really_input : t -> buffer -> int -> int -> unit

really_input in_stream buffer off len input streams's bytes into buffer and grantees fill it.

Example

let input_request in_stream =
  (* ... *)
  Bytream.In.really_input in_stream payload_buffer 0 payload_size
  • raises End_of_file

    if in_stream not have enough bytes for input

Sourceval really_input_bytes : t -> bytes -> int -> int -> unit

really_input_bytes in_stream bytes off len same as really_input but for bytes.

Inputting substrings

Sourceval input_string : t -> int -> string

input_string in_stream len

Input len-sized bytes value from incoming byte stream.

Sourceval input_while : ?max:int -> (char -> bool) -> t -> string

input_while ?max p in_stream

Input byte while p on the byte returns true.

  • parameter ?max

    determined the maximum length of the input string

Sourceval input_while' : max:int -> (char -> bool) -> t -> string

input_while ~max p in_stream

Same as the input_while function, but consuming remaining bytes until max. Ideal for input fixed-size C string field some binary formats.

Input combinators

Sourceval take : int -> (t -> 'a) -> t -> 'a list

take n input_value in_stream

Call input_value n-times and save the function's results in a list.

Sourceval with_size : (t -> 'a) -> t -> 'a * int

with_size f in_stream

  • returns

    (value, size)

A value and a number of bytes were inputted by the f function.

Example

let section, section_size = Bytream.In.with_size input_section in_stream in
(* ... *)

Inputting integer values

Input bytes and decode them into integer values.

Sourceval input_char : t -> char

input_char in_stream

Sourceval input_int8 : t -> int

input_int8 in_stream

Sourceval input_uint8 : t -> int

input_uint8 in_stream

Sourceval input_byte : t -> int

input_byte in_stream

Sourceval input_int16_be : t -> int

input_int16_be in_stream

Sourceval input_int16_ne : t -> int

input_int16_ne in_stream

Sourceval input_int16_le : t -> int

input_int16_le in_stream

Sourceval input_uint16_be : t -> int

input_uint16_be in_stream

Sourceval input_uint16_ne : t -> int

input_uint16_ne in_stream

Sourceval input_uint16_le : t -> int

input_uint16_le in_stream

Sourceval input_int32_be : t -> int32

input_int32_be in_stream

Sourceval input_int32_ne : t -> int32

input_int32_ne in_stream

Sourceval input_int32_le : t -> int32

input_int32_le in_stream

Sourceval input_int64_be : t -> int64

input_int64_be in_stream

Sourceval input_int64_ne : t -> int64

input_int64_ne in_stream

Sourceval input_int64_le : t -> int64

input_int64_le in_stream