Package convex.core.data
Class Format
java.lang.Object
convex.core.data.Format
Static utility class for message format encoding
"Standards are always out of date. That's what makes them standards." - Alan
Bennett
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final long
Memory size of a fully embedded value (zero)static final int
8191 byte system-wide limit on the legal length of a data object encoding.static final int
Maximum size in bytes of an embedded value, including tagstatic final long
Maximum allowed encoded message length in bytesstatic final int
Maximum length in bytes of a Ref encoding (may be an embedded data object)static final int
Maximum length for a VLC encoded Countstatic final int
Maximum length for a VLC encoded Longstatic final int
Encoded length of a null value -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ACell[]
decodeCells
(Blob data) Decodes an array of Cells packed in a Blob.static void
decodeCells
(HashMap<Hash, ACell> acc, Blob data) Decode encoded non-embedded Cells into an accumulator HashMapstatic <T extends ACell>
TdecodeMultiCell
(Blob data) Reads a cell from a Blob of data, allowing for non-embedded children following the first cellstatic Blob
encodeCells
(List<ACell> cells) Encodes a flat list of cells in order specified in multi-cell formatstatic Blob
encodedBlob
(ACell o) Gets the encoded Blob for an object in canonical message formatstatic Blob
encodeDelta
(List<ACell> cells) Encode a list of cells as a delta message.static String
encodedString
(ACell cell) Gets a hex String representing an object's encodingstatic String
Gets a hex String representing an object's encoding.static Blob
encodeMultiCell
(ACell a, boolean everything) Encode a Cell completely in multi-cell message format.static int
estimateEncodingSize
(ACell cell) Estimate the encoding size of a Cell value.static int
getEncodingLength
(ACell value) static int
getLongLength
(long value) Gets the length of a Long in bytes (minimum bytes needs to express valuestatic int
getVLCCountLength
(long x) Gets the length in bytes of VLC count encoding for the given long valuestatic int
getVLCLength
(long x) Gets the length in bytes of VLC encoding for the given long valuestatic boolean
isCanonical
(ACell o) Returns true if the object is a canonical data object.static boolean
isEmbedded
(ACell cell) Determines if an object should be embedded directly in the encoding rather than referenced with a Ref / hash.static int
Peeks for a VLC encoded message length at the start of a ByteBuffer, which must contain at least 1 byte, maximum 2.static <T extends ACell>
TDecodes a single Value from a Blob.static <T extends ACell>
TDecodes a single Value from a Blob, starting at a given offset Assumes the presence of a tag.static <T extends ACell>
THelper method to read a value encoded as a hex stringstatic long
Reads a long value represented by the specified bytes in a BlobReads a Ref or embedded Cell value from a Blob Converts Embedded Cells to Direct Refs automatically.static AString
readUTF8String
(Blob blob, int pos, int len) Reads UTF-8 String data from a Blob.static AString
readUTF8String
(ByteBuffer bb, int len) Reads a UTF-8 String from a ByteBuffer.static long
readVLCCount
(byte[] data, int pos) Reads a VLC encoded count (non-negative integer) as a long from the given location in a byte array.static long
readVLCCount
(AArrayBlob blob, int pos) static long
readVLCLong
(byte[] data, int pos) Reads a VLC encoded long as a long from the given location in a byte array.static long
readVLCLong
(AArrayBlob blob, int pos) protected static boolean
vlcContinuesFrom
(byte octet) Checks if VLC continues from given byte, i.e.static long
vlcSignExtend
(byte b) Sign extend 7th bit (sign in a VLC byte) of a byte to all bits in a long i.e.static int
Writes a cell encoding to a byte array, preceded by the appropriate tagstatic int
writeHexDigits
(byte[] bs, int pos, ABlob src, long start, long length) Writes hex digits from digit position start, total length.static ByteBuffer
writeLength
(ByteBuffer bb, int i) static ByteBuffer
writeMessageLength
(ByteBuffer bb, int len) Writes a message length as a VLC encoded longstatic int
writeRawUTF8String
(byte[] bs, int pos, String s) Writes a raw string without tag to the byte array.static int
writeVLCCount
(byte[] bs, int pos, long x) Puts a variable length count into the specified byte array (with no tag) Format: MSB of each byte 0=last octet, 1=more octets Following MSB, 7 bits of integer representation for each octetstatic int
writeVLCLong
(byte[] bs, int pos, long x) Puts a variable length integer into the specified byte array (with no tag) Format: MSB of each byte 0=last octet, 1=more octets Following MSB, 7 bits of integer representation for each octet Second highest bit of first byte is interpreted as the signstatic ByteBuffer
writeVLCLong
(ByteBuffer bb, long x) Puts a VLC encoded long into the specified bytebuffer (with no tag) Format: MSB of each byte 0=last octet, 1=more octets Following MSB, 7 bits of integer representation for each octet Second highest bit of first byte is interpreted as the sign
-
Field Details
-
LIMIT_ENCODING_LENGTH
public static final int LIMIT_ENCODING_LENGTH8191 byte system-wide limit on the legal length of a data object encoding. Technical reasons for this choice:- This is the max length that can be VLC encoded in a 2 byte message header. This simplifies message encoding and decoding.
- It is big enough to include a 4096-byte Blob
- See Also:
-
MAX_VLC_LONG_LENGTH
public static final int MAX_VLC_LONG_LENGTHMaximum length for a VLC encoded Long- See Also:
-
MAX_VLC_COUNT_LENGTH
public static final int MAX_VLC_COUNT_LENGTHMaximum length for a VLC encoded Count- See Also:
-
MAX_EMBEDDED_LENGTH
public static final int MAX_EMBEDDED_LENGTHMaximum size in bytes of an embedded value, including tag- See Also:
-
NULL_ENCODING_LENGTH
public static final int NULL_ENCODING_LENGTHEncoded length of a null value- See Also:
-
MAX_REF_LENGTH
public static final int MAX_REF_LENGTHMaximum length in bytes of a Ref encoding (may be an embedded data object) -
MAX_MESSAGE_LENGTH
public static final long MAX_MESSAGE_LENGTHMaximum allowed encoded message length in bytes- See Also:
-
FULL_EMBEDDED_MEMORY_SIZE
public static final long FULL_EMBEDDED_MEMORY_SIZEMemory size of a fully embedded value (zero)- See Also:
-
-
Constructor Details
-
Format
public Format()
-
-
Method Details
-
getVLCLength
public static int getVLCLength(long x) Gets the length in bytes of VLC encoding for the given long value- Parameters:
x
- Long value to encode- Returns:
- Length of VLC encoding
-
getVLCCountLength
public static int getVLCCountLength(long x) Gets the length in bytes of VLC count encoding for the given long value- Parameters:
x
- Long value to encode- Returns:
- Length of VLC encoding
-
writeVLCLong
Puts a VLC encoded long into the specified bytebuffer (with no tag) Format:- MSB of each byte 0=last octet, 1=more octets
- Following MSB, 7 bits of integer representation for each octet
- Second highest bit of first byte is interpreted as the sign
- Parameters:
bb
- ByteBuffer to write tox
- Value to VLC encode- Returns:
- Updated ByteBuffer
-
writeVLCLong
public static int writeVLCLong(byte[] bs, int pos, long x) Puts a variable length integer into the specified byte array (with no tag) Format:- MSB of each byte 0=last octet, 1=more octets
- Following MSB, 7 bits of integer representation for each octet
- Second highest bit of first byte is interpreted as the sign
- Parameters:
bs
- Byte array to write topos
- Initial position in byte arrayx
- Long value to write- Returns:
- end position in byte array after writing VLC long
-
writeVLCCount
public static int writeVLCCount(byte[] bs, int pos, long x) Puts a variable length count into the specified byte array (with no tag) Format:- MSB of each byte 0=last octet, 1=more octets
- Following MSB, 7 bits of integer representation for each octet
- Parameters:
bs
- Byte array to write topos
- Initial position in byte arrayx
- Long value to write- Returns:
- end position in byte array after writing VLC long
-
vlcSignExtend
public static long vlcSignExtend(byte b) Sign extend 7th bit (sign in a VLC byte) of a byte to all bits in a long i.e. sign extend excluding the continuation bit: where VLC Byte = csxxxxxx- Parameters:
b
- Byte to extend- Returns:
- The sign-extended byte as a long
-
vlcContinuesFrom
protected static boolean vlcContinuesFrom(byte octet) Checks if VLC continues from given byte, i.e. if high bit is set- Parameters:
octet
-- Returns:
- True if VLC coding continues, false otherwise
-
readVLCLong
- Throws:
BadFormatException
-
readVLCLong
Reads a VLC encoded long as a long from the given location in a byte array. Assumes no tag- Parameters:
data
- Byte arraypos
- Position from which to read in byte array- Returns:
- long value from byte array
- Throws:
BadFormatException
- If format is invalid, or reading beyond end of array
-
readVLCCount
Reads a VLC encoded count (non-negative integer) as a long from the given location in a byte array. Assumes no tag- Parameters:
data
- Byte arraypos
- Position from which to read in byte array- Returns:
- long value from byte array
- Throws:
BadFormatException
- If format is invalid, or reading beyond end of array
-
readVLCCount
- Throws:
BadFormatException
-
peekMessageLength
Peeks for a VLC encoded message length at the start of a ByteBuffer, which must contain at least 1 byte, maximum 2. Does not move the buffer position.- Parameters:
bb
- ByteBuffer containing a message length- Returns:
- The message length, or negative if insufficient bytes
- Throws:
BadFormatException
- If the ByteBuffer does not start with a valid message length
-
writeMessageLength
Writes a message length as a VLC encoded long- Parameters:
bb
- ByteBuffer with capacity available for writinglen
- Length of message to write- Returns:
- The ByteBuffer after writing the message length
-
write
Writes a cell encoding to a byte array, preceded by the appropriate tag- Parameters:
bs
- Byte array to write topos
- Starting position to write in byte arraycell
- Cell to write (may be null)- Returns:
- Position in byte array after writing the specified object
-
writeRawUTF8String
Writes a raw string without tag to the byte array. Includes length in bytes of UTF-8 representation- Parameters:
bs
- Byte arraypos
- Starting position to write in byte arrays
- String to write- Returns:
- Position in byte array after writing
-
readUTF8String
Reads a UTF-8 String from a ByteBuffer. Assumes any tag has already been read- Parameters:
bb
- ByteBuffer to read fromlen
- Number of UTF-8 bytes to read- Returns:
- String from ByteBuffer
- Throws:
BadFormatException
- If encoding is invalid
-
readUTF8String
Reads UTF-8 String data from a Blob. Assumes any tag has already been read- Parameters:
blob
- Blob data to read frompos
- Position of first UTF-8 bytelen
- Number of UTF-8 bytes to read- Returns:
- String from ByteBuffer
- Throws:
BadFormatException
- If there are insufficient bytes in Blob to read
-
writeLength
-
readRef
Reads a Ref or embedded Cell value from a Blob Converts Embedded Cells to Direct Refs automatically.- Type Parameters:
T
- Type of referenced value- Parameters:
b
- Blob containing a ref to readpos
- Position to read Ref from (should point to tag)- Returns:
- Ref as read from ByteBuffer
- Throws:
BadFormatException
- If the data is badly formatted, or a non-embedded value is found where it should be a Ref.
-
read
Decodes a single Value from a Blob. Assumes the presence of a tag. throws an exception if the Blob contents are not fully consumed- Parameters:
blob
- Blob representing the Encoding of the Value- Returns:
- Value read from the blob of encoded data
- Throws:
BadFormatException
- In case of encoding error
-
read
Decodes a single Value from a Blob, starting at a given offset Assumes the presence of a tag. throws an exception if the Blob contents are not fully consumed- Parameters:
blob
- Blob representing the Encoding of the Valueoffset
- Offset of tag byte in blob- Returns:
- Value read from the blob of encoded data
- Throws:
BadFormatException
- In case of encoding error
-
read
Helper method to read a value encoded as a hex string- Type Parameters:
T
- Type of value to read- Parameters:
hexString
- A valid hex String- Returns:
- Value read
- Throws:
BadFormatException
- If encoding is invalid
-
isCanonical
Returns true if the object is a canonical data object. Canonical data objects can be used as first class decentralised data objects.- Parameters:
o
- Value to test- Returns:
- true if object is canonical, false otherwise.
-
isEmbedded
Determines if an object should be embedded directly in the encoding rather than referenced with a Ref / hash. Defined to be true for most small objects.- Parameters:
cell
- Value to test- Returns:
- true if object is embedded, false otherwise
-
encodedBlob
-
writeHexDigits
Writes hex digits from digit position start, total length. Fills final hex digit with 0 if length is odd.- Parameters:
bs
- Byte arraypos
- Position to write into byte arraysrc
- Source Blob for hex digitsstart
- Start position in source blob (hex digit number from beginning)length
- Number of hex digits to write- Returns:
- position after writing
-
encodedString
-
encodedString
-
estimateEncodingSize
Estimate the encoding size of a Cell value. Useful for pre-sizing buffers.- Parameters:
cell
- Cell to estimate encoding size for- Returns:
- Estimated encoding size. May not be precise.
-
decodeCells
Decodes an array of Cells packed in a Blob. Cells may be partial.- Parameters:
data
- Data containing cell encodings in multi-cell format- Returns:
- Array of decoded cells
- Throws:
BadFormatException
- In event of any encoding error detected
-
decodeMultiCell
Reads a cell from a Blob of data, allowing for non-embedded children following the first cell- Parameters:
data
- Data to decode- Returns:
- Cell instance
- Throws:
BadFormatException
- If encoding format is invalid
-
decodeCells
Decode encoded non-embedded Cells into an accumulator HashMap- Parameters:
acc
- Accumulator for Cells, keyed by Hashdata
- Encoding to read- Throws:
BadFormatException
- In case of bad format, including any embedded values
-
encodeMultiCell
-
encodeCells
-
encodeDelta
-
getEncodingLength
-
readLong
Reads a long value represented by the specified bytes in a Blob- Parameters:
blob
- Blob instanceoffset
- Offset into bloblength
- Length in bytes to read- Returns:
- Long value
- Throws:
BadFormatException
- If the Long format is not canonical (i.e. starts with 0x00)
-
getLongLength
public static int getLongLength(long value) Gets the length of a Long in bytes (minimum bytes needs to express value- Parameters:
value
- Long value to analyse- Returns:
- Number of bytes needed to express Long
-