net.i2p.data
Class Hash

java.lang.Object
  extended by net.i2p.data.DataStructureImpl
      extended by net.i2p.data.Hash
All Implemented Interfaces:
DataStructure

public class Hash
extends DataStructureImpl

Defines the hash as defined by the I2P data structure spec. AA hash is the SHA-256 of some data, taking up 32 bytes.

Author:
jrandom

Field Summary
static Hash FAKE_HASH
           
static int HASH_LENGTH
           
 
Constructor Summary
Hash()
           
Hash(byte[] data)
           
 
Method Summary
 byte[] cachedXor(Hash key)
          Calculate the xor with the current object and the specified hash, caching values where possible.
 void clearXorCache()
           
 boolean equals(java.lang.Object obj)
           
 byte[] getData()
           
 int hashCode()
           
static void main(java.lang.String[] args)
           
 void prepareCache()
          Prepare this hash's cache for xor values - very few hashes will need it, so we don't want to waste the memory, and lazy initialization would incur online overhead to verify the initialization.
 void readBytes(java.io.InputStream in)
          Load up the current object with data from the given stream.
 void setData(byte[] data)
           
 java.lang.String toBase64()
          render the structure into modified base 64 notation
 java.lang.String toString()
           
 void writeBytes(java.io.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
calculateHash, fromBase64, fromByteArray, read, toByteArray
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

HASH_LENGTH

public static final int HASH_LENGTH
See Also:
Constant Field Values

FAKE_HASH

public static final Hash FAKE_HASH
Constructor Detail

Hash

public Hash()

Hash

public Hash(byte[] data)
Method Detail

getData

public byte[] getData()

setData

public void setData(byte[] data)

prepareCache

public void prepareCache()
Prepare this hash's cache for xor values - very few hashes will need it, so we don't want to waste the memory, and lazy initialization would incur online overhead to verify the initialization.


cachedXor

public byte[] cachedXor(Hash key)
                 throws java.lang.IllegalStateException
Calculate the xor with the current object and the specified hash, caching values where possible. Currently this keeps up to MAX_CACHED_XOR (1024) entries, and uses an essentially random ejection policy. Later perhaps go for an LRU or FIFO?

Throws:
java.lang.IllegalStateException - if you try to use the cache without first preparing this object's cache via .prepareCache()

clearXorCache

public void clearXorCache()

readBytes

public void readBytes(java.io.InputStream in)
               throws DataFormatException,
                      java.io.IOException
Description copied from interface: DataStructure
Load up the current object with data from the given stream. Data loaded this way must match the I2P data structure specification.

Parameters:
in - stream to read from
Throws:
DataFormatException - if the data is improperly formatted
java.io.IOException - if there was a problem reading the stream

writeBytes

public void writeBytes(java.io.OutputStream out)
                throws DataFormatException,
                       java.io.IOException
Description copied from interface: DataStructure
Write out the data structure to the stream, using the format defined in the I2P data structure specification.

Parameters:
out - stream to write to
Throws:
DataFormatException - if the data was incomplete or not yet ready to be written
java.io.IOException - if there was a problem writing to the stream

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

toBase64

public java.lang.String toBase64()
Description copied from interface: DataStructure
render the structure into modified base 64 notation

Specified by:
toBase64 in interface DataStructure
Overrides:
toBase64 in class DataStructureImpl
Returns:
null on error

main

public static void main(java.lang.String[] args)