Package convex.core.data
Class Blob
- All Implemented Interfaces:
IValidated,IWriteable,Comparable<ABlob>
General purpose immutable wrapper for byte array data.
Can be serialised directly if 4096 bytes or less, otherwise needs to be
structures as a BlobTree.
Encoding format is:
- Tag.BLOB tag byte
- VLC encoded Blob length in bytes (one or two bytes describing a length in range 0..4096)
- Byte data of the given length
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic intstatic Blobstatic intMaximum encoding size for a regular Blobstatic BlobFields inherited from class convex.core.data.AArrayBlob
length, offset, storeFields inherited from class convex.core.data.ABlob
contentHashFields inherited from class convex.core.data.ACell
cachedRef, EMPTY_ARRAY -
Method Summary
Modifier and TypeMethodDescriptionvoidattachContentHash(Hash hash)static Blobcreate(byte[] data)Creates a new data object using a copy of the specified byte array.static Blobcreate(byte[] data, int offset, int length)Creates a new data object using a copy of the specified byte rangestatic BlobcreateRandom(Random random, long length)Creates a Blob of random bytes of the given lengthintencode(byte[] bs, int pos)Writes this Cell's encoding to a byte array, including a tag byte which will be written firstbooleanequals(AArrayBlob other)Equality for array Blob objects Implemented by testing equality of byte databooleanDetermines if this Blob is equal to another Blob.booleanintEstimate the encoded data size for this Cell.static BlobfromByteBuffer(ByteBuffer bb)Constructs a Blob object from all remaining bytes in a ByteBufferstatic BlobConstructs a Blob object from a hex stringGets a byte buffer containing this Blob's data.getChunk(long i)Gets a chunk of this Blob, as a canonical Blob up to the maximum chunk sizebytegetTag()Gets the tag byte for this cell.booleanReturns true if this Cell is in a canonical representation for message writing.booleanReturns true if this object represents a first class CVM Value.booleanReturns true if this object is a regular blob (i.e.static AArrayBlobFast read of a Blob from its representation insider another Blob object, Main benefit is to avoid reconstructing via ByteBuffer allocation, enabling retention of source Blob object as encoded data.slice(long start, long length)Gets a contiguous slice of this blob, as a new Blob.toBlob()Converts this object to a Blob instanceConverts this Cell to its canonical version.static Blobwrap(byte[] data)Wraps the specified bytes as a Data object Warning: underlying bytes are used directly.static Blobwrap(byte[] data, int offset, int length)Wraps the specified bytes as a Data object Warning: underlying bytes are used directly.Methods inherited from class convex.core.data.AArrayBlob
append, byteAt, commonHexPrefixLength, compareTo, compareTo, count, encodeRaw, equalsBytes, equalsBytes, getBytes, getHexDigit, getInternalArray, getInternalOffset, getRefCount, getUnchecked, hexMatches, hexMatchLength, longValue, rangeMatches, rangeMatches, slice, toHexString, toHexString, toLong, updateDigest, validate, validateCell, writeToBuffer, writeToBufferMethods inherited from class convex.core.data.ABlob
computeHash, empty, equals, get, getBytes, getContentHash, getElementRef, getType, hashCode, hexEquals, hexEquals, hexLength, print, toByteBuffer, toHexStringMethods inherited from class convex.core.data.ACountable
isEmpty, sizeMethods inherited from class convex.core.data.ACell
announce, announce, attachMemorySize, attachRef, cachedEncoding, cachedHash, calcMemorySize, createAnnounced, createEncoding, createPersisted, createPersisted, createRef, equals, getChildRefs, getEncoding, getEncodingLength, getHash, getMemorySize, getRef, getRef, isEmbedded, mark, mark, toString, updateRefs, writeMethods inherited from class convex.core.data.AObject
attachEncoding, print
-
Field Details
-
EMPTY
-
NULL_ENCODING
-
CHUNK_LENGTH
public static final int CHUNK_LENGTH- See Also:
- Constant Field Values
-
MAX_ENCODING_LENGTH
public static final int MAX_ENCODING_LENGTHMaximum encoding size for a regular Blob
-
-
Method Details
-
create
Creates a new data object using a copy of the specified byte range- Parameters:
data- Byte arrayoffset- Start offset in the byte arraylength- Number of bytes to take from data array- Returns:
- The new Data object
-
create
Creates a new data object using a copy of the specified byte array.- Parameters:
data- Byte array- Returns:
- Blob with the same byte contents as the given array
-
wrap
Wraps the specified bytes as a Data object Warning: underlying bytes are used directly. Use only if no other references to the byte array are kept which might be mutated.- Parameters:
data- Byte array- Returns:
- Blob wrapping the given data
-
wrap
Wraps the specified bytes as a Data object Warning: underlying bytes are used directly. Use only if no other references to the byte array are kept which might be mutated.- Parameters:
data- Byte arrayoffset- Offset into byte arraylength- Length of byte array to wrap- Returns:
- Blob wrapping the given byte array segment
-
toBlob
Description copied from class:ABlobConverts this object to a Blob instance- Overrides:
toBlobin classAArrayBlob- Returns:
- A Blob instance containing the same data as this Blob.
-
slice
Description copied from class:ABlobGets a contiguous slice of this blob, as a new Blob. Shares underlying backing data where possible- Overrides:
slicein classAArrayBlob- Parameters:
start- Start position for the created slicelength- Length of the slice- Returns:
- A blob of the specified length, representing a slice of this blob.
-
equals
Description copied from class:ABlobDetermines if this Blob is equal to another Blob. Blobs are defined to be equal if they have the same on-chain representation, i.e. if and only if all of the following are true: - Blob is of the same general type - Blobs are of the same length - All byte values are equal -
equals
-
equals
Equality for array Blob objects Implemented by testing equality of byte data- Parameters:
other- Blob to comapre with- Returns:
- true if blobs are equal, false otherwise.
-
fromHex
Constructs a Blob object from a hex string- Parameters:
hexString- Hex String to read- Returns:
- Blob with the provided hex value, or null if not a valid blob
-
fromByteBuffer
Constructs a Blob object from all remaining bytes in a ByteBuffer- Parameters:
bb- ByteBuffer- Returns:
- Blob containing the contents read from the ByteBuffer
-
getByteBuffer
Description copied from class:ABlobGets a byte buffer containing this Blob's data. Will have remaining bytes equal to this Blob's size.- Overrides:
getByteBufferin classAArrayBlob- Returns:
- A ByteBuffer containing the Blob's data.
-
read
Fast read of a Blob from its representation insider another Blob object, Main benefit is to avoid reconstructing via ByteBuffer allocation, enabling retention of source Blob object as encoded data.- Parameters:
source- Source Blob object.len- Length in bytes to take from the source Blob- Returns:
- Blob read from the source
- Throws:
BadFormatException- If encoding is invalid
-
encode
public int encode(byte[] bs, int pos)Description copied from class:ACellWrites this Cell's encoding to a byte array, including a tag byte which will be written first- Specified by:
encodein interfaceIWriteable- Specified by:
encodein classACell- Parameters:
bs- A byte array to which to write the encodingpos- The offset into the byte array- Returns:
- New position after writing
-
estimatedEncodingSize
public int estimatedEncodingSize()Description copied from interface:IWriteableEstimate the encoded data size for this Cell. Used for quickly sizing buffers. Implementations should try to return a size that is likely to contain the entire object when represented in binary format, including the tag byte.- Returns:
- The estimated size for the binary representation of this object.
-
isCanonical
public boolean isCanonical()Description copied from class:ACellReturns true if this Cell is in a canonical representation for message writing. Non-canonical objects may be used on a temporary internal basis, they must always be converted to canonical representations for external use (e.g. Encoding).- Specified by:
isCanonicalin classACell- Returns:
- true if the object is in canonical format, false otherwise
-
isCVMValue
public final boolean isCVMValue()Description copied from class:ACellReturns true if this object represents a first class CVM Value. Sub-structural cells that are not themselves first class values should return false. CVM values might not be in a canonical format, e.g. temporary data structures- Specified by:
isCVMValuein classACell- Returns:
- true if the object is a CVM Value, false otherwise
-
createRandom
Creates a Blob of random bytes of the given length- Parameters:
random- Any Random generator instancelength- Length of blob to generate in bytes- Returns:
- Blob with the specified number of random bytes
-
getChunk
Description copied from class:ABlobGets a chunk of this Blob, as a canonical Blob up to the maximum chunk size -
attachContentHash
-
isRegularBlob
public boolean isRegularBlob()Description copied from class:ABlobReturns true if this object is a regular blob (i.e. not a special blob type like Address)- Overrides:
isRegularBlobin classABlob- Returns:
- True if a regular blob
-
getTag
public byte getTag()Description copied from class:ACellGets the tag byte for this cell. The tag byte is always written as the first byte of the Cell's Encoding -
toCanonical
Description copied from class:ACellConverts this Cell to its canonical version. Returns this Cell if already canonical.- Specified by:
toCanonicalin classABlob- Returns:
- Canonical version of Cell
-