Page
Library
Module
Module type
Parameter
Class
Class type
Source
Bytream.InSourceIncoming byte stream module for processing byte sources, using chunk buffers when needed, with minimized memory allocations.
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.
A byte chunk is a non-empty consecutive range of bytes in a buffer value.
The section explains how you can create a new incoming byte stream.
make ?overlap_size reader
Construct incoming byte stream from reader.
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 readerof_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
(* ... *)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.
Example pattern for your modules
module Tar_archive = struct
(* ... *)
let of_channel ic =
Bytream.In.of_channel ic
|> Tar_reader.input_archive in_streamThe 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.
consume_bytes in_stream len
Consume len bytes from incoming bytes stream with offset shifting.
ensure_chunk in_stream len
See. ensure_chunk function that have most fast allocation.
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.
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.
input_bytes in_stream bytes off len s
Same as the Input function, but working with bytes value.
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_sizereally_input_bytes in_stream bytes off len same as really_input but for bytes.
input_string in_stream len
Input len-sized bytes value from incoming byte stream.
input_while ?max p in_stream
Input byte while p on the byte returns true.
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.
take n input_value in_stream
Call input_value n-times and save the function's results in a list.
with_size f in_stream
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
(* ... *)Input bytes and decode them into integer values.