Reader¶
Store network data and provide a parsing interface.
Reader
provides a way to extract typed data from a sequence of
bytes. The Reader
manages the underlying data structures to
provide a read cursor over a contiguous sequence of bytes. It is
useful for decoding data that is received over a network or stored
in a file. Chunk of bytes are added to the Reader
using the
append
method, and typed data is extracted using the getter
methods.
For example, suppose we have a UDP-based network data protocol where messages consist of the following:
list_size
- the number of items in the following list of items as a big-endian 32-bit integer- zero or more items of the following data:
- a big-endian 64-bit floating point number
- a string that starts with a big-endian 32-bit integer that specifies the length of the string, followed by a number of bytes that represent the string
A message would be something like this:
The following program uses a Reader
to decode a message of
this type and print them:
use "buffered"
use "collections"
class Notify is InputNotify
let _env: Env
new create(env: Env) =>
_env = env
fun ref apply(data: Array[U8] iso) =>
let rb = Reader
rb.append(consume data)
try
while true do
let len = rb.i32_be()?
let items = rb.i32_be()?.usize()
for range in Range(0, items) do
let f = rb.f32_be()?
let str_len = rb.i32_be()?.usize()
let str = String.from_array(rb.block(str_len)?)
_env.out.print("[(" + f.string() + "), (" + str + ")]")
end
end
end
actor Main
new create(env: Env) =>
env.input(recover Notify(env) end, 1024)
Constructors¶
create¶
Returns¶
- Reader iso^
Public Functions¶
size¶
Return the number of available bytes.
Returns¶
- USize val
clear¶
Discard all pending data.
Returns¶
- None val
append¶
Add a chunk of data.
Parameters¶
Returns¶
- None val
skip¶
Skip n bytes.
Parameters¶
- n: USize val
Returns¶
- None val ?
block¶
Return a block as a contiguous chunk of memory.
Will throw an error if you request a block larger than what is currently
stored in the Reader
.
Parameters¶
- len: USize val
Returns¶
read_until¶
Find the first occurrence of the separator and return the block of bytes before its position. The separator is not included in the returned array, but it is removed from the buffer. To read a line of text, prefer line() that handles \n and \r\n.
Parameters¶
- separator: U8 val
Returns¶
line¶
Return a \n or \r\n terminated line as a string. By default the newline is not
included in the returned string, but it is removed from the buffer.
Set keep_line_breaks
to true
to keep the line breaks in the returned line.
Parameters¶
- keep_line_breaks: Bool val = false
Returns¶
- String iso^ ?
u8¶
Get a U8. Raise an error if there isn't enough data.
Returns¶
- U8 val ?
i8¶
Get an I8.
Returns¶
- I8 val ?
u16_be¶
Get a big-endian U16.
Returns¶
- U16 val ?
u16_le¶
Get a little-endian U16.
Returns¶
- U16 val ?
i16_be¶
Get a big-endian I16.
Returns¶
- I16 val ?
i16_le¶
Get a little-endian I16.
Returns¶
- I16 val ?
u32_be¶
Get a big-endian U32.
Returns¶
- U32 val ?
u32_le¶
Get a little-endian U32.
Returns¶
- U32 val ?
i32_be¶
Get a big-endian I32.
Returns¶
- I32 val ?
i32_le¶
Get a little-endian I32.
Returns¶
- I32 val ?
u64_be¶
Get a big-endian U64.
Returns¶
- U64 val ?
u64_le¶
Get a little-endian U64.
Returns¶
- U64 val ?
i64_be¶
Get a big-endian I64.
Returns¶
- I64 val ?
i64_le¶
Get a little-endian I64.
Returns¶
- I64 val ?
u128_be¶
Get a big-endian U128.
Returns¶
- U128 val ?
u128_le¶
Get a little-endian U128.
Returns¶
- U128 val ?
i128_be¶
Get a big-endian I129.
Returns¶
- I128 val ?
i128_le¶
Get a little-endian I128.
Returns¶
- I128 val ?
f32_be¶
Get a big-endian F32.
Returns¶
- F32 val ?
f32_le¶
Get a little-endian F32.
Returns¶
- F32 val ?
f64_be¶
Get a big-endian F64.
Returns¶
- F64 val ?
f64_le¶
Get a little-endian F64.
Returns¶
- F64 val ?
peek_u8¶
Peek at a U8 at the given offset. Raise an error if there isn't enough data.
Parameters¶
- offset: USize val = 0
Returns¶
- U8 val ?
peek_i8¶
Peek at an I8.
Parameters¶
- offset: USize val = 0
Returns¶
- I8 val ?
peek_u16_be¶
Peek at a big-endian U16.
Parameters¶
- offset: USize val = 0
Returns¶
- U16 val ?
peek_u16_le¶
Peek at a little-endian U16.
Parameters¶
- offset: USize val = 0
Returns¶
- U16 val ?
peek_i16_be¶
Peek at a big-endian I16.
Parameters¶
- offset: USize val = 0
Returns¶
- I16 val ?
peek_i16_le¶
Peek at a little-endian I16.
Parameters¶
- offset: USize val = 0
Returns¶
- I16 val ?
peek_u32_be¶
Peek at a big-endian U32.
Parameters¶
- offset: USize val = 0
Returns¶
- U32 val ?
peek_u32_le¶
Peek at a little-endian U32.
Parameters¶
- offset: USize val = 0
Returns¶
- U32 val ?
peek_i32_be¶
Peek at a big-endian I32.
Parameters¶
- offset: USize val = 0
Returns¶
- I32 val ?
peek_i32_le¶
Peek at a little-endian I32.
Parameters¶
- offset: USize val = 0
Returns¶
- I32 val ?
peek_u64_be¶
Peek at a big-endian U64.
Parameters¶
- offset: USize val = 0
Returns¶
- U64 val ?
peek_u64_le¶
Peek at a little-endian U64.
Parameters¶
- offset: USize val = 0
Returns¶
- U64 val ?
peek_i64_be¶
Peek at a big-endian I64.
Parameters¶
- offset: USize val = 0
Returns¶
- I64 val ?
peek_i64_le¶
Peek at a little-endian I64.
Parameters¶
- offset: USize val = 0
Returns¶
- I64 val ?
peek_u128_be¶
Peek at a big-endian U128.
Parameters¶
- offset: USize val = 0
Returns¶
- U128 val ?
peek_u128_le¶
Peek at a little-endian U128.
Parameters¶
- offset: USize val = 0
Returns¶
- U128 val ?
peek_i128_be¶
Peek at a big-endian I129.
Parameters¶
- offset: USize val = 0
Returns¶
- I128 val ?
peek_i128_le¶
Peek at a little-endian I128.
Parameters¶
- offset: USize val = 0
Returns¶
- I128 val ?
peek_f32_be¶
Peek at a big-endian F32.
Parameters¶
- offset: USize val = 0
Returns¶
- F32 val ?
peek_f32_le¶
Peek at a little-endian F32.
Parameters¶
- offset: USize val = 0
Returns¶
- F32 val ?
peek_f64_be¶
Peek at a big-endian F64.
Parameters¶
- offset: USize val = 0
Returns¶
- F64 val ?
peek_f64_le¶
Peek at a little-endian F64.
Parameters¶
- offset: USize val = 0
Returns¶
- F64 val ?