Class Encoding
-
Method Summary
Modifier and TypeMethodDescriptionstatic Encoderstatic Uint8Arraystatic booleanhasContent(Encoder encoder) Check whether encoder is empty.static intThe current length of the encoded data.static voidWrite one byte at a specific position.static voidWrite two bytes as an unsigned integer at a specific location.static voidWrite two bytes as an unsigned integer at a specific location.static voidWrite one byte as an unsigned Integer at a specific location.static Uint8ArraytoUint8Array(Encoder encoder) Transform to Uint8Array.static voidVerify that it is possible to write `len` bytes wtihout checking.static voidWrite one byte to the encoder.static voidWrite one byte to the encoder.static voidEncode data with efficient binary format.static voidwriteBigInt64(Encoder encoder, long num) static voidwriteBinaryEncoder(Encoder encoder, Encoder append) Write the content of another Encoder.static voidwriteFloat32(Encoder encoder, float num) static voidwriteFloat64(Encoder encoder, double num) static voidwriteTerminatedString(Encoder encoder, String str) Write a string terminated by a special byte sequence.static voidwriteTerminatedUint8Array(Encoder encoder, Uint8Array buf) Write a terminating Uint8Array.static voidwriteUint16(Encoder encoder, int num) Write two bytes as an unsigned integer.static voidwriteUint32(Encoder encoder, int num) Write two bytes as an unsigned integerstatic voidwriteUint32BigEndian(Encoder encoder, int num) Write two bytes as an unsigned integer in big endian order.static voidwriteUint8(Encoder encoder, int num) Write one byte as an unsigned integer.static voidwriteUint8Array(Encoder encoder, Uint8Array uint8Array) Append fixed-length Uint8Array to the encoder.static voidwriteVarInt(Encoder encoder, int num) Write a variable length integer.static voidwriteVarInt(Encoder encoder, long num) Write a variable length long.static voidwriteVarString(Encoder encoder, String str) Write a variable length string.static voidwriteVarUint(Encoder encoder, int num) Write a variable length unsigned integer.static voidwriteVarUint(Encoder encoder, long num) Write a variable length unsigned long.static voidwriteVarUint8Array(Encoder encoder, Uint8Array uint8Array) Append an Uint8Array to Encoder.
-
Method Details
-
createEncoder
-
encode
-
length
The current length of the encoded data.- Parameters:
encoder-- Returns:
-
hasContent
Check whether encoder is empty.- Parameters:
encoder-- Returns:
-
toUint8Array
Transform to Uint8Array.- Parameters:
encoder-- Returns:
-
verifyLen
Verify that it is possible to write `len` bytes wtihout checking. If necessary, a new Buffer with the required length is attached.- Parameters:
encoder-len-
-
write
Write one byte to the encoder.- Parameters:
encoder-num-
-
write
Write one byte to the encoder.- Parameters:
encoder-num-
-
set
Write one byte at a specific position. Position must already be written (i.e. encoder.length > pos)- Parameters:
encoder-pos-num-
-
writeUint8
Write one byte as an unsigned integer.- Parameters:
encoder-num-
-
setUint8
Write one byte as an unsigned Integer at a specific location.- Parameters:
encoder-pos-num-
-
writeUint16
Write two bytes as an unsigned integer.- Parameters:
encoder-num-
-
setUint16
Write two bytes as an unsigned integer at a specific location.- Parameters:
encoder-pos-num-
-
writeUint32
Write two bytes as an unsigned integer- Parameters:
encoder-num-
-
writeUint32BigEndian
Write two bytes as an unsigned integer in big endian order. (most significant byte first)- Parameters:
encoder-num-
-
setUint32
Write two bytes as an unsigned integer at a specific location.- Parameters:
encoder-pos-num-
-
writeVarUint
Write a variable length unsigned integer. Acceptable max integer is 2^31.- Parameters:
encoder-num-
-
writeVarUint
Write a variable length unsigned long.- Parameters:
encoder-num-
-
writeVarInt
Write a variable length integer. Acceptable max integer is 2^31.- Parameters:
encoder-num-
-
writeVarInt
Write a variable length long.- Parameters:
encoder-num-
-
writeVarString
Write a variable length string.- Parameters:
encoder-str-
-
writeTerminatedString
Write a string terminated by a special byte sequence. This is not very performant and is generally discouraged. However, the resulting byte arrays are lexiographically ordered which makes this a nice feature for databases.The string will be encoded using utf8 and then terminated and escaped using writeTerminatingUint8Array.
- Parameters:
encoder-str-
-
writeTerminatedUint8Array
Write a terminating Uint8Array. Note that this is not performant and is generally discouraged. There are few situations when this is needed.We use 0x0 as a terminating character. 0x1 serves as an escape character for 0x0 and 0x1.
Example: [0,1,2] is encoded to [1,0,1,1,2,0]. 0x0, and 0x1 needed to be escaped using 0x1. Then the result is terminated using the 0x0 character.
This is basically how many systems implement null terminated strings. However, we use an escape character 0x1 to avoid issues and potenial attacks on our database (if this is used as a key encoder for NoSql databases).
- Parameters:
encoder-buf-
-
writeBinaryEncoder
Write the content of another Encoder.- Parameters:
encoder-append-
-
writeUint8Array
Append fixed-length Uint8Array to the encoder.- Parameters:
encoder-uint8Array-
-
writeVarUint8Array
Append an Uint8Array to Encoder.- Parameters:
encoder-uint8Array-
-
writeFloat32
-
writeFloat64
-
writeBigInt64
-
writeAny
Encode data with efficient binary format.Differences to JSON: • Transforms data to a binary format (not to a string) • Encodes undefined, NaN, and ArrayBuffer (these can't be represented in JSON) • Numbers are efficiently encoded either as a variable length integer, as a 32 bit float, as a 64 bit float, or as a 64 bit bigint.
Encoding table:
| Data Type | Prefix | Encoding Method | Comment | | ------------------- | -------- | ------------------ | ------- | | undefined | 127 | | Functions, symbol, and everything that cannot be identified is encoded as undefined | | null | 126 | | | | integer | 125 | writeVarInt | Only encodes 32 bit signed integers | | float32 | 124 | writeFloat32 | | | float64 | 123 | writeFloat64 | | | bigint | 122 | writeBigInt64 | | | boolean (false) | 121 | | True and false are different data types so we save the following byte | | boolean (true) | 120 | | - 0b01111000 so the last bit determines whether true or false | | string | 119 | writeVarString | | | Map | 118 | custom | Writes {length} then {length} key-value pairs | | array | 117 | custom | Writes {length} then {length} json values | | Uint8Array | 116 | writeVarUint8Array | We use Uint8Array for any kind of binary data |
Reasons for the decreasing prefix: We need the first bit for extendability (later we may want to encode the prefix with writeVarUint). The remaining 7 bits are divided as follows: [0-30] the beginning of the data range is used for custom purposes (defined by the function that uses this library) [31-127] the end of the data range is used for data encoding by lib0/encoding.js
- Parameters:
encoder-data- the type might be {null|number|bigint|boolean|string|Map|Array|Uint8Array}
-