public abstract class SimpleDataStructure extends Object implements DataStructure
Modifier and Type | Field and Description |
---|---|
protected byte[] |
_data |
Constructor and Description |
---|
SimpleDataStructure()
A new instance with the data set to null.
|
SimpleDataStructure(byte[] data) |
Modifier and Type | Method and Description |
---|---|
Hash |
calculateHash()
Calculate the SHA256 value of this object (useful for a few scenarios)
|
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()
render the structure into modified base 64 notation
|
byte[] |
toByteArray() |
String |
toString() |
void |
writeBytes(OutputStream out)
Write out the data structure to the stream, using the format defined in the
I2P data structure specification.
|
public SimpleDataStructure()
public SimpleDataStructure(byte[] data)
IllegalArgumentException
- if data is not the legal number of bytes (but null is ok)public abstract int length()
public byte[] getData()
public 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 void readBytes(InputStream in) throws DataFormatException, IOException
readBytes
in interface DataStructure
in
- the stream to readRuntimeException
- if data already set.DataFormatException
- if the data is improperly formattedIOException
- if there was a problem reading the streamprotected int read(InputStream in, byte[] target) throws IOException
IOException
public void writeBytes(OutputStream out) throws DataFormatException, IOException
DataStructure
writeBytes
in interface DataStructure
out
- stream to write toDataFormatException
- if the data was incomplete or not yet ready to be writtenIOException
- if there was a problem writing to the streampublic String toBase64()
DataStructure
toBase64
in interface DataStructure
public void fromBase64(String data) throws DataFormatException
fromBase64
in interface DataStructure
DataFormatException
- if decoded data is not the legal number of bytes or on decoding errorRuntimeException
- if data already set.public Hash calculateHash()
DataStructure
calculateHash
in interface DataStructure
public byte[] toByteArray()
toByteArray
in interface DataStructure
public void fromByteArray(byte[] data) throws DataFormatException
fromByteArray
in interface DataStructure
data
- non-nullDataFormatException
- if null or wrong lengthRuntimeException
- if data already set.public int hashCode()