net.i2p.data.i2np
Class FastI2NPMessageImpl

java.lang.Object
  extended by net.i2p.data.DataStructureImpl
      extended by net.i2p.data.i2np.I2NPMessageImpl
          extended by net.i2p.data.i2np.FastI2NPMessageImpl
All Implemented Interfaces:
Serializable, DataStructure, I2NPMessage
Direct Known Subclasses:
DatabaseLookupMessage, DatabaseSearchReplyMessage, DatabaseStoreMessage, DataMessage, DeliveryStatusMessage, GarlicMessage, TunnelDataMessage, TunnelGatewayMessage, UnknownI2NPMessage

public abstract class FastI2NPMessageImpl
extends I2NPMessageImpl

Ignore, but save, the SHA-256 checksum in the full 16-byte header when read in. Use the same checksum when writing out. This is a savings for NTCP in, and for NTCP-in to NTCP-out for TunnelDataMessages. It's also a savings for messages embedded in other messages. Note that SSU does not use the SHA-256 checksum. Subclasses must take care to set _hasChecksum to false to invalidate it if the message payload changes between reading and writing. It isn't clear where, if anywhere, we actually need to send a checksum. For point-to-point messages over NTCP where we know the router version of the peer, we could add a method to skip checksum generation. For end-to-end I2NP messages embedded in a Garlic, TGM, etc... we would need a flag day.

Since:
0.8.12
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class net.i2p.data.i2np.I2NPMessageImpl
I2NPMessageImpl.Builder
 
Field Summary
protected  byte _checksum
           
protected  boolean _hasChecksum
           
 
Fields inherited from class net.i2p.data.i2np.I2NPMessageImpl
_context, _expiration, _log, _uniqueId, CHECKSUM_LENGTH, DEFAULT_EXPIRATION_MS, HEADER_LENGTH
 
Fields inherited from interface net.i2p.data.i2np.I2NPMessage
MAX_ID_VALUE, MAX_SIZE
 
Constructor Summary
FastI2NPMessageImpl(I2PAppContext context)
           
 
Method Summary
 int readBytes(byte[] data, int type, int offset, int maxLen)
          Ignore, but save, the checksum, to be used later if necessary.
 void readBytes(InputStream in)
          Deprecated. unused
 int readBytes(InputStream in, int type, byte[] buffer)
          Deprecated. unused
 int toByteArray(byte[] buffer)
          If available, use the previously-computed or previously-read checksum for speed
protected  int toByteArrayWithSavedChecksum(byte[] buffer)
          Use a previously-computed checksum for speed
 void writeBytes(OutputStream out)
          Deprecated. unused
 
Methods inherited from class net.i2p.data.i2np.I2NPMessageImpl
calculateWrittenLength, createMessage, fromRawByteArray, getMessageExpiration, getMessageSize, getRawMessageSize, getUniqueId, readBytes, readMessage, registerBuilder, setMessageExpiration, setUniqueId, toByteArray, toRawByteArray, writeMessageBody
 
Methods inherited from class net.i2p.data.DataStructureImpl
calculateHash, fromBase64, fromByteArray, read, toBase64
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface net.i2p.data.i2np.I2NPMessage
getType, readMessage
 
Methods inherited from interface net.i2p.data.DataStructure
calculateHash, fromBase64, fromByteArray, toBase64
 

Field Detail

_checksum

protected byte _checksum

_hasChecksum

protected boolean _hasChecksum
Constructor Detail

FastI2NPMessageImpl

public FastI2NPMessageImpl(I2PAppContext context)
Method Detail

readBytes

public void readBytes(InputStream in)
               throws DataFormatException,
                      IOException
Deprecated. unused

Description copied from class: I2NPMessageImpl
Read the whole message but only if it's exactly 1024 bytes. Unused - All transports provide encapsulation and so we have byte arrays available.

Specified by:
readBytes in interface DataStructure
Overrides:
readBytes in class I2NPMessageImpl
Parameters:
in - stream to read from
Throws:
UnsupportedOperationException
DataFormatException - if the data is improperly formatted
IOException - if there was a problem reading the stream

readBytes

public int readBytes(InputStream in,
                     int type,
                     byte[] buffer)
              throws I2NPMessageException,
                     IOException
Deprecated. unused

Description copied from class: I2NPMessageImpl
Read the header, then read the rest into buffer, then call readMessage in the implemented message type This does a copy from the stream to the buffer, so if you already have a byte array, use the other readBytes() instead.
  Specifically:
    1 byte type (if caller didn't read already, as specified by the type param
    4 byte ID
    8 byte expiration
    2 byte size
    1 byte checksum
    size bytes of payload (read by readMessage() in implementation)
Unused - All transports provide encapsulation and so we have byte arrays available.

Specified by:
readBytes in interface I2NPMessage
Overrides:
readBytes in class I2NPMessageImpl
Parameters:
in - stream to read from starting at type if type is < 0 (16 byte header) starting at ID if type is >= 0 (15 byte header)
type - the message type or -1 if we should read it here
buffer - temp buffer to use
Returns:
total length of the message
Throws:
UnsupportedOperationException
I2NPMessageException - if the stream doesn't contain a valid message that this class can read.
IOException - if there is a problem reading from the stream

readBytes

public int readBytes(byte[] data,
                     int type,
                     int offset,
                     int maxLen)
              throws I2NPMessageException
Ignore, but save, the checksum, to be used later if necessary.

Specified by:
readBytes in interface I2NPMessage
Overrides:
readBytes in class I2NPMessageImpl
Parameters:
maxLen - read no more than this many bytes from data starting at offset, even if it is longer This includes the type byte only if type < 0
data - the data, may or may not include the type
type - I2NP message type. If less than zero, read the type from data
offset - where to start starting at type if type is < 0 (16 byte header) starting at ID if type is >= 0 (15 byte header)
Returns:
size of the message read (including headers)
Throws:
IllegalStateException - if called twice, to protect saved checksum
I2NPMessageException - if there is no valid message

writeBytes

public void writeBytes(OutputStream out)
                throws DataFormatException,
                       IOException
Deprecated. unused

Description copied from class: I2NPMessageImpl
Don't do this if you need a byte array - use toByteArray()

Specified by:
writeBytes in interface DataStructure
Overrides:
writeBytes in class I2NPMessageImpl
Parameters:
out - stream to write to
Throws:
UnsupportedOperationException
DataFormatException - if the data was incomplete or not yet ready to be written
IOException - if there was a problem writing to the stream

toByteArray

public int toByteArray(byte[] buffer)
If available, use the previously-computed or previously-read checksum for speed

Specified by:
toByteArray in interface I2NPMessage
Overrides:
toByteArray in class I2NPMessageImpl

toByteArrayWithSavedChecksum

protected int toByteArrayWithSavedChecksum(byte[] buffer)
Use a previously-computed checksum for speed