java.lang.Object
net.i2p.data.SimpleDataStructure
- All Implemented Interfaces:
DataStructure
- Direct Known Subclasses:
EncryptedBuildRecord, Hash, Hash384, Hash512, NodeInfo, PrivateKey, PublicKey, SessionKey, SHA1Hash, Signature, SigningPrivateKey, SigningPublicKey
Base class for I2P data structures holding a single byte array in a protected
_data field.What the base class actually provides:
- Length:
length()is abstract, and subclasses fix it per type - 32 bytes forHashorSessionKey, 256 for an ElGamalPublicKey, and 20, 32, 64 or 256 for aSigningPublicKeydepending on the algorithm. It is a fixed length per subclass, not per instance. - Single assignment:
setData(byte[]),readBytes(InputStream)andfromBase64(String)all refuse to overwrite non-null data with aRuntimeException, and reject a wrong length.fromByteArray(byte[])andfromBase64(String)additionally reject null. - No copy:
getData()andtoByteArray()return the backing array itself, not a copy. - Encoding:
toBase64()and, where a subclass adds it, its own base32 rendering. - Comparison:
equals(Object)compares the raw arrays without checking the class or length, so two different SimpleDataStructure subclasses of equal length holding equal bytes compare equal (e.g. a 32-byteSessionKeyand a 32-byteHash).hashCode()uses the first 4 bytes only, which requires the data to be random - subclasses with structured data override it.
Not provided here:
- No cache: there is no LRU or any other cache in this class. Subclasses that want one supply their own static
SDSCacheand expose create() factory methods that go through it - seeHash. - Not thread-safe: nothing here is synchronized and
_datais not volatile, so an instance must be published safely (final field, or a happens-before edge) before other threads read it. Safe sharing applies only to a fully populated instance that nobody mutates - and callers that mutate the array returned bygetData()break that.
Field Summary
FieldsConstructor Summary
ConstructorsConstructorDescriptionA new instance with the data set to null.SimpleDataStructure(byte[] data) Creates a SimpleDataStructure with the given data.Method Summary
Modifier and TypeMethodDescriptionSHA256 hash of the byte array, or null if data is null.booleanCompares the raw data only: two different SimpleDataStructure subclasses of the same length holding the same bytes are equal, e.g.voidfromBase64(String data) Data from the base64 string.voidfromByteArray(byte[] data) Does the same thing assetData(byte[])but null not allowed.byte[]getData()Data reference (not a copy).inthashCode()We assume the data has enough randomness in it, so use the first 4 bytes for speed.abstract intlength()The legal length of the byte array in this data structure.protected intread(InputStream in, byte[] target) Repeated reads until the buffer is full or IOException is thrown.voidreadBytes(InputStream in) Data read from the stream.voidsetData(byte[] data) The byte array data, or null.toBase64()Renders the structure into modified base 64 notation.byte[]Same thing as getData().toString()Returns a string representation of this data structure.voidwriteBytes(OutputStream out) Writes the data to the output stream.
Field Details
_data
protected byte[] _dataThe byte array data for this structure.
Constructor Details
SimpleDataStructure
public SimpleDataStructure()A new instance with the data set to null. CallreadBytes(InputStream), setData(), or fromByteArray() after this to set the data.SimpleDataStructure
public SimpleDataStructure(byte[] data) Creates a SimpleDataStructure with the given data.- Parameters:
data- the byte array, or null- Throws:
IllegalArgumentException- if data is not the legal number of bytes (but null is ok)
Method Details
length
public abstract int length()The legal length of the byte array in this data structure.- Returns:
- the legal length of the byte array
getData
public byte[] getData()Data reference (not a copy). Mutating the returned array mutates this structure, and breaks the hashCode contract of any subclass that caches a hash of the data.- Returns:
- the backing array, always exactly
length()long or null before the data has been set
setData
public void setData(byte[] data) The byte array data, or null. Not a copy - see the class documentation.- Parameters:
data- of correct length, or null- Throws:
IllegalArgumentException- if data is not the legal number of bytes (but null is ok)RuntimeException- if data already set.
readBytes
Data read from the stream.- Specified by:
readBytesin interfaceDataStructure- Parameters:
in- the stream to read- Throws:
RuntimeException- if data already set.DataFormatException- if the data is improperly formattedIOException- if there was a problem reading the stream
read
Repeated reads until the buffer is full or IOException is thrown.- Parameters:
in- the stream to read fromtarget- the buffer to fill- Returns:
- number of bytes read (should always equal target.length)
- Throws:
IOException- on IO error
writeBytes
Writes the data to the output stream.- Specified by:
writeBytesin interfaceDataStructure- Parameters:
out- stream to write to- Throws:
DataFormatException- if the data was incomplete or not yet ready to be writtenIOException- if there was a problem writing to the stream
toBase64
Renders the structure into modified base 64 notation.- Specified by:
toBase64in interfaceDataStructure- Returns:
- null on error
fromBase64
Data from the base64 string.- Specified by:
fromBase64in interfaceDataStructure- Parameters:
data- base64 encoded string to load from- Throws:
DataFormatException- if decoded data is not the legal number of bytes or on decoding errorRuntimeException- if data already set.
calculateHash
SHA256 hash of the byte array, or null if data is null.- Specified by:
calculateHashin interfaceDataStructure- Returns:
- SHA256 hash, or null if there were problems (data format or io errors)
toByteArray
public byte[] toByteArray()Same thing as getData().- Specified by:
toByteArrayin interfaceDataStructure- Returns:
- same thing as getData()
fromByteArray
Does the same thing assetData(byte[])but null not allowed.- Specified by:
fromByteArrayin interfaceDataStructure- Parameters:
data- non-null byte array- Throws:
DataFormatException- if null or wrong lengthRuntimeException- if data already set
toString
hashCode
equals
Compares the raw data only: two different SimpleDataStructure subclasses of the same length holding the same bytes are equal, e.g. a 32-byte SessionKey and a 32-byte Hash. Only compare instances of the same class, and only use them as map keys or set members when that cannot happen.