Package net.i2p.data
Class SimpleDataStructure
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
public abstract class SimpleDataStructure extends Object implements DataStructure
A SimpleDataStructure contains only a single fixed-length byte array.
Implemented in 0.8.2 and retrofitted over several of the classes in this package.
As of 0.8.3, SDS objects may be cached. An SDS may be instantiated with null data,
and setData(null) is also OK. However,
once non-null data is set, the data reference is immutable;
subsequent attempts to set the data via setData(), readBytes(),
fromByteArray(), or fromBase64() will throw a RuntimeException.
As of 0.9.48, no longer extends DataStrucureImpl to save space
- Since:
- 0.8.2
- Author:
- zzz
-
Field Summary
Fields Modifier and Type Field Description protected byte[]_data -
Constructor Summary
Constructors Constructor Description SimpleDataStructure()A new instance with the data set to null.SimpleDataStructure(byte[] data) -
Method Summary
Modifier and Type Method Description HashcalculateHash()Calculate the SHA256 value of this object (useful for a few scenarios)booleanequals(Object obj)Warning - this returns true for two different classes with the same size and same data, e.g.voidfromBase64(String data)Sets the data.voidfromByteArray(byte[] data)Does the same thing as setData() but null not allowed.byte[]getData()Get the 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 structureprotected intread(InputStream in, byte[] target)Repeated reads until the buffer is full or IOException is thrownvoidreadBytes(InputStream in)Sets the data.voidsetData(byte[] data)Sets the data.StringtoBase64()render the structure into modified base 64 notationbyte[]toByteArray()StringtoString()voidwriteBytes(OutputStream out)Write out the data structure to the stream, using the format defined in the I2P data structure specification.
-
Field Details
-
_data
protected byte[] _data
-
-
Constructor Details
-
SimpleDataStructure
public SimpleDataStructure()A new instance with the data set to null. Call readBytes(), setData(), or fromByteArray() after this to set the data -
SimpleDataStructure
public SimpleDataStructure(byte[] data)- 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- Since:
- 0.8.2
-
getData
public byte[] getData()Get the data reference (not a copy)- Returns:
- the byte array, or null if unset
-
setData
public void setData(byte[] data)Sets the data.- 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
Sets the data.- 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- Returns:
- number of bytes read (should always equal target.length)
- Throws:
IOException- Since:
- 0.9.48, copied from former superclass DataStructureImpl
-
writeBytes
Description copied from interface:DataStructureWrite out the data structure to the stream, using the format defined in the I2P data structure specification.- 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
Description copied from interface:DataStructurerender the structure into modified base 64 notation- Specified by:
toBase64in interfaceDataStructure- Returns:
- null on error
-
fromBase64
Sets the data.- Specified by:
fromBase64in interfaceDataStructure- Throws:
DataFormatException- if decoded data is not the legal number of bytes or on decoding errorRuntimeException- if data already set.
-
calculateHash
Description copied from interface:DataStructureCalculate the SHA256 value of this object (useful for a few scenarios)- Specified by:
calculateHashin interfaceDataStructure- Returns:
- the SHA256 hash of the byte array, or null if the data is null
-
toByteArray
public byte[] toByteArray()- Specified by:
toByteArrayin interfaceDataStructure- Returns:
- same thing as getData()
-
fromByteArray
Does the same thing as setData() but null not allowed.- Specified by:
fromByteArrayin interfaceDataStructure- Parameters:
data- non-null- Throws:
DataFormatException- if null or wrong lengthRuntimeException- if data already set.
-
toString
-
hashCode
public int hashCode()We assume the data has enough randomness in it, so use the first 4 bytes for speed. If this is not the case, override in the extending class. -
equals
Warning - this returns true for two different classes with the same size and same data, e.g. SessionKey and SessionTag, but you wouldn't put them in the same Set, would you?
-