binary

package binary
Read and write binary data with any Reader and Writer

Concerns

  • binary: Read and write binary data with any Reader and Writer
    • binary: Read and write binary data with any Reader and Writer
    • serialization: Serialize and deserialize Nit objects to binary streams

binary::binary

module binary

Read and write binary data with any Reader and Writer

var w = new FileWriter.open("/tmp/data.bin")
w.write "hello"
w.write_int64 123456789
w.write_byte 3u8
w.write_float 1.25
w.write_double 1.234567
w.write_bits(true, false, true)
assert w.last_error == null
w.close

var r = new FileReader.open("/tmp/data.bin")
assert r.read(5) == "hello"
assert r.read_int64 == 123456789
assert r.read_byte == 3u8
assert r.read_float == 1.25
assert r.read_double == 1.234567

var bits = r.read_bits
assert bits[0] and not bits[1] and bits[2]

assert r.last_error == null
r.close
Redefines
  • Writer: A Stream that can be written to
  • Reader: A Stream that can be read from
  • Float: Native floating point numbers.
  • Int: Native integer numbers.

binary::serialization

module serialization

Serialize and deserialize Nit objects to binary streams

The serialized data format uses a dictionary structure similar to BSON:

object = 0x01                    # null
       | 0x02 id attributes      # New object
       | 0x03 id                 # Ref to object
       | 0x04 int64              # Int
       | 0x05 int8               # Bool (int8 != 0)
       | 0x06 utf8 byte sequence # Char
       | 0x07 double(64 bits)    # Float
       | 0x08 block              # String
       | 0x09 block              # CString
       | 0x0A flat_array;        # Array[nullable Object]

block = int64 int8*;
cstring = int8* 0x00;
id = int64;

attributes = attribute* 0x00;
attribute = cstring object;
Redefines
  • Serializable: Instances of this class can be passed to Serializer::serialize
  • Sys: The main class of the program.
  • Map: Maps are associative collections: key -> item.
  • SimpleCollection: Items can be added to these collections.
  • Bool: Native Booleans.
  • CString: C string char *
  • Char: Native characters.
  • Float: Native floating point numbers.
  • Int: Native integer numbers.
  • Text: High-level abstraction for all text representations