package bytream

  1. Overview
  2. Docs

Module Bytream.OutSource

Outgoing bytes stream module for streaming bytes to a sink in chunks.

Sourcetype t

Outgoing 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

Sourceval make : ?buffer_size:int -> (chunk -> unit) -> t

make ?buffer_size writer

Construct an outgoing byte stream using the writer function to output chunks to a bytes sink.

  • parameter writer

    Function that accepting a chunk of buffer that always has an offset of zero and a variable length. Therefore, for you, it is enough to match only the buffer and length fields.

    let writer (~buffer, ~length, ..) = (* ... *)
  • parameter ?buffer_size

    by default is 4096 bytes

Example

This example illustrates the basic concept of chunking.

(* Queue as a byte chunk sink. *)
let queue = Queue.create () in

(* Writer function that outputs chunks of text to the sink. *)
let writer (~buffer, ~length:len, ..) =
  Queue.add Bstr.(sub_string ~off:0 ~len buffer) queue
in

Bytream.Out.make writer
Sourceval of_channel : ?io_buffer_size:int -> out_channel -> t

of_channel ?io_buffer_size oc make an outgoing stream from the channel.

Note. You can disable internal buffering for the channels to get the expected behavior: Out_channel.set_buffered oc false v.

Withing into

Sourceval with_into_buffer : Buffer.t -> (t -> unit) -> unit

with_into_string f make a stream that outgoing to buffer.

Sourceval with_into_string : (t -> unit) -> string

with_into_string f make a stream that outgoing to buffer and returns string value.

Buffering mechanism

Sourceval available_to_write : t -> int

available_to_write out_stream

  • returns

    Number of bytes that have not been written to the internal buffer.

Sourceval shift : t -> int -> unit

shift out_stream n

Shift the buffer's offset by n bytes to account for the written bytes.

  • raises Shifted_beyond_buffer

    if n is more than available space to write

Sourceval ensure_writable_bytes_at : t -> int -> int

writable_guard_bytes_at out_stream len

Guarantees that the buffer has enough space to write len bytes into it.

  • returns

    the buffer's offset

Example

let write_hello_record out_stream hello =
  let off = Out.writable_guard_bytes_at out_stream 6 in
  Out.output_byte out_stream 5;
  Out.output_string out_stream "hello"

Flushing

Sourceval flush : t -> unit

flush out_stream flush outgoing stream to sink.

Sourceval with_flush : t -> (unit -> unit) -> unit

with_flush out_stream f same as flush but with with-function.

Output

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

output out_stream buffer off len

Output the buffer into outgoing byte stream.

Sourceval output_string : t -> string -> unit

output out_stream string

Output the string into outgoing byte stream.

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

output out_stream buffer off len

Output the buffer into outgoing byte stream with flushing.

Integer values outputting

Sourceval output_char : t -> char -> unit
Sourceval output_byte : t -> int -> unit
Sourceval output_int8 : t -> int -> unit
Sourceval output_uint8 : t -> int -> unit
Sourceval output_int16_be : t -> int -> unit
Sourceval output_int16_le : t -> int -> unit
Sourceval output_int16_ne : t -> int -> unit
Sourceval output_uint16_be : t -> int -> unit
Sourceval output_uint16_le : t -> int -> unit
Sourceval output_uint16_ne : t -> int -> unit
Sourceval output_int32_be : t -> int32 -> unit
Sourceval output_int32_le : t -> int32 -> unit
Sourceval output_int32_ne : t -> int32 -> unit
Sourceval output_int64_be : t -> int64 -> unit
Sourceval output_int64_le : t -> int64 -> unit
Sourceval output_int64_ne : t -> int64 -> unit