Binary data: buffer
A buffer holds bytes for building and reading binary data: file formats, network packets, anything that isn’t text. You build one with write, and read one with read, which goes forward from a position.
w: buffer = buffer()
w.write(0x01) // one byte
w.write(1000, 4) // an int in 4 bytes
w.write("Ann") // the bytes of a str
w.write(0) // a 0 byte to end it
print(w)
r: buffer = buffer(convert(w))
kind: byte = r.read()
size: int = r.read(4)
name: str = r.until(0)
print("${kind} ${size} ${name}")Output:
buffer(01 e8 03 00 00 41 6e 6e 00)
1 1000 Annprint shows a buffer’s bytes in hex.
Making one
buffer() | an empty buffer |
buffer(bytes) | a buffer with the bytes of a [byte] or a str |
convert | a [byte] or str into a buffer, and a buffer back into either: data: [byte] = convert(buf) |
file(path).read() | a file’s bytes, when the result goes into a buffer: b: buffer = file("image.png").read() |
file(path).write(buf) writes a buffer’s bytes to a file.
A buffer stores each byte in one byte of memory. A [byte] list takes 8 bytes of memory for each element, so a buffer is the better choice for large amounts of binary data.
Writing
write adds to the end of the buffer:
| Call | Adds |
|---|---|
b.write(x) with a byte (or a number 0 to 255) | that byte |
b.write(bytes) with a [byte] | its bytes |
b.write(text) with a str | its bytes (UTF-8), without anything after them |
b.write(other) with a buffer | its bytes |
b.write(f) with a float | its 8 bytes (IEEE 754) |
b.write(f, 4) with a float | the nearest 32-bit float, in 4 bytes (b.write(f, 8) is the same as b.write(f)) |
b.write(n, size) with an int | n in size bytes, 1 to 8 |
An int always needs a size, because a format says how many bytes a number takes: b.write(n) with an int is a compile error. The number must fit in that many bytes, either as a signed or an unsigned number: in 2 bytes, -32768 up to 65535. Otherwise, error 13 is raised.
Reading
Reading takes bytes at the position and moves the position past them. What you get depends on the type it goes into:
b: buffer = buffer([7, 1, 0, 104, 105, 254, 255])
x: byte = b.read() // one byte
n: int = b.read(2) // a 2-byte number
s: str = b.read(2) // 2 bytes as text
m: int = b.signed(2) // a 2-byte number that can be negative
print("${x} ${n} ${s} ${m}")Output:
7 1 hi -2| Call | Into | Reads |
|---|---|---|
b.read() | byte | one byte |
b.read() | [byte] or str | everything that is left |
b.read(n) | [byte] or str | n bytes |
b.read(n) | int | an unsigned number of n bytes (1 to 8) |
b.read(8) | float | a float (IEEE 754) |
b.read(4) | float | a 32-bit float, as a float |
b.signed(n) | int | a signed number of n bytes (1 to 8) |
b.until(d) | [byte] or str | the bytes before the next byte d; the position moves past d |
Where nothing says which type, read() gives a byte, and read(n) and until(d) give a [byte]. An 8-byte number is the whole int, so read(8) and signed(8) are the same.
Reading more than is left raises error 13, which try can catch. That way a program can reject a broken file or packet instead of stopping:
b: buffer = buffer([1, 2])
err: error = nil
try err {
n: int = b.read(4)
}
print(err)Output:
error 13: cannot read 4 bytes at position 0: the buffer has 2The position
| Call | Does |
|---|---|
b.position() | gives the position: how many bytes were read |
b.remaining() | gives how many bytes are left after it |
b.skip(n) | moves it n bytes on, without reading them |
b.reset() | moves it back to the start |
len(b) | gives the number of bytes in the buffer |
b: buffer = buffer("header:data")
b.skip(7)
print(b.position())
print(b.remaining())
rest: str = b.read()
print(rest)
b.reset()
print(b.position())Output:
7
4
data
0Writing always adds at the end, and doesn’t move the position.
Byte order
Numbers are written and read with the lowest byte first (little-endian), as most file formats and processors store them. b.big() switches a buffer to the highest byte first (big-endian, or “network order”), which many network protocols use. b.little() switches it back:
b: buffer = buffer()
b.write(258, 2)
b.big()
b.write(258, 2)
print(b)Output:
buffer(02 01 01 02)The byte order belongs to the buffer. It applies to everything written and read with it from then on.
Buffers are shared
Like a list, a buffer is a reference: assigning it or passing it to a function doesn’t copy it, so a function can fill in a buffer it is given. b.write(b) adds a copy of its own bytes. An obj field can be a buffer, with buffer() as its default: each object gets its own.
Errors
Buffer errors have code 13:
| Message | When |
|---|---|
cannot read 4 bytes at position 10: the buffer has 12 | reading (or skipping) more than is left |
no byte 0 after position 3: the buffer has 8 | until doesn’t find its byte |
300 does not fit in 1 byte | write(n, size) with a number too big for the size |
an int takes 1 to 8 bytes, not 9 | a size that isn’t 1 to 8 (with a fixed number, the compiler says so) |
Not available yet
- Reading or changing a byte at a given place (
b[i]): useskipandreset, orconvertto a[byte]. - Comparing buffers with
==, and buffers in JSON.