new ByteTree(value)
Class for representing ordered trees of byte arrays. A
byte tree is represented as an array of bytes as follows.
- A leaf holding a sequence of bytes B of length l is converted into a byte array T|L|B, where "|" denotes concatenation, T is a single byte equal to 1 indicating that this is a leaf, and L is a 32-bit signed integer representation of l.
- A node holding children c_0,...,c_{l-1} is converted into a byte array T|L|C_0|...|C_{l-1}, where T is a single byte equal to 0 indicating that this is a node, L is a 32-bit unsigned integer representation of l and C_i is the representation of c_i as a byte array.
Parameters:
Name | Type | Description |
---|---|---|
value |
Data needed to construct a byte tree. This can be: (1) an array of other byte trees that becomes siblings in the new instance, (2) a raw byte array in which case the resulting instance becomes a leaf, or (3) a hexadecimal string representing a byte tree. The hexadecimal string may contain an ASCII encoded prefix ending with "::", in which case it is discarded. |
- Source:
Returns:
Byte tree containing the input data.
Methods
(static) asByteTree(value)
Guarantees that the input is a byte tree.
Parameters:
Name | Type | Description |
---|---|---|
value |
Byte tree or a byte array. |
- Source:
Returns:
Input value if it is a byte tree and a leaf byte tree based
on the byte array otherwise.
(static) readByteTreeFromByteArray(source, index)
Recovers a byte tree from its representation as a byte
array from the given source. If the second parameter is given, then
reading starts at this position and a pair is returned. If no
second parameter is given, then the byte tree is simply returned.
Parameters:
Name | Type | Description |
---|---|---|
source |
Array holding a representation of a byte tree. | |
index |
Position in the array where reading starts. |
- Source:
Returns:
Recovered byte tree.
isLeaf()
Indicates if this byte tree is a leaf or not.
- Source:
Returns:
True or false depending on if this byte tree is a leaf or not.
setToByteArray(destination, index)
Writes a byte tree representation of this byte tree to
the destination starting at the given index.
Parameters:
Name | Type | Description |
---|---|---|
destination |
Destination of written bytes. | |
index |
Index of starting position. |
- Source:
Returns:
Number of bytes written.
size()
Computes the total number of bytes needed to represent
this byte tree as a byte array.
- Source:
Returns:
Number of bytes needed to store a byte array representation
of this byte tree.
toByteArray()
Generates a representation of this byte tree as a byte
array.
- Source:
Returns:
Representation of this byte tree as a byte array.
toHexString()
Generates hexadecimal representation of this byte
tree.
- Source:
Returns:
Hexadecimal representation of this byte tree.
toPrettyString()
Generates representation as a nested JSON list with
the leaves as hexadecimal string representations of the data in
leaves. This is meant for debugging.
- Source:
Returns:
Pretty representation of this byte tree.