|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.i2p.data.DataStructureImpl
net.i2p.data.SimpleDataStructure
public abstract class SimpleDataStructure
A SimpleDataStructure contains only a single fixed-length byte array. The main reason to do this is to override toByteArray() and fromByteArray(), which are used by toBase64(), fromBase64(), and calculateHash() in DataStructureImpl - otherwise these would go through a wasteful array-to-stream-to-array pass. It also centralizes a lot of common code. 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.
Field Summary | |
---|---|
protected byte[] |
_data
|
Constructor Summary | |
---|---|
SimpleDataStructure()
A new instance with the data set to null. |
|
SimpleDataStructure(byte[] data)
|
Method Summary | |
---|---|
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)
Overridden for efficiency. |
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 |
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()
Overridden for efficiency. |
String |
toString()
|
void |
writeBytes(OutputStream out)
Write out the data structure to the stream, using the format defined in the I2P data structure specification. |
Methods inherited from class net.i2p.data.DataStructureImpl |
---|
read |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected byte[] _data
Constructor Detail |
---|
public SimpleDataStructure()
public SimpleDataStructure(byte[] data)
IllegalArgumentException
- if data is not the legal number of bytes (but null is ok)Method Detail |
---|
public abstract int length()
public byte[] getData()
public void setData(byte[] data)
data
- of correct length, or null
IllegalArgumentException
- 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
in
- the stream to read
RuntimeException
- if data already set.
DataFormatException
- if the data is improperly formatted
IOException
- if there was a problem reading the streampublic void writeBytes(OutputStream out) throws DataFormatException, IOException
DataStructure
out
- stream to write to
DataFormatException
- if the data was incomplete or not yet ready to be written
IOException
- if there was a problem writing to the streampublic String toBase64()
DataStructure
toBase64
in interface DataStructure
toBase64
in class DataStructureImpl
public void fromBase64(String data) throws DataFormatException
fromBase64
in interface DataStructure
fromBase64
in class DataStructureImpl
DataFormatException
- if decoded data is not the legal number of bytes or on decoding error
RuntimeException
- if data already set.public Hash calculateHash()
DataStructure
calculateHash
in interface DataStructure
calculateHash
in class DataStructureImpl
public byte[] toByteArray()
toByteArray
in interface DataStructure
toByteArray
in class DataStructureImpl
public void fromByteArray(byte[] data) throws DataFormatException
fromByteArray
in interface DataStructure
fromByteArray
in class DataStructureImpl
data
- non-null
DataFormatException
- if null or wrong length
RuntimeException
- if data already set.public String toString()
toString
in class Object
public int hashCode()
hashCode
in class Object
public boolean equals(Object obj)
equals
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |