Package convex.core.data
Class Blob
- All Implemented Interfaces:
IValidated,IWriteable,Comparable<ABlobLike<?>>
General purpose immutable wrapper for byte array data.
Can be encoded fully as a single Cell 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 final intstatic final Blobstatic final Blobstatic final intMaximum encoding size for a regular Blobstatic final Blobstatic final Blobstatic final BlobFields inherited from class convex.core.data.AArrayBlob
contentHash, offset, storeFields inherited from class convex.core.data.ACell
cachedRef, memorySize -
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 Blob using a copy of the specified byte rangestatic BlobcreateRandom(Random random, long length) Creates a Blob of random bytes of the given lengthintencodeRaw(byte[] bs, int pos) Encodes this Blob, excluding tag byte (will include count)booleanequals(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 BlobforByte(byte b) static BlobConstructs a Blob object from all remaining bytes in a ByteBufferstatic BlobConstructs a Blob object from a hex stringgetChunk(long i) Gets a chunk of this Blob, as a canonical flat Blob up to the maximum Blob chunk size.booleanReturns true if this Cell is in a canonical representation.static BlobParses String input as a Blob.static BlobFast read of a Blob from its encoding inside another Blob object,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.slice(long start, long end) Gets a contiguous slice of this Blob, as a new Blob.Converts this Cell to a canonical version.Converts the contents of this value to a flat array-backed Blob instance.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, appendHex, appendSmall, byteAt, byteAtUnchecked, calcMemorySize, compareTo, compareTo, equalsBytes, equalsBytes, getByteBuffer, getBytes, getContentHash, getHexDigit, getInternalArray, getInternalOffset, getRef, getRefCount, hexMatch, hexMatch, hexMatches, isChunkPacked, isFullyPacked, longAt, longValue, rangeMatches, rangeMatches, read, shortAt, updateDigest, updateRefs, validate, validateCellMethods inherited from class convex.core.data.ABlob
computeHash, count, empty, encode, equals, get, getElementRef, getTag, getType, hashCode, isCVMValue, print, read, replaceSlice, size, toBlob, toByteBufferMethods inherited from class convex.core.data.ABlobLike
getBytes, hexEquals, hexLength, isDataValue, toHexString, toHexStringMethods inherited from class convex.core.data.ACountable
isEmptyMethods inherited from class convex.core.data.ACell
attachMemorySize, attachRef, cachedEncoding, cachedHash, createEncoding, createRef, equals, genericEquals, getCanonical, getChildRefs, getEncoding, getEncodingLength, getHash, getMemorySize, getMemorySize, getRef, isEmbedded, toCVMString, toStringMethods inherited from class convex.core.data.AObject
attachEncoding, print, print
-
Field Details
-
EMPTY
-
ZERO
-
ONE
-
NULL_ENCODING
-
CHUNK_LENGTH
public static final int CHUNK_LENGTH- See Also:
-
EMPTY_CHUNK
-
MAX_ENCODING_LENGTH
public static final int MAX_ENCODING_LENGTHMaximum encoding size for a regular Blob
-
-
Method Details
-
create
Creates a new Blob 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
-
parse
-
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
-
toFlatBlob
Description copied from class:ABlobLikeConverts 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:
toFlatBlobin classAArrayBlob- Returns:
- A Blob instance containing the same data as this value. Might be `this` if already a flat 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 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
Description copied from class:ABlobGets 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 -
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 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- Overrides:
equalsin classAArrayBlob- Parameters:
a- Blob to compare with- Returns:
- true if Blobs are equal, false otherwise
-
equals
-
equals
Equality for array Blob objects Implemented by testing equality of byte data- Parameters:
other- Blob to compare with- Returns:
- true if blobs are equal, false otherwise.
-
fromHex
-
forByte
-
fromByteBuffer
Constructs a Blob object from all remaining bytes in a ByteBuffer- Parameters:
bb- ByteBuffer- Returns:
- Blob containing the contents read from the ByteBuffer
-
read
Fast read of a Blob from its encoding inside another Blob object,- Parameters:
source- Source Blob object.pos- Position in source to start reading from (location of tag byte)count- Length in bytes to take from the source Blob- Returns:
- Blob read from the source
- Throws:
BadFormatException- If encoding is invalid
-
encodeRaw
public int encodeRaw(byte[] bs, int pos) Description copied from class:AArrayBlobEncodes this Blob, excluding tag byte (will include count)- Overrides:
encodeRawin classAArrayBlob- 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 highly likely to contain the entire object when encoded, including the tag byte. Should not traverse soft Refs, i.e. must be usable on arbitrary partial data structures- 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. Non-canonical objects may be used on a temporary internal basis, they should be converted to canonical representations for general purpose use.- Specified by:
isCanonicalin classACell- Returns:
- true if the object is in canonical format, false otherwise
-
createRandom
-
getChunk
Description copied from class:ABlobGets 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 -
attachContentHash
-
toCanonical
Description copied from class:ACellConverts 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:
toCanonicalin classABlob- Returns:
- Canonical version of Cell
-