A Stream that can be written to

Introduced properties

abstract fun is_writable: Bool

core :: Writer :: is_writable

Can the stream be used to write
fun serialize_msgpack(value: nullable Serializable, plain: nullable Bool)

core :: Writer :: serialize_msgpack

Serialize value in MessagePack format
abstract fun write(s: Text)

core :: Writer :: write

Write a string
fun write_bits(bits: Bool...)

core :: Writer :: write_bits

Write up to 8 Bool in a byte
fun write_block(text: Text)

core :: Writer :: write_block

Write the length as a 64 bits integer, then the content of text
fun write_bool(value: Bool)

core :: Writer :: write_bool

Write a boolean value on a byte, using 0 for false and 1 for true
abstract fun write_byte(value: Int)

core :: Writer :: write_byte

Write a single byte
fun write_bytes(s: Bytes)

core :: Writer :: write_bytes

Write bytes from s
abstract fun write_bytes_from_cstring(ns: CString, len: Int)

core :: Writer :: write_bytes_from_cstring

Write len bytes from ns
fun write_char(c: Char)

core :: Writer :: write_char

Write a single char
fun write_double(value: Float)

core :: Writer :: write_double

Write a floating point value on 64 bits
fun write_float(value: Float)

core :: Writer :: write_float

Write a floating point value on 32 bits
fun write_int64(value: Int)

core :: Writer :: write_int64

Write value as a signed integer on 64 bits
fun write_msgpack_array(len: Int)

core :: Writer :: write_msgpack_array

Write an array header for len items in the shortest possible MessagePack array format
fun write_msgpack_array16(len: Int)

core :: Writer :: write_msgpack_array16

Write an array header for len items, max of 0xFFFF items
fun write_msgpack_array32(len: Int)

core :: Writer :: write_msgpack_array32

Write an array header for len items, max of 0xFFFF_FFFF items
fun write_msgpack_bin(data: Bytes)

core :: Writer :: write_msgpack_bin

Write data in the shortest possible MessagePack bin format
fun write_msgpack_bin16(data: Bytes)

core :: Writer :: write_msgpack_bin16

Write data in bin16 format, max of 0xFFFF bytes
fun write_msgpack_bin32(data: Bytes)

core :: Writer :: write_msgpack_bin32

Write data in bin32 format, max of 0xFFFF_FFFF bytes
fun write_msgpack_bin8(data: Bytes)

core :: Writer :: write_msgpack_bin8

Write data in bin8 format, max of 0xFF bytes
fun write_msgpack_bool(bool: Bool)

core :: Writer :: write_msgpack_bool

Write bool in MessagePack format
fun write_msgpack_double(value: Float)

core :: Writer :: write_msgpack_double

Write value as a MessagePack double
fun write_msgpack_ext(typ: Int, bytes: Bytes)

core :: Writer :: write_msgpack_ext

Write an application-specific extension for typ and bytes in the shortest possible MessagePack ext format
fun write_msgpack_ext16(typ: Int, len: Int)

core :: Writer :: write_msgpack_ext16

Write the header for an application-specific extension of len data bytes
fun write_msgpack_ext32(typ: Int, len: Int)

core :: Writer :: write_msgpack_ext32

Write the header for an application-specific extension of len data bytes
fun write_msgpack_ext8(typ: Int, len: Int)

core :: Writer :: write_msgpack_ext8

Write the header for an application-specific extension of len data bytes
fun write_msgpack_fixarray(len: Int)

core :: Writer :: write_msgpack_fixarray

Write an array header for len items, max of 0x0F items
fun write_msgpack_fixext1(typ: Int)

core :: Writer :: write_msgpack_fixext1

Write the header for an application-specific extension of one data byte
fun write_msgpack_fixext16(typ: Int)

core :: Writer :: write_msgpack_fixext16

Write the header for an application-specific extension of 16 data bytes
fun write_msgpack_fixext2(typ: Int)

core :: Writer :: write_msgpack_fixext2

Write the header for an application-specific extension of two data bytes
fun write_msgpack_fixext4(typ: Int)

core :: Writer :: write_msgpack_fixext4

Write the header for an application-specific extension of 4 data bytes
fun write_msgpack_fixext8(typ: Int)

core :: Writer :: write_msgpack_fixext8

Write the header for an application-specific extension of 8 data bytes
fun write_msgpack_fixint(value: Int)

core :: Writer :: write_msgpack_fixint

Write value as a single byte with metadata
fun write_msgpack_fixmap(len: Int)

core :: Writer :: write_msgpack_fixmap

Write a map header for len key/value pairs, max of 0x0F pairs
fun write_msgpack_fixstr(text: Text)

core :: Writer :: write_msgpack_fixstr

Write text in fixstr format, max of 0x1F bytes
fun write_msgpack_float(value: Float)

core :: Writer :: write_msgpack_float

Write value as a MessagePack float (losing precision)
fun write_msgpack_int(value: Int)

core :: Writer :: write_msgpack_int

Write the integer value either as the shortest possible MessagePack int
fun write_msgpack_int16(value: Int)

core :: Writer :: write_msgpack_int16

Write value over two signed bytes, following 1 metadata byte
fun write_msgpack_int32(value: Int)

core :: Writer :: write_msgpack_int32

Write value over 4 signed bytes, following 1 metadata byte
fun write_msgpack_int64(value: Int)

core :: Writer :: write_msgpack_int64

Write value over 8 signed bytes, following 1 metadata byte
fun write_msgpack_int8(value: Int)

core :: Writer :: write_msgpack_int8

Write value over one signed byte, following 1 metadata byte
fun write_msgpack_map(len: Int)

core :: Writer :: write_msgpack_map

Write a map header for len keys/value pairs in the shortest possible MessagePack map format
fun write_msgpack_map16(len: Int)

core :: Writer :: write_msgpack_map16

Write a map header for len key/value pairs, max of 0xFFFF pairs
fun write_msgpack_map32(len: Int)

core :: Writer :: write_msgpack_map32

Write a map header for len key/value pairs, max of 0xFFFF_FFFF pairs
fun write_msgpack_null

core :: Writer :: write_msgpack_null

Write null, or nil, in MessagePack format
fun write_msgpack_str(text: Text)

core :: Writer :: write_msgpack_str

Write text in the shortest possible MessagePack format
fun write_msgpack_str16(text: Text)

core :: Writer :: write_msgpack_str16

Write text in str16 format, max of 0xFFFF bytes
fun write_msgpack_str32(text: Text)

core :: Writer :: write_msgpack_str32

Write text in str32 format, max of 0xFFFF_FFFF bytes
fun write_msgpack_str8(text: Text)

core :: Writer :: write_msgpack_str8

Write text in str8 format, max of 0xFF bytes
fun write_msgpack_uint16(value: Int)

core :: Writer :: write_msgpack_uint16

Write value over two unsigned bytes, following 1 metadata byte
fun write_msgpack_uint32(value: Int)

core :: Writer :: write_msgpack_uint32

Write value over 4 unsigned bytes, following 1 metadata byte
fun write_msgpack_uint64(value: Int)

core :: Writer :: write_msgpack_uint64

Write value over 8 unsigned bytes, following 1 metadata byte
fun write_msgpack_uint8(value: Int)

core :: Writer :: write_msgpack_uint8

Write value over one unsigned byte, following 1 metadata byte
fun write_string(text: Text)

core :: Writer :: write_string

Write text as a null terminated string

Redefined properties

redef type SELF: Writer

core $ Writer :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
fun big_endian: Bool

binary :: BinaryStream :: big_endian

Use the big-endian convention? otherwise use little-endian.
fun big_endian=(big_endian: Bool)

binary :: BinaryStream :: big_endian=

Use the big-endian convention? otherwise use little-endian.
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
abstract fun close

core :: Stream :: close

close the stream
fun codec: Codec

core :: Stream :: codec

Codec used to transform raw data to text
fun codec=(c: Codec)

core :: Stream :: codec=

Change the codec for this stream.
fun finish

core :: Stream :: finish

Post-work hook.
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
abstract fun is_writable: Bool

core :: Writer :: is_writable

Can the stream be used to write
fun last_error: nullable IOError

core :: Stream :: last_error

Error produced by the file stream
protected fun last_error=(last_error: nullable IOError)

core :: Stream :: last_error=

Error produced by the file stream
protected fun lookahead: CString

core :: Stream :: lookahead

Lookahead buffer for codecs
protected fun lookahead=(lookahead: CString)

core :: Stream :: lookahead=

Lookahead buffer for codecs
protected fun lookahead_capacity: Int

core :: Stream :: lookahead_capacity

Capacity of the lookahead
protected fun lookahead_capacity=(lookahead_capacity: Int)

core :: Stream :: lookahead_capacity=

Capacity of the lookahead
protected fun lookahead_length: Int

core :: Stream :: lookahead_length

Current occupation of the lookahead
protected fun lookahead_length=(lookahead_length: Int)

core :: Stream :: lookahead_length=

Current occupation of the lookahead
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun serialize_msgpack(value: nullable Serializable, plain: nullable Bool)

core :: Writer :: serialize_msgpack

Serialize value in MessagePack format
protected fun set_codec(codec: Codec)

core :: Stream :: set_codec

Codec used to transform raw data to text
fun start

core :: Stream :: start

Pre-work hook.
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
abstract fun write(s: Text)

core :: Writer :: write

Write a string
fun write_bits(bits: Bool...)

core :: Writer :: write_bits

Write up to 8 Bool in a byte
fun write_block(text: Text)

core :: Writer :: write_block

Write the length as a 64 bits integer, then the content of text
fun write_bool(value: Bool)

core :: Writer :: write_bool

Write a boolean value on a byte, using 0 for false and 1 for true
protected fun write_buffer: CString

core :: Stream :: write_buffer

Buffer for writing data to a stream
protected fun write_buffer=(write_buffer: CString)

core :: Stream :: write_buffer=

Buffer for writing data to a stream
abstract fun write_byte(value: Int)

core :: Writer :: write_byte

Write a single byte
fun write_bytes(s: Bytes)

core :: Writer :: write_bytes

Write bytes from s
abstract fun write_bytes_from_cstring(ns: CString, len: Int)

core :: Writer :: write_bytes_from_cstring

Write len bytes from ns
fun write_char(c: Char)

core :: Writer :: write_char

Write a single char
fun write_double(value: Float)

core :: Writer :: write_double

Write a floating point value on 64 bits
fun write_float(value: Float)

core :: Writer :: write_float

Write a floating point value on 32 bits
fun write_int64(value: Int)

core :: Writer :: write_int64

Write value as a signed integer on 64 bits
fun write_msgpack_array(len: Int)

core :: Writer :: write_msgpack_array

Write an array header for len items in the shortest possible MessagePack array format
fun write_msgpack_array16(len: Int)

core :: Writer :: write_msgpack_array16

Write an array header for len items, max of 0xFFFF items
fun write_msgpack_array32(len: Int)

core :: Writer :: write_msgpack_array32

Write an array header for len items, max of 0xFFFF_FFFF items
fun write_msgpack_bin(data: Bytes)

core :: Writer :: write_msgpack_bin

Write data in the shortest possible MessagePack bin format
fun write_msgpack_bin16(data: Bytes)

core :: Writer :: write_msgpack_bin16

Write data in bin16 format, max of 0xFFFF bytes
fun write_msgpack_bin32(data: Bytes)

core :: Writer :: write_msgpack_bin32

Write data in bin32 format, max of 0xFFFF_FFFF bytes
fun write_msgpack_bin8(data: Bytes)

core :: Writer :: write_msgpack_bin8

Write data in bin8 format, max of 0xFF bytes
fun write_msgpack_bool(bool: Bool)

core :: Writer :: write_msgpack_bool

Write bool in MessagePack format
fun write_msgpack_double(value: Float)

core :: Writer :: write_msgpack_double

Write value as a MessagePack double
fun write_msgpack_ext(typ: Int, bytes: Bytes)

core :: Writer :: write_msgpack_ext

Write an application-specific extension for typ and bytes in the shortest possible MessagePack ext format
fun write_msgpack_ext16(typ: Int, len: Int)

core :: Writer :: write_msgpack_ext16

Write the header for an application-specific extension of len data bytes
fun write_msgpack_ext32(typ: Int, len: Int)

core :: Writer :: write_msgpack_ext32

Write the header for an application-specific extension of len data bytes
fun write_msgpack_ext8(typ: Int, len: Int)

core :: Writer :: write_msgpack_ext8

Write the header for an application-specific extension of len data bytes
fun write_msgpack_fixarray(len: Int)

core :: Writer :: write_msgpack_fixarray

Write an array header for len items, max of 0x0F items
fun write_msgpack_fixext1(typ: Int)

core :: Writer :: write_msgpack_fixext1

Write the header for an application-specific extension of one data byte
fun write_msgpack_fixext16(typ: Int)

core :: Writer :: write_msgpack_fixext16

Write the header for an application-specific extension of 16 data bytes
fun write_msgpack_fixext2(typ: Int)

core :: Writer :: write_msgpack_fixext2

Write the header for an application-specific extension of two data bytes
fun write_msgpack_fixext4(typ: Int)

core :: Writer :: write_msgpack_fixext4

Write the header for an application-specific extension of 4 data bytes
fun write_msgpack_fixext8(typ: Int)

core :: Writer :: write_msgpack_fixext8

Write the header for an application-specific extension of 8 data bytes
fun write_msgpack_fixint(value: Int)

core :: Writer :: write_msgpack_fixint

Write value as a single byte with metadata
fun write_msgpack_fixmap(len: Int)

core :: Writer :: write_msgpack_fixmap

Write a map header for len key/value pairs, max of 0x0F pairs
fun write_msgpack_fixstr(text: Text)

core :: Writer :: write_msgpack_fixstr

Write text in fixstr format, max of 0x1F bytes
fun write_msgpack_float(value: Float)

core :: Writer :: write_msgpack_float

Write value as a MessagePack float (losing precision)
fun write_msgpack_int(value: Int)

core :: Writer :: write_msgpack_int

Write the integer value either as the shortest possible MessagePack int
fun write_msgpack_int16(value: Int)

core :: Writer :: write_msgpack_int16

Write value over two signed bytes, following 1 metadata byte
fun write_msgpack_int32(value: Int)

core :: Writer :: write_msgpack_int32

Write value over 4 signed bytes, following 1 metadata byte
fun write_msgpack_int64(value: Int)

core :: Writer :: write_msgpack_int64

Write value over 8 signed bytes, following 1 metadata byte
fun write_msgpack_int8(value: Int)

core :: Writer :: write_msgpack_int8

Write value over one signed byte, following 1 metadata byte
fun write_msgpack_map(len: Int)

core :: Writer :: write_msgpack_map

Write a map header for len keys/value pairs in the shortest possible MessagePack map format
fun write_msgpack_map16(len: Int)

core :: Writer :: write_msgpack_map16

Write a map header for len key/value pairs, max of 0xFFFF pairs
fun write_msgpack_map32(len: Int)

core :: Writer :: write_msgpack_map32

Write a map header for len key/value pairs, max of 0xFFFF_FFFF pairs
fun write_msgpack_null

core :: Writer :: write_msgpack_null

Write null, or nil, in MessagePack format
fun write_msgpack_str(text: Text)

core :: Writer :: write_msgpack_str

Write text in the shortest possible MessagePack format
fun write_msgpack_str16(text: Text)

core :: Writer :: write_msgpack_str16

Write text in str16 format, max of 0xFFFF bytes
fun write_msgpack_str32(text: Text)

core :: Writer :: write_msgpack_str32

Write text in str32 format, max of 0xFFFF_FFFF bytes
fun write_msgpack_str8(text: Text)

core :: Writer :: write_msgpack_str8

Write text in str8 format, max of 0xFF bytes
fun write_msgpack_uint16(value: Int)

core :: Writer :: write_msgpack_uint16

Write value over two unsigned bytes, following 1 metadata byte
fun write_msgpack_uint32(value: Int)

core :: Writer :: write_msgpack_uint32

Write value over 4 unsigned bytes, following 1 metadata byte
fun write_msgpack_uint64(value: Int)

core :: Writer :: write_msgpack_uint64

Write value over 8 unsigned bytes, following 1 metadata byte
fun write_msgpack_uint8(value: Int)

core :: Writer :: write_msgpack_uint8

Write value over one unsigned byte, following 1 metadata byte
fun write_string(text: Text)

core :: Writer :: write_string

Write text as a null terminated string
package_diagram core::Writer Writer binary::BinaryStream BinaryStream core::Writer->binary::BinaryStream core::Stream Stream binary::BinaryStream->core::Stream ...core::Stream ... ...core::Stream->core::Stream core::FileWriter FileWriter core::FileWriter->core::Writer core::Duplex Duplex core::Duplex->core::Writer core::BytesWriter BytesWriter core::BytesWriter->core::Writer core::ProcessWriter ProcessWriter core::ProcessWriter->core::Writer core::WriterProtocol WriterProtocol core::WriterProtocol->core::Writer filter_stream::FilterWriter FilterWriter filter_stream::FilterWriter->core::Writer libevent::Connection Connection libevent::Connection->core::Writer core::Stdout Stdout core::Stdout->core::FileWriter core::Stderr Stderr core::Stderr->core::FileWriter core::Stdout... ... core::Stdout...->core::Stdout core::Stderr... ... core::Stderr...->core::Stderr core::ProcessDuplex ProcessDuplex core::ProcessDuplex->core::Duplex core::ProcessDuplex->core::ProcessWriter core::DuplexProtocol DuplexProtocol core::DuplexProtocol->core::Duplex core::DuplexProtocol->core::WriterProtocol socket::TCPStream TCPStream socket::TCPStream->core::Duplex core::ProcessDuplex... ... core::ProcessDuplex...->core::ProcessDuplex core::DuplexProtocol... ... core::DuplexProtocol...->core::DuplexProtocol socket::TCPStream... ... socket::TCPStream...->socket::TCPStream core::StringWriter StringWriter core::StringWriter->core::BytesWriter core::StringWriter... ... core::StringWriter...->core::StringWriter filter_stream::StreamDemux StreamDemux filter_stream::StreamDemux->filter_stream::FilterWriter filter_stream::StreamDemux... ... filter_stream::StreamDemux...->filter_stream::StreamDemux nitcorn::HTTPConnection HTTPConnection nitcorn::HTTPConnection->libevent::Connection libevent::EchoConnection EchoConnection libevent::EchoConnection->libevent::Connection libevent::TestConnection TestConnection libevent::TestConnection->libevent::Connection nitcorn::HTTPConnection... ... nitcorn::HTTPConnection...->nitcorn::HTTPConnection libevent::EchoConnection... ... libevent::EchoConnection...->libevent::EchoConnection libevent::TestConnection... ... libevent::TestConnection...->libevent::TestConnection

Ancestors

interface Object

core :: Object

The root of the class hierarchy.
abstract class Stream

core :: Stream

Any kind of stream to read/write/both to or from a source

Parents

abstract class BinaryStream

binary :: BinaryStream

A stream of binary data

Children

class BytesWriter

core :: BytesWriter

Write to bytes in memory
class Connection

libevent :: Connection

Spawned to manage a specific connection
abstract class Duplex

core :: Duplex

A Stream that can be written to and read from
class FileWriter

core :: FileWriter

Stream that can write to a File
class ProcessWriter

core :: ProcessWriter

Process on which stdin is writable like a Writer
class WriterProtocol

core :: WriterProtocol

Writer decorator over a write-capable stream

Descendants

class DuplexProtocol

core :: DuplexProtocol

Reader/Writer decorator over a duplex-capable stream
class EchoConnection

libevent :: EchoConnection

Connection echoing data received from clients back at them
class HTTPConnection

nitcorn :: HTTPConnection

Connection rebuilding HTTP requests
class HttpServer

nitcorn :: HttpServer

A server handling a single connection
class ProcessDuplex

core :: ProcessDuplex

Process on which stdout can be read and stdin can be written to like a Duplex
class Stderr

core :: Stderr

Standard error stream.
class Stdout

core :: Stdout

Standard output stream.
class StringWriter

core :: StringWriter

Stream writing to a String
class TCPStream

socket :: TCPStream

Simple communication stream with a remote socket
class WebsocketConnection

websocket :: WebsocketConnection

Connection to a websocket client

Class definitions

core $ Writer
# A `Stream` that can be written to
abstract class Writer
	super Stream

	# Write bytes from `s`
	fun write_bytes(s: Bytes) do write_bytes_from_cstring(s.items, s.length)

	# Write `len` bytes from `ns`
	fun write_bytes_from_cstring(ns: CString, len: Int) is abstract

	# Write a string
	fun write(s: Text) is abstract

	# Write a single byte
	fun write_byte(value: Int) is abstract

	# Write a single char
	fun write_char(c: Char) do
		var ln = codec.add_char_to(c, write_buffer)
		write_bytes_from_cstring(write_buffer, ln)
	end

	# Can the stream be used to write
	fun is_writable: Bool is abstract
end
lib/core/stream.nit:521,1--545,3

binary :: binary $ Writer
redef abstract class Writer
	super BinaryStream

	# Write a boolean `value` on a byte, using 0 for `false` and 1 for `true`
	fun write_bool(value: Bool) do write_byte if value then 1 else 0

	# Write up to 8 `Bool` in a byte
	#
	# To be used with `BinaryReader::read_bits`.
	#
	# Ensure: `bits.length <= 8`
	fun write_bits(bits: Bool...)
	do
		assert bits.length <= 8

		var int = 0
		for b in bits.length.times do
			if bits[b] then int |= 1 << (7 - b)
		end

		write_byte int
	end

	# Write `text` as a null terminated string
	#
	# To be used with `Reader::read_string`.
	#
	# Require: `text` has no null bytes.
	fun write_string(text: Text)
	do
		write text
		write_byte 0x00
	end

	# Write the length as a 64 bits integer, then the content of `text`
	#
	# To be used with `Reader::read_block`.
	#
	# Compared to `write_string`, this method supports null bytes in `text`.
	fun write_block(text: Text)
	do
		write_int64 text.byte_length
		write text
	end

	# Write a floating point `value` on 32 bits
	#
	# Using this format may result in a loss of precision as it uses less bits
	# than Nit `Float`.
	fun write_float(value: Float)
	do
		for i in [0..4[ do write_byte value.float_byte_at(i, big_endian)
	end

	# Write a floating point `value` on 64 bits
	fun write_double(value: Float)
	do
		for i in [0..8[ do write_byte value.double_byte_at(i, big_endian)
	end

	# Write `value` as a signed integer on 64 bits
	#
	# Using this format may result in a loss of precision as the length of a
	# Nit `Int` may be more than 64 bits on some platforms.
	fun write_int64(value: Int)
	do
		for i in [0..8[ do write_byte value.int64_byte_at(i, big_endian)
	end

	# TODO:
	#
	# fun write_int8
	# fun write_uint8 # No need for this one, it is the current `read_char`
	# fun write_int16
	# fun write_uint16
	# fun write_int32
	# fun write_uint32
	# fun write_uint64
	# fun write_long_double?
end
lib/binary/binary.nit:69,1--148,3

msgpack :: write $ Writer
redef class Writer

	# Write `null`, or nil, in MessagePack format
	fun write_msgpack_null do write_byte 0xC0

	# Write `bool` in MessagePack format
	fun write_msgpack_bool(bool: Bool)
	do write_byte(if bool then 0xC3 else 0xC2)

	# ---
	# Integers

	# Write the integer `value` either as the shortest possible MessagePack _int_
	fun write_msgpack_int(value: Int)
	do
		if value >= -0x20 and value <= 0x7F then
			write_msgpack_fixint value
		else if value >= 0 then
			if value <= 0xFF then
				write_msgpack_uint8 value
			else if value <= 0xFFFF then
				write_msgpack_uint16 value
			else if value <= 0xFFFF_FFFF then
				write_msgpack_uint32 value
			else #if value <= 0xFFFF_FFFF_FFFF_FFFF then
				write_msgpack_uint64 value
			end
		else if value >= -128 then
			write_msgpack_int8 value
		else if value >= -32768 then
			write_msgpack_int16 value
		else if value >= -2147483648 then
			write_msgpack_int32 value
		else
			write_msgpack_int64 value
		end
	end

	# Write `value` as a single byte with metadata
	#
	# Require: `value >= -0x20 and value <= 0x7F`
	fun write_msgpack_fixint(value: Int)
	do
		assert value >= -0x20 and value <= 0x7F
		write_byte value
	end

	# Write `value` over one unsigned byte, following 1 metadata byte
	#
	# Require: `value >= 0x00 and value <= 0xFF`
	fun write_msgpack_uint8(value: Int)
	do
		write_byte 0xCC
		write_bytes value.to_bytes(n_bytes=1)
	end

	# Write `value` over two unsigned bytes, following 1 metadata byte
	#
	# Require: `value >= 0x00 and value <= 0xFFFF`
	fun write_msgpack_uint16(value: Int)
	do
		write_byte 0xCD
		write_bytes value.to_bytes(n_bytes=2)
	end

	# Write `value` over 4 unsigned bytes, following 1 metadata byte
	#
	# Require: `value >= 0x00 and value <= 0xFFFF_FFFF`
	fun write_msgpack_uint32(value: Int)
	do
		write_byte 0xCE
		write_bytes value.to_bytes(n_bytes=4)
	end

	# Write `value` over 8 unsigned bytes, following 1 metadata byte
	#
	# Require: `value >= 0x00 and value <= 0xFFFF_FFFF_FFFF_FFFF`
	fun write_msgpack_uint64(value: Int)
	do
		write_byte 0xCF
		write_bytes value.to_bytes(n_bytes=8)
	end

	# Write `value` over one signed byte, following 1 metadata byte
	#
	# Require: `value >= -128  and value <= 127`
	fun write_msgpack_int8(value: Int)
	do
		write_byte 0xD0
		write_bytes value.to_bytes(n_bytes=1)
	end

	# Write `value` over two signed bytes, following 1 metadata byte
	fun write_msgpack_int16(value: Int)
	do
		write_byte 0xD1
		write_bytes value.to_bytes(n_bytes=2)
	end

	# Write `value` over 4 signed bytes, following 1 metadata byte
	fun write_msgpack_int32(value: Int)
	do
		write_byte 0xD2
		write_bytes value.to_bytes(n_bytes=4)
	end

	# Write `value` over 8 signed bytes, following 1 metadata byte
	fun write_msgpack_int64(value: Int)
	do
		write_byte 0xD3
		write_int64 value
	end

	# ---
	# Floats

	# Write `value` as a MessagePack float (losing precision)
	fun write_msgpack_float(value: Float)
	do
		write_byte 0xCA
		write_float value
	end

	# Write `value` as a MessagePack double
	fun write_msgpack_double(value: Float)
	do
		write_byte 0xCB
		write_double value
	end

	# ---
	# Strings

	# Write `text` in the shortest possible MessagePack format
	#
	# Require: `text.byte_length <= 0xFFFF_FFFF`
	fun write_msgpack_str(text: Text)
	do
		var len = text.byte_length
		if len <= 0x1F then
			write_msgpack_fixstr text
		else if len <= 0xFF then
			write_msgpack_str8 text
		else if len <= 0xFFFF then
			write_msgpack_str16 text
		else if len <= 0xFFFF_FFFF then
			write_msgpack_str32 text
		else
			abort
		end
	end

	# Write `text` in _fixstr_ format, max of 0x1F bytes
	#
	# Require: `text.byte_length <= 0x1F`
	fun write_msgpack_fixstr(text: Text)
	do
		var len = text.byte_length
		assert len <= 0x1F

		var b = 0b1010_0000 | len
		write_byte b

		write text
	end

	# Write `text` in _str8_ format, max of 0xFF bytes
	#
	# Require: `text.byte_length <= 0xFF`
	fun write_msgpack_str8(text: Text)
	do
		var len = text.byte_length
		assert len <= 0xFF

		write_byte 0xD9
		write_byte len
		write text
	end

	# Write `text` in _str16_ format, max of 0xFFFF bytes
	#
	# Require: `text.byte_length <= 0xFFFF`
	fun write_msgpack_str16(text: Text)
	do
		var len = text.byte_length
		assert len <= 0xFFFF

		write_byte 0xDA
		var len_bytes = len.to_bytes
		write_byte len_bytes[0]
		write_byte if len_bytes.length > 1 then len_bytes[1] else 0
		write text
	end

	# Write `text` in _str32_ format, max of 0xFFFF_FFFF bytes
	#
	# Require: `text.byte_length <= 0xFFFF_FFFF`
	fun write_msgpack_str32(text: Text)
	do
		var len = text.byte_length
		assert len <= 0xFFFF_FFFF

		write_byte 0xDB
		var len_bytes = len.to_bytes
		write_byte len_bytes[0]
		for i in [1..4[ do
			write_byte if len_bytes.length > i then len_bytes[i] else 0
		end
		write text
	end

	# ---
	# Binary data

	# Write `data` in the shortest possible MessagePack _bin_ format
	#
	# Require: `data.length <= 0xFFFF_FFFF`
	fun write_msgpack_bin(data: Bytes)
	do
		var len = data.length
		if len <= 0xFF then
			write_msgpack_bin8 data
		else if len <= 0xFFFF then
			write_msgpack_bin16 data
		else if len <= 0xFFFF_FFFF then
			write_msgpack_bin32 data
		else abort
	end

	# Write `data` in _bin8_ format, max of 0xFF bytes
	#
	# Require: `data.length <= 0xFF`
	fun write_msgpack_bin8(data: Bytes)
	do
		var len = data.length
		assert len <= 0xFF

		write_byte 0xC4
		write_byte len
		write_bytes data
	end

	# Write `data` in _bin16_ format, max of 0xFFFF bytes
	#
	# Require: `data.length <= 0xFFFF`
	fun write_msgpack_bin16(data: Bytes)
	do
		var len = data.length
		assert len <= 0xFFFF

		write_byte 0xC5
		write_bytes len.to_bytes(n_bytes=2)
		write_bytes data
	end

	# Write `data` in _bin32_ format, max of 0xFFFF_FFFF bytes
	#
	# Require: `data.length <= 0xFFFF_FFFF`
	fun write_msgpack_bin32(data: Bytes)
	do
		var len = data.length
		assert len <= 0xFFFF_FFFF

		write_byte 0xC6
		write_bytes len.to_bytes(n_bytes=4)
		write_bytes data
	end

	# ---
	# Arrays

	# Write an array header for `len` items in the shortest possible MessagePack _array_ format
	#
	# After writing the header, clients should write the array items.
	#
	# Require: `len <= 0xFFFF_FFFF`
	fun write_msgpack_array(len: Int)
	do
		if len <= 0x0F then
			write_msgpack_fixarray len
		else if len <= 0xFFFF then
			write_msgpack_array16 len
		else if len <= 0xFFFF_FFFF then
			write_msgpack_array32 len
		else
			abort
		end
	end

	# Write an array header for `len` items, max of 0x0F items
	#
	# After writing the header, clients should write the array items.
	#
	# Require: `len <= 0x0F`
	fun write_msgpack_fixarray(len: Int)
	do
		assert len <= 0x0F
		write_byte 0b1001_0000 | len
	end

	# Write an array header for `len` items, max of 0xFFFF items
	#
	# After writing the header, clients should write the array items.
	#
	# Require: `len <= 0xFFFF`
	fun write_msgpack_array16(len: Int)
	do
		assert len <= 0xFFFF
		write_byte 0xDC
		write_bytes len.to_bytes(n_bytes=2)
	end

	# Write an array header for `len` items, max of 0xFFFF_FFFF items
	#
	# After writing the header, clients should write the array items.
	#
	# Require: `len <= 0xFFFF_FFFF`
	fun write_msgpack_array32(len: Int)
	do
		assert len <= 0xFFFF_FFFF
		write_byte 0xDD
		write_bytes len.to_bytes(n_bytes=4)
	end

	# ---
	# Map

	# Write a map header for `len` keys/value pairs in the shortest possible MessagePack _map_ format
	#
	# After writing the header, clients should write the map data, alternating
	# between keys and values.
	#
	# Require: `len <= 0xFFFF_FFFF`
	fun write_msgpack_map(len: Int)
	do
		if len <= 0x0F then
			write_msgpack_fixmap len
		else if len <= 0xFFFF then
			write_msgpack_map16 len
		else if len <= 0xFFFF_FFFF then
			write_msgpack_map32 len
		else
			abort
		end
	end

	# Write a map header for `len` key/value pairs, max of 0x0F pairs
	#
	# After writing the header, clients should write the map data, alternating
	# between keys and values.
	#
	# Require: `len <= 0x0F`
	fun write_msgpack_fixmap(len: Int)
	do
		assert len <= 0x0F
		write_byte 0b1000_0000 | len
	end

	# Write a map header for `len` key/value pairs, max of 0xFFFF pairs
	#
	# After writing the header, clients should write the map data, alternating
	# between keys and values.
	#
	# Require: `len <= 0xFFFF`
	fun write_msgpack_map16(len: Int)
	do
		assert len <= 0xFFFF
		write_byte 0xDE
		write_bytes len.to_bytes(n_bytes=2)
	end

	# Write a map header for `len` key/value pairs, max of 0xFFFF_FFFF pairs
	#
	# After writing the header, clients should write the map data, alternating
	# between keys and values.
	#
	# Require: `len <= 0xFFFF_FFFF`
	fun write_msgpack_map32(len: Int)
	do
		assert len <= 0xFFFF_FFFF
		write_byte 0xDF
		write_bytes len.to_bytes(n_bytes=4)
	end

	# ---
	# Ext

	# Write an application-specific extension for `typ` and `bytes` in the shortest possible MessagePack _ext_ format
	#
	# Require: `bytes.length <= 0xFFFF_FFFF`
	#
	# ~~~
	# var writer = new BytesWriter
	# writer.write_msgpack_ext(0x0A, b"\x0B\x0C\x0D")
	# assert writer.bytes == b"\xC7\x03\x0A\x0B\x0C\x0D"
	# ~~~
	fun write_msgpack_ext(typ: Int, bytes: Bytes)
	do
		var len = bytes.length
		if len == 1 then
			write_msgpack_fixext1 typ
			write_byte bytes.first
		else if len == 2 then
			write_msgpack_fixext2 typ
			write_bytes bytes
		else if len == 4 then
			write_msgpack_fixext4 typ
			write_bytes bytes
		else if len == 8 then
			write_msgpack_fixext8 typ
			write_bytes bytes
		else if len == 16 then
			write_msgpack_fixext16 typ
			write_bytes bytes
		else if len <= 0xFF then
			write_msgpack_ext8(typ, len)
			write_bytes bytes
		else if len <= 0xFFFF then
			write_msgpack_ext16(typ, len)
			write_bytes bytes
		else if len <= 0xFFFF_FFFF then
			write_msgpack_ext32(typ, len)
			write_bytes bytes
		else
			abort
		end
	end

	# Write the header for an application-specific extension of one data byte
	#
	# After writing the header, clients should write the data byte.
	fun write_msgpack_fixext1(typ: Int)
	do
		write_byte 0xD4
		write_byte typ
	end

	# Write the header for an application-specific extension of two data bytes
	#
	# After writing the header, clients should write the two data bytes.
	fun write_msgpack_fixext2(typ: Int)
	do
		write_byte 0xD5
		write_byte typ
	end

	# Write the header for an application-specific extension of 4 data bytes
	#
	# After writing the header, clients should write the 4 data bytes.
	fun write_msgpack_fixext4(typ: Int)
	do
		write_byte 0xD6
		write_byte typ
	end

	# Write the header for an application-specific extension of 8 data bytes
	#
	# After writing the header, clients should write the 8 data bytes.
	fun write_msgpack_fixext8(typ: Int)
	do
		write_byte 0xD7
		write_byte typ
	end

	# Write the header for an application-specific extension of 16 data bytes
	#
	# After writing the header, clients should write the 16 data bytes.
	fun write_msgpack_fixext16(typ: Int)
	do
		write_byte 0xD8
		write_byte typ
	end

	# Write the header for an application-specific extension of `len` data bytes
	#
	# After writing the header, clients should write the data bytes.
	#
	# Require: `len >= 0 and <= 0xFF`
	fun write_msgpack_ext8(typ, len: Int)
	do
		assert len >= 0 and len <= 0xFF
		write_byte 0xC7
		write_byte len
		write_byte typ
	end

	# Write the header for an application-specific extension of `len` data bytes
	#
	# After writing the header, clients should write the data bytes.
	#
	# Require: `len >= 0 and <= 0xFFFF`
	fun write_msgpack_ext16(typ, len: Int)
	do
		assert len >= 0 and len <= 0xFFFF
		write_byte 0xC8
		write_bytes len.to_bytes(n_bytes=2)
		write_byte typ
	end

	# Write the header for an application-specific extension of `len` data bytes
	#
	# After writing the header, clients should write the data bytes.
	#
	# Require: `len >= 0 and <= 0xFFFF_FFFF`
	fun write_msgpack_ext32(typ, len: Int)
	do
		assert len >= 0 and len <= 0xFFFF_FFFF
		write_byte 0xC9
		write_bytes len.to_bytes(n_bytes=4)
		write_byte typ
	end

	# TODO timestamps
end
lib/msgpack/write.nit:20,1--533,3

msgpack :: serialization_write $ Writer
redef class Writer
	# Serialize `value` in MessagePack format
	fun serialize_msgpack(value: nullable Serializable, plain: nullable Bool)
	do
		var serializer = new MsgPackSerializer(self)
		serializer.plain_msgpack = plain or else false
		serializer.serialize value
	end
end
lib/msgpack/serialization_write.nit:164,1--172,3