Package convex.core.data
Class ABlob
- All Implemented Interfaces:
IValidated
,IWriteable
,Comparable<ABlobLike<?>>
- Direct Known Subclasses:
AArrayBlob
,ADerivedBlob
,ALongBlob
,BlobTree
Abstract base class for data objects containing immutable chunks of binary
data. Representation is equivalent to a fixed size immutable byte sequence.
Rationale: - Allow data to be encapsulated as an immutable object - Provide
specialised methods for processing byte data - Provide a cached Hash value,
lazily computed on demand
-
Field Summary
FieldsFields inherited from class convex.core.data.ACell
cachedRef, memorySize
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract ABlob
Append an additional Blob to this, creating a new Blob as needed.byte
byteAt
(long i) Gets the byte at the specified positionint
Compares this Blob to another Blob, in lexicographic order sorting by first bytes (unsigned).final Hash
computeHash
(MessageDigest digest) Computes the hash of the byte data stored in this Blob, using the given MessageDigest.final long
count()
Gets the length of this Blobfinal Blob
empty()
Returns a canonical, singleton empty instance of the same type as this Countable value.final int
encode
(byte[] bs, int pos) Writes this Cell's encoding to a byte array, including a tag byte which will be written first.abstract int
encodeRaw
(byte[] bs, int pos) Writes this Blob's encoding to a byte array, excluding the tag byteabstract boolean
Determines if this Blob is equal to another Blob.boolean
Determines if this Blob is equal to another Object.abstract boolean
equalsBytes
(byte[] bytes, long offset) Tests if the byte contents of this instance are equal to a subset of a byte arrayabstract boolean
equalsBytes
(ABlob b) Tests if this Blob has exactly the same bytes as another Blobget
(long ix) Gets the element at the specified element index in this valueabstract ByteBuffer
Gets a byte buffer containing this Blob's raw data.abstract Blob
getChunk
(long i) Gets a chunk of this Blob, as a canonical flat Blob up to the maximum Blob chunk size.Computes the hash of the byte data stored in this Blob, using the default MessageDigest.getElementRef
(long index) Gets a Ref to the element at the specified element index in this collectionfinal byte
getTag()
Gets the tag byte for this cell.final AType
getType()
Gets the most specific known runtime Type for this Cell.int
hashCode()
Gets the Java hashCode for this cell.boolean
Returns true if this Blob is a fully packed set of chunksfinal boolean
Returns true if this cell is a first class CVM Value allowable in the CVM state Sub-structural cells that are not themselves first class values should return false Records and types that are not permissible on the CVM should return false.abstract boolean
Returns true if this is a fully packed set of chunksboolean
print
(BlobBuilder bb, long limit) Prints this Object to a readable String Representation.abstract int
read
(long offset, long count, ByteBuffer dest) Gets bytes from this Blob into a ByteBufferint
read
(long offset, ByteBuffer dest) Gets bytes from this Blob into a ByteBufferreplaceSlice
(long position, ABlob b) Replaces a slice of this Blob, returning a new Blobshort
shortAt
(long i) final int
size()
Gets the size of this data structure as an int.slice
(long start) Gets a slice of this blob, as a new blob, starting from the given offset and extending to the end of the blob.abstract ABlob
slice
(long start, long end) Gets a contiguous slice of this Blob, as a new Blob.final ABlob
toBlob()
Convert this BlobLike object to an ABlob instance, in the most efficient way.Converts this blob to a readable byte buffer.protected abstract ABlob
Converts this Cell to a canonical version.abstract Blob
Converts the contents of this value to a flat array-backed Blob instance.protected abstract void
updateDigest
(MessageDigest digest) void
validate()
Validates the complete structure of this object.void
Validates the local structure and invariants of this cell.Methods inherited from class convex.core.data.ABlobLike
appendHex, byteAtUnchecked, getBytes, getBytes, getHexDigit, hexEquals, hexLength, hexMatch, hexMatch, isDataValue, longValue, toHexString, toHexString
Methods inherited from class convex.core.data.ACountable
isEmpty
Methods inherited from class convex.core.data.ACell
attachMemorySize, attachRef, cachedEncoding, cachedHash, calcMemorySize, createEncoding, createRef, equals, genericEquals, getCanonical, getChildRefs, getEncoding, getEncodingLength, getHash, getMemorySize, getMemorySize, getRef, getRef, getRefCount, isCanonical, isEmbedded, toCVMString, toString, updateRefs
Methods inherited from class convex.core.data.AObject
attachEncoding, print, print
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface convex.core.data.IWriteable
estimatedEncodingSize
-
Field Details
-
count
protected final long count
-
-
Constructor Details
-
ABlob
protected ABlob(long count)
-
-
Method Details
-
isChunkPacked
public boolean isChunkPacked()Returns true if this Blob is a fully packed set of chunks- Returns:
- True if fully packed, false otherwise
-
isFullyPacked
public abstract boolean isFullyPacked()Returns true if this is a fully packed set of chunks- Returns:
- True if fully packed, false otherwise
-
size
public final int size()Description copied from class:ACountable
Gets the size of this data structure as an int. Returns Integer.MAX_SIZE if the count is larger than can fit in an int. If this might be a problem, use count() instead.- Overrides:
size
in classACountable<CVMLong>
- Returns:
- Number of elements in this collection.
-
getType
-
count
public final long count()Gets the length of this Blob- Specified by:
count
in classACountable<CVMLong>
- Returns:
- The length in bytes of this data object
-
get
Description copied from class:ACountable
Gets the element at the specified element index in this value- Specified by:
get
in classACountable<CVMLong>
- Parameters:
ix
- Index of element to get- Returns:
- Element at the specified index
-
getElementRef
Description copied from class:ACountable
Gets a Ref to the element at the specified element index in this collection- Overrides:
getElementRef
in classABlobLike<CVMLong>
- Parameters:
index
- Index of element to get- Returns:
- Element at the specified index
-
empty
Description copied from class:ACountable
Returns a canonical, singleton empty instance of the same type as this Countable value. -
toByteBuffer
Converts this blob to a readable byte buffer. WARNING: may be large. May refer to underlying byte array so should not be mutated- Returns:
- ByteBuffer with position zero (ready to read)
-
slice
Gets a contiguous slice of this Blob, as a new Blob. Shares underlying backing data where possible- Specified by:
slice
in classACountable<CVMLong>
- Parameters:
start
- Start position for the created slice (inclusive)end
- End of the slice (exclusive)- Returns:
- A blob of the specified length, representing a slice of this blob, or null if the slice is invalid
-
slice
Gets a slice of this blob, as a new blob, starting from the given offset and extending to the end of the blob. Shares underlying backing data where possible. Returned Blob may not be the same type as the original Blob- Overrides:
slice
in classACountable<CVMLong>
- Parameters:
start
- Start position to slice from- Returns:
- Slice of Blob
-
toFlatBlob
Description copied from class:ABlobLike
Converts the contents of this value to a flat array-backed Blob instance. Warning: might be O(n) in size of Blob, may not be canonical etc.- Overrides:
toFlatBlob
in classABlobLike<CVMLong>
- Returns:
- A Blob instance containing the same data as this value. Might be `this` if already a flat Blob.
-
toBlob
-
getContentHash
Computes the hash of the byte data stored in this Blob, using the default MessageDigest. This is the correct hash ID for a data value if this blob contains the data value's encoding- Returns:
- The Hash
-
computeHash
Computes the hash of the byte data stored in this Blob, using the given MessageDigest.- Parameters:
digest
- MessageDigest instance- Returns:
- The hash
-
updateDigest
-
byteAt
-
append
-
equals
Determines if this Blob is equal to another Object. 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
Determines if this Blob is equal to another Blob. Blobs are defined to be equal if they have the same encoded representation, i.e. if and only if all of the following are true: - Blobs are of the same length - All byte values are equal- Parameters:
o
- Blob to compare with- Returns:
- true if Blobs are equal, false otherwise
-
toCanonical
Description copied from class:ACell
Converts this Cell to a canonical version. Must return this Cell if already canonical, may be O(n) in size of value otherwise. Callers should usually use getCanonical(), which caches canonical instances once created- Specified by:
toCanonical
in classACell
- Returns:
- Canonical version of Cell
-
equalsBytes
public abstract boolean equalsBytes(byte[] bytes, long offset) Tests if the byte contents of this instance are equal to a subset of a byte array- Parameters:
bytes
- Byte array to compare withoffset
- Offset into byte array from which to start comparison- Returns:
- true if exactly equal, false otherwise
-
compareTo
Compares this Blob to another Blob, in lexicographic order sorting by first bytes (unsigned). Note: This means that compareTo does not precisely match equality, because specialised Blob types may be lexicographically equal but represent different values. -
getChunk
Gets a chunk of this Blob, as a canonical flat Blob up to the maximum Blob chunk size. Returns empty Blob if and only if referencing the end of a Blob with fully packed chunks- Parameters:
i
- Index of chunk- Returns:
- A Blob containing the specified chunk data.
-
print
Description copied from class:AObject
Prints this Object to a readable String Representation. SECURITY: Must halt and return false in O(1) time when limit of printing is exceeded otherwise DoS attacks may be possible. -
getByteBuffer
Gets a byte buffer containing this Blob's raw data. Will have remaining bytes equal to this Blob's size.- Returns:
- A ByteBuffer containing the Blob's data.
-
validate
Description copied from interface:IValidated
Validates the complete structure of this object. It is necessary to ensure all child Refs are validated, so the general contract for validate is:- Call super.validate() - which will indirectly call validateCell()
- Call validate() on any contained cells in this class
- Specified by:
validate
in interfaceIValidated
- Overrides:
validate
in classACell
- Throws:
InvalidDataException
- If the data Value is invalid in any way
-
validateCell
Description copied from class:ACell
Validates the local structure and invariants of this cell. Called by validate() super implementation. Should validate directly contained data, but should not validate all other structure of this cell. In particular, should not traverse potentially missing child Refs.- Specified by:
validateCell
in classACell
- Throws:
InvalidDataException
- If the Cell is invalid
-
encode
public final int encode(byte[] bs, int pos) Description copied from class:ACell
Writes this Cell's encoding to a byte array, including a tag byte which will be written first. Cell must be canonical, or else an error may occur.- Specified by:
encode
in interfaceIWriteable
- Specified by:
encode
in classACell
- Parameters:
bs
- A byte array to which to write the encodingpos
- The offset into the byte array- Returns:
- New position after writing
-
encodeRaw
-
hashCode
public int hashCode()Description copied from class:ACell
Gets the Java hashCode for this cell. Must be consistent with equals. Default is the hashCode of the Encoding blob, since this is consistent with encoding-based equality. However, different Types may provide more efficient hashcodes provided that the usual invariants are preserved -
isCVMValue
public final boolean isCVMValue()Description copied from class:ACell
Returns true if this cell is a first class CVM Value allowable in the CVM state Sub-structural cells that are not themselves first class values should return false Records and types that are not permissible on the CVM should return false. Pretty much everything else should return true. Note: CVM values might not be in a canonical format, e.g. temporary data structures- Specified by:
isCVMValue
in classACell
- Returns:
- true if the object is a CVM Value, false otherwise
-
getTag
public final byte getTag()Description copied from class:ACell
Gets the tag byte for this cell. The tag byte is always equal to the first byte of the Cell's canonical Encoding, and is sufficient to distinguish how to read the rest of the encoding. -
equalsBytes
Tests if this Blob has exactly the same bytes as another Blob- Specified by:
equalsBytes
in classABlobLike<CVMLong>
- Parameters:
b
- Blob to compare with- Returns:
- True if byte content is exactly equal, false otherwise
-
shortAt
public short shortAt(long i) -
read
Gets bytes from this Blob into a ByteBuffer- Parameters:
offset
- Offset into this Blob to read fromdest
- Destination byte buffer- Returns:
- Number of bytes read
-
read
Gets bytes from this Blob into a ByteBuffer- Parameters:
offset
- Offset into this Blob to read fromcount
- Number of bytes to read. Must be in boundsdest
- Destination byte buffer- Returns:
- Number of bytes read
-
replaceSlice
-