package bytesrw
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha512=388858b0db210a62a16f56655746fdfadbc64b22c2abb5ed5a12b2872e4f8c34f045cdb953a5dda9b92f0003c7f9f34d70fa5b5bb19fd32fb6121bbaeb7ceba0
doc/index.html
Bytesrw v0.3.0
Bytesrw extends the OCaml Bytes module with composable, memory efficient, byte stream readers and writers compatible with effect based concurrency.
Except for byte slice life-times, these abstractions intentionally separate away ressource management and the specifics of reading and writing bytes.
Manuals
The following manuals are available:
- The quick start should do so.
- The tutorial is a conceptual overview of byte stream readers and writers.
- The Bytesrw cookbook has a few conventions and byte stream recipes.
- The design notes explains design choices made by the library.
Library bytesrw
This library has the base definition of byte stream reader and writers as an extension of the Stdlib.Bytes module.
BytesrwExtendedStdlib.Bytesmodule. Open to use it.
Bytesrw.Bytes.SliceByte slices.Bytesrw.Bytes.StreamByte streams.Bytesrw.Bytes.ReaderByte stream readers.Bytesrw.Bytes.WriterByte stream writers.
The following modules rely only on the Stdlib:
Bytesrw_utfUTF streams.Bytesrw_hexHexadecimal tools.
Libraries bytesrw.{blake3,crypto,md,tls,unix,xxhash,zlib,zstd}
Each of these modules lives in its corresponding library. Compression and hashing libraries depend on their canonical C library and encryption on Mbed TLS. If a library is missing, install the corresponding conf opam package and reinstall bytesrw.
Bytesrw_blake3Bytesrw_cryptoBytesrw_mdBytesrw_sysrandomCryptographically secure pseudorandom byte streams and entropy.Bytesrw_tlsBytesrw_unixBlockingUnixfile descriptor byte stream readers and writers.Bytesrw_xxhashBytesrw_zlibBytesrw_zstd
Quick start
This example compresses standard input to standard output with zstd using either a compressing byte stream reader (pull) or a compressing byte stream writer (push).
cat << 'EOF' > quickstart.ml
open Bytesrw
let stdio_compress_reads () =
try
let stdin = Bytes.Reader.of_in_channel In_channel.stdin in
let stdout = Bytes.Writer.of_out_channel Out_channel.stdout in
let params = Bytesrw_zstd.Cctx_params.make ~checksum:true () in
let zstdr = Bytesrw_zstd.compress_reads ~params () stdin in
Bytes.Writer.write_reader ~eod:true stdout zstdr;
Ok ()
with
| Bytes.Stream.Error e -> Bytes.Stream.error_to_result e
| Sys_error e -> Error e
let stdio_compress_writes () =
try
let stdin = Bytes.Reader.of_in_channel In_channel.stdin in
let stdout = Bytes.Writer.of_out_channel Out_channel.stdout in
let params = Bytesrw_zstd.Cctx_params.make ~checksum:true () in
let zstdw = Bytesrw_zstd.compress_writes ~params () ~eod:true stdout in
Bytes.Writer.write_reader ~eod:true zstdw stdin;
Ok ()
with
| Bytes.Stream.Error e -> Bytes.Stream.error_to_result e
| Sys_error e -> Error e
let main () =
Result.fold ~ok:(Fun.const 0) ~error:(fun e -> prerr_endline e; 1) @@
if Array.exists (String.equal "-w") Sys.argv
then stdio_compress_writes ()
else stdio_compress_reads ()
let () = if !Sys.interactive then () else exit (main ())
EOFIt can be compiled and used with:
ocamlfind ocamlopt -package bytesrw,bytesrw.zstd -linkpkg quickstart.ml
./a.out < quickstart.ml | zstd -dThe tutorial is a short conceptual overview of byte stream readers and writers. The cookbook has a few more tips and code snippets.