Package convex.core.data
Class ABlob
- All Implemented Interfaces:
IValidated
,IWriteable
,Comparable<ABlob>
- Direct Known Subclasses:
AArrayBlob
,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, EMPTY_ARRAY
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract ABlob
Append an additional data object to this, creating a new data object.byte
byteAt(long i)
Gets the byte at the specified position in this blobabstract long
Computes the length of the longest common hex prefix between two blobsint
Compares this blob to another blob, in lexographic order sorting by first bytes.computeHash(MessageDigest digest)
Computes the hash of the byte data stored in this Blob, using the given MessageDigest.abstract long
count()
Gets the length of this Blobempty()
abstract 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, int byteOffset)
Tests if this Blob is 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 index in this collectionabstract ByteBuffer
Gets a byte buffer containing this Blob's data.byte[]
getBytes()
Gets a byte array containing a copy of this data object.abstract void
getBytes(byte[] dest, int destOffset)
Copies the bytes from this blob to a given destinationabstract Blob
getChunk(long i)
Gets a chunk of this Blob, as a canonical Blob up to the maximum chunk sizeComputes 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 index in this collectionint
getHexDigit(long digitPos)
Gets the specified hex digit from this data object.getType()
Gets the most specific known runtime Type for this Cell.abstract byte
getUnchecked(long i)
Gets the byte at the specified position in this data object, without bounds checking.int
hashCode()
Gets the Java hashCode for this cell.boolean
boolean
long
abstract long
hexMatchLength(ABlob b, long start, long length)
Returns the number of matching hex digits in the given hex range of another blob.boolean
Returns true if this object is a regular blob (i.e.abstract long
Gets the long value of this Blob if the length is exactly 8 bytes, otherwise throws an Exceptionvoid
print(StringBuilder sb)
Prints this Object to a readable String Representationslice(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 length)
Gets a contiguous slice of this blob, as a new Blob.abstract Blob
toBlob()
Converts this object to a Blob instanceConverts this blob to a readable byte bufferabstract ABlob
Converts this Cell to its canonical version.abstract String
Converts this data object to a lowercase hex string representationtoHexString(int length)
Converts this data object to a hex string representation of the given length.abstract void
toHexString(StringBuilder sb)
abstract long
toLong()
Converts this Blob to the corresponding long value.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.abstract int
writeToBuffer(byte[] bs, int pos)
Writes the raw byte contents of this blob to a byte arrayabstract ByteBuffer
writeToBuffer(ByteBuffer bb)
Writes the raw byte contents of this blob to a ByteBuffer.Methods inherited from class convex.core.data.ACountable
isEmpty, size
Methods inherited from class convex.core.data.ACell
announce, announce, attachMemorySize, attachRef, cachedEncoding, cachedHash, calcMemorySize, createAnnounced, createEncoding, createPersisted, createPersisted, createRef, encode, encodeRaw, equals, getChildRefs, getEncoding, getEncodingLength, getHash, getMemorySize, getRef, getRef, getRefCount, getTag, isCanonical, isCVMValue, isEmbedded, toString, updateRefs, write
Methods inherited from class convex.core.data.AObject
attachEncoding, 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
-
contentHash
Cached hash of the Blob data. Might be null.
-
-
Constructor Details
-
ABlob
public ABlob()
-
-
Method Details
-
getType
Description copied from class:ACell
Gets the most specific known runtime Type for this Cell. -
getBytes
public abstract void getBytes(byte[] dest, int destOffset)Copies the bytes from this blob to a given destination- Parameters:
dest
- Destination arraydestOffset
- Offset into destination array
-
count
public abstract long count()Gets the length of this Blob- Specified by:
count
in classACountable<CVMByte>
- Returns:
- The length in bytes of this data object
-
get
Description copied from class:ACountable
Gets the element at the specified index in this collection- Specified by:
get
in classACountable<CVMByte>
- 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 index in this collection- Specified by:
getElementRef
in classACountable<CVMByte>
- Parameters:
index
- Index of element to get- Returns:
- Element at the specified index
-
empty
-
toHexString
Converts this data object to a lowercase hex string representation- Returns:
- Hex String representation
-
toByteBuffer
Converts this blob to a readable byte buffer- Returns:
- ByteBuffer with position zero (ready to read)
-
toHexString
Converts this data object to a hex string representation of the given length. Equivalent to truncating the full String representation.- Parameters:
length
- Length to truncate String to (in hex characters)- Returns:
- String representation of hex values in Blob
-
slice
Gets a contiguous slice of this blob, as a new Blob. Shares underlying backing data where possible- 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.
-
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- Parameters:
start
- Start position to slice from- Returns:
- Slice of Blob
-
toBlob
Converts this object to a Blob instance- Returns:
- A Blob instance containing the same data as this Blob.
-
commonHexPrefixLength
Computes the length of the longest common hex prefix between two blobs- Parameters:
b
- Blob to compare with- Returns:
- The length of the longest common prefix in hex digits
-
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
public byte byteAt(long i)Gets the byte at the specified position in this blob- Parameters:
i
- Index of the byte to get- Returns:
- The byte at the specified position
-
getUnchecked
public abstract byte getUnchecked(long i)Gets the byte at the specified position in this data object, without bounds checking. Only safe if index is known to be in bounds, otherwise result is undefined.- Parameters:
i
- Index of the byte to get- Returns:
- The byte at the specified position
-
getHexDigit
public int getHexDigit(long digitPos)Gets the specified hex digit from this data object. Result is undefined if index is out of bounds.- Parameters:
digitPos
- The position of the hex digit- Returns:
- The value of the hex digit, in the range 0-15 inclusive
-
getBytes
public byte[] getBytes()Gets a byte array containing a copy of this data object.- Returns:
- A new byte array containing the contents of this blob.
-
append
Append an additional data object to this, creating a new data object.- Parameters:
d
- Blob to append- Returns:
- A new blob, containing the additional data appended to this blob.
-
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 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- Parameters:
o
- Blob to compare with- Returns:
- true if Blobs are equal, false otherwise
-
toCanonical
Description copied from class:ACell
Converts this Cell to its canonical version. Returns this if already canonical- Specified by:
toCanonical
in classACell
- Returns:
- Canonical version of Cell
-
equalsBytes
public abstract boolean equalsBytes(byte[] bytes, int byteOffset)Tests if this Blob is equal to a subset of a byte array- Parameters:
bytes
- Byte array to compare withbyteOffset
- Offset into byte array- Returns:
- true if exactly equal, false otherwise
-
compareTo
Compares this blob to another blob, in lexographic order sorting by first bytes. Note: This means that compareTo does not precisely match equality, because different blob types may be lexicographically equal but represent different values.- Specified by:
compareTo
in interfaceComparable<ABlob>
-
writeToBuffer
Writes the raw byte contents of this blob to a ByteBuffer.- Parameters:
bb
- ByteBuffer to write to- Returns:
- The passed ByteBuffer, after writing byte content
-
writeToBuffer
public abstract int writeToBuffer(byte[] bs, int pos)Writes the raw byte contents of this blob to a byte array- Parameters:
bs
- Byte array to write topos
- Starting position in byte array to write to- Returns:
- The position in the array after writing
-
getChunk
Gets a chunk of this Blob, as a canonical Blob up to the maximum chunk size- 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 -
getByteBuffer
Gets a byte buffer containing this Blob's data. Will have remaining bytes equal to this Blob's size.- Returns:
- A ByteBuffer containing the Blob's data.
-
toHexString
-
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 Valie 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
-
hexMatchLength
Returns the number of matching hex digits in the given hex range of another blob. Assumes range is valid for both blobs. Returns length if this Blob is exactly equal to the specified hex range.- Parameters:
start
- Start position (in hex digits)length
- Length to compare (in hex digits)b
- Blob to compare with- Returns:
- The number of matching hex characters
-
hexEquals
-
hexEquals
-
hexLength
public long hexLength() -
toLong
public abstract long toLong()Converts this Blob to the corresponding long value. Assumes big-endian format, as if the entire blob is interpreted as a signed big integer. Higher bytes outside the Long range will be ignored.- Returns:
- long value of this blob
-
hashCode
public int hashCode()Description copied from class:ACell
Gets the Java hashCode for this cell. Must be consistent with equals. Default is the first bytes (big-endian) of the Cell Encoding's hash, since this is consistent with encoding-based equality. However, different Types may provide more efficient hashcodes provided that the usual invariants are preserved -
longValue
public abstract long longValue()Gets the long value of this Blob if the length is exactly 8 bytes, otherwise throws an Exception- Returns:
- The long value represented by the Blob
-
isRegularBlob
public boolean isRegularBlob()Returns true if this object is a regular blob (i.e. not a special blob type like Hash or Address)- Returns:
- True if a regular blob
-
equalsBytes
Tests if this Blob has exactly the same bytes as another Blob- Parameters:
b
- Blob to compare with- Returns:
- True if byte content is exactly equal, false otherwise
-