public abstract class SimpleDataStructure extends Object implements DataStructure
SimpleDataStructure provides efficient storage and caching for fixed-size data:
Key Features:
length() method defines sizegetData()Caching System:
create() methods for cache accessImmutability Model:
RuntimeExceptionUsage Patterns:
Performance Optimizations:
Common Subclasses:
Hash - 32-byte SHA-256 hashesPublicKey - Variable-length encryption keysPrivateKey - Variable-length decryption keysSigningPublicKey - Variable-length signing keysSigningPrivateKey - Variable-length signing private keysSessionKey - 32-byte session encryption keysSignature - Variable-length digital signaturesThread Safety:
Evolution:
| Modifier and Type | Field and Description |
|---|---|
protected byte[] | _dataThe byte array data for this structure. |
| Constructor and Description |
|---|
SimpleDataStructure()A new instance with the data set to null. |
SimpleDataStructure(byte[] data)Creates a SimpleDataStructure with the given data. |
| Modifier and Type | Method and Description |
|---|---|
Hash | calculateHash()SHA256 hash of the byte array, or null if data is null. |
boolean | equals(Object obj)Warning - this returns true for two different classes with the same size
and same data, e.g. |
void | fromBase64(String data)Sets the data. |
void | fromByteArray(byte[] data)Does the same thing as setData() but null not allowed. |
byte[] | getData()Get the data reference (not a copy) |
int | hashCode()We assume the data has enough randomness in it, so use the first 4 bytes for speed. |
abstract int | length()The legal length of the byte array in this data structure. |
protected int | read(InputStream in,
byte[] target)Repeated reads until the buffer is full or IOException is thrown. |
void | readBytes(InputStream in)Sets the data. |
void | setData(byte[] data)Sets the data. |
String | toBase64()Renders the structure into modified base 64 notation. |
byte[] | toByteArray()Same thing as getData(). |
String | toString()Returns a string representation of this data structure. |
void | writeBytes(OutputStream out)Writes the data to the output stream. |
public SimpleDataStructure()
public SimpleDataStructure(byte[] data)
data - the byte array, or nullIllegalArgumentException - if data is not the legal number of bytes (but null is ok)public Hash calculateHash()
calculateHash in interface DataStructurepublic boolean equals(Object obj)
public void fromBase64(String data) throws DataFormatException
fromBase64 in interface DataStructuredata - base64 encoded string to load fromDataFormatException - if decoded data is not the legal number of bytes or on decoding errorRuntimeException - if data already set.public void fromByteArray(byte[] data)
throws DataFormatExceptionfromByteArray in interface DataStructuredata - non-null byte arrayDataFormatException - if null or wrong lengthRuntimeException - if data already setpublic byte[] getData()
public int hashCode()
public abstract int length()
protected int read(InputStream in, byte[] target) throws IOException
in - the stream to read fromtarget - the buffer to fillIOException - on IO errorpublic void readBytes(InputStream in) throws DataFormatException, IOException
readBytes in interface DataStructurein - the stream to readRuntimeException - if data already set.DataFormatException - if the data is improperly formattedIOException - if there was a problem reading the streampublic void setData(byte[] data)
data - of correct length, or nullIllegalArgumentException - if data is not the legal number of bytes (but null is ok)RuntimeException - if data already set.public String toBase64()
toBase64 in interface DataStructurepublic byte[] toByteArray()
toByteArray in interface DataStructurepublic String toString()
public void writeBytes(OutputStream out) throws DataFormatException, IOException
writeBytes in interface DataStructureout - stream to write toDataFormatException - if the data was incomplete or not yet ready to be writtenIOException - if there was a problem writing to the stream