Class Decoding
Use [lib0/decoding] with [lib0/encoding]. Every encoding function has a corresponding decoding function.
Encodes numbers in little-endian order (least to most significant byte order) and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/) which is also used in Protocol Buffers.
```js // encoding step const encoder = encoding.createEncoder() encoding.writeVarUint(encoder, 256) encoding.writeVarString(encoder, 'Hello world!') const buf = encoding.toUint8Array(encoder) ```
```js // decoding step const decoder = decoding.createDecoder(buf) decoding.readVarUint(decoder) // => 256 decoding.readVarString(decoder) // => 'Hello world!' decoding.hasContent(decoder) // => false - all data is read ```
-
Method Summary
Modifier and TypeMethodDescriptionstatic DecoderClone a decoder instance.static DecoderClone a decoder instance.static DecodercreateDecoder(Uint8Array uint8Array) static booleanhasContent(Decoder decoder) static intpeekRealVarInt(Decoder decoder) Look ahead and read varUint without incrementing positionstatic intpeekRealVarUint(Decoder decoder) Look ahead and read varUint without incrementing positionstatic intpeekUint16(Decoder decoder) static intpeekUint32(Decoder decoder) static intstatic longpeekVarInt(Decoder decoder) static StringpeekVarString(Decoder decoder) static longpeekVarUint(Decoder decoder) static Objectstatic longreadBigInt64(Decoder decoder) static floatreadFloat32(Decoder decoder) static doublereadFloat64(Decoder decoder) static intreadRealVarInt(Decoder decoder) Read real signed int (32 bit, coz max Integer is 2^31) with variable length. 1/8th of the storage is used as encoding overhead.static intreadRealVarUint(Decoder decoder) Read real unsigned int (31 bit, coz max Integer is 2^31 - 1) with variable length. 1/8th of the storage is used as encoding overhead.static Uint8ArrayreadTailAsUint8Array(Decoder decoder) static StringreadTerminatedString(Decoder decoder) static Uint8ArrayreadTerminatedUint8Array(Decoder decoder) static intreadUint16(Decoder decoder) static intreadUint32(Decoder decoder) static intreadUint32BigEndian(Decoder decoder) static intstatic Uint8ArrayreadUint8Array(Decoder decoder, int len) static longreadVarInt(Decoder decoder) static StringreadVarString(Decoder decoder) static longreadVarUint(Decoder decoder) Read unsigned long (32bit) with variable length.static Uint8ArrayreadVarUint8Array(Decoder decoder) static void
-
Method Details
-
createDecoder
-
hasContent
-
clone
Clone a decoder instance. Optionally set a new position parameter.- Parameters:
decoder-- Returns:
-
clone
Clone a decoder instance. Optionally set a new position parameter.- Parameters:
decoder-newPos-- Returns:
-
readUint8Array
-
readVarUint8Array
-
readTailAsUint8Array
-
skip8
-
readUint8
-
readUint16
-
readUint32
-
readUint32BigEndian
-
peekUint8
-
peekUint16
-
peekUint32
-
readVarUint
Read unsigned long (32bit) with variable length. K-Note: treat it as long value -
readVarInt
-
peekVarUint
-
peekVarInt
-
readRealVarUint
Read real unsigned int (31 bit, coz max Integer is 2^31 - 1) with variable length. 1/8th of the storage is used as encoding overhead. numbers < 2^7 is stored in one byte length numbers < 2^14 is stored in two byte length -
readRealVarInt
Read real signed int (32 bit, coz max Integer is 2^31) with variable length. 1/8th of the storage is used as encoding overhead. numbers < 2^7 is stored in one byte length numbers < 2^14 is stored in two byte length- Parameters:
decoder-- Returns:
- An unsigned integer
-
peekRealVarUint
Look ahead and read varUint without incrementing position- Parameters:
decoder-- Returns:
-
peekRealVarInt
Look ahead and read varUint without incrementing position- Parameters:
decoder-
-
readFloat32
-
readFloat64
-
readBigInt64
-
readTerminatedUint8Array
-
readTerminatedString
-
peekVarString
-
readVarString
-
readAny
-