Package net.i2p.data.i2np
Class I2NPMessageImpl
- java.lang.Object
- 
- net.i2p.data.i2np.I2NPMessageImpl
 
- 
- All Implemented Interfaces:
- I2NPMessage
 - Direct Known Subclasses:
- FastI2NPMessageImpl,- TunnelBuildMessageBase
 
 public abstract class I2NPMessageImpl extends Object implements I2NPMessage Defines the base message implementation. Note: No longer extends DataStructureImpl as of 0.9.48- Author:
- jrandom
 
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static interfaceI2NPMessageImpl.Builderinterface for extending the types of messages handled - unused
 - 
Field SummaryFields Modifier and Type Field Description protected I2PAppContext_contextprotected long_expirationprotected Log_logstatic intCHECKSUM_LENGTHstatic longDEFAULT_EXPIRATION_MSstatic intHEADER_LENGTH16- 
Fields inherited from interface net.i2p.data.i2np.I2NPMessageMAX_ID_VALUE, MAX_SIZE
 
- 
 - 
Constructor SummaryConstructors Constructor Description I2NPMessageImpl(I2PAppContext context)
 - 
Method SummaryAll Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected abstract intcalculateWrittenLength()calculate the message body's length (not including the header and footerstatic I2NPMessagecreateMessage(I2PAppContext context, int type)Yes, this is fairly ugly, but its the only place it ever happens.static I2NPMessagefromRawByteArray(I2PAppContext ctx, byte[] buffer, int offset, int len, I2NPMessageHandler handler)Read the message with a short 5-byte header.static I2NPMessagefromRawByteArrayNTCP2(I2PAppContext ctx, byte[] buffer, int offset, int len, I2NPMessageHandler handler)Read the message with a short 9-byte header.longgetMessageExpiration()Date after which the message should be dropped (and the associated uniqueId forgotten)intgetMessageSize()How large the message is, including any checksums, i.e.intgetRawMessageSize()The raw header consists of a one-byte type and a 4-byte expiration in seconds only.longgetUniqueId()Replay resistant message IdintreadBytes(byte[] data, int type, int offset)Read the header, then read the rest into buffer, then call readMessage in the implemented message typeintreadBytes(byte[] data, int type, int offset, int maxLen)Set a limit on the max to read from the data buffer, so that we can use a large buffer but prevent the reader from reading off the end.voidreadBytes(InputStream in)Deprecated.unusedvoidreadMessage(byte[] data, int offset, int dataSize, int type, I2NPMessageHandler handler)static voidregisterBuilder(I2NPMessageImpl.Builder builder, int type)Deprecated.unusedvoidsetMessageExpiration(long exp)The expiration is set to one minute from now in the constructor but it can be overridden here.voidsetUniqueId(long id)The ID is set to a random value when written but it can be overridden here.byte[]toByteArray()inttoByteArray(byte[] buffer)write the message to the buffer, returning the number of bytes written.inttoByteArray(byte[] buffer, int off)Write the message to the buffer, returning the new offset (NOT the length).inttoRawByteArray(byte[] buffer)Write the message with a short 5-byte header.inttoRawByteArrayNTCP2(byte[] buffer, int off)Write the message to the buffer, returning the new offset (NOT the length).voidwriteBytes(OutputStream out)Deprecated.unusedprotected abstract intwriteMessageBody(byte[] out, int curIndex)write the message body to the output array, starting at the given index.- 
Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 - 
Methods inherited from interface net.i2p.data.i2np.I2NPMessagegetType, readMessage
 
- 
 
- 
- 
- 
Field Detail- 
_logprotected final Log _log 
 - 
_contextprotected final I2PAppContext _context 
 - 
_expirationprotected long _expiration 
 - 
DEFAULT_EXPIRATION_MSpublic static final long DEFAULT_EXPIRATION_MS - See Also:
- Constant Field Values
 
 - 
CHECKSUM_LENGTHpublic static final int CHECKSUM_LENGTH - See Also:
- Constant Field Values
 
 - 
HEADER_LENGTHpublic static final int HEADER_LENGTH 16- See Also:
- Constant Field Values
 
 
- 
 - 
Constructor Detail- 
I2NPMessageImplpublic I2NPMessageImpl(I2PAppContext context) 
 
- 
 - 
Method Detail- 
registerBuilder@Deprecated public static final void registerBuilder(I2NPMessageImpl.Builder builder, int type) Deprecated.unused
 - 
readBytes@Deprecated public void readBytes(InputStream in) Deprecated.unusedRead the whole message. Unused - All transports provide encapsulation and so we have byte arrays available.- Throws:
- UnsupportedOperationException- always
 
 - 
readBytespublic int readBytes(byte[] data, int type, int offset) throws I2NPMessageExceptionRead the header, then read the rest into buffer, then call readMessage in the implemented message typeSpecifically: 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)- Specified by:
- readBytesin interface- I2NPMessage
- Parameters:
- type- the message type or -1 if we should read it here
- data- the 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:
- total length of the message
- Throws:
- I2NPMessageException- if there is no valid message
 
 - 
readBytespublic int readBytes(byte[] data, int type, int offset, int maxLen) throws I2NPMessageExceptionSet a limit on the max to read from the data buffer, so that we can use a large buffer but prevent the reader from reading off the end.- Specified by:
- readBytesin interface- I2NPMessage
- Parameters:
- type- the message type or -1 if we should read it here
- 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
- 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:
- total length of the message
- Throws:
- I2NPMessageException- if there is no valid message
- Since:
- 0.8.12
 
 - 
writeBytes@Deprecated public void writeBytes(OutputStream out) Deprecated.unusedDon't do this if you need a byte array - use toByteArray()- Throws:
- UnsupportedOperationException- always
 
 - 
getUniqueIdpublic long getUniqueId() Replay resistant message Id- Specified by:
- getUniqueIdin interface- I2NPMessage
 
 - 
setUniqueIdpublic void setUniqueId(long id) The ID is set to a random value when written but it can be overridden here.- Specified by:
- setUniqueIdin interface- I2NPMessage
 
 - 
getMessageExpirationpublic long getMessageExpiration() Date after which the message should be dropped (and the associated uniqueId forgotten)- Specified by:
- getMessageExpirationin interface- I2NPMessage
 
 - 
setMessageExpirationpublic void setMessageExpiration(long exp) The expiration is set to one minute from now in the constructor but it can be overridden here.- Specified by:
- setMessageExpirationin interface- I2NPMessage
 
 - 
getMessageSizepublic int getMessageSize() Description copied from interface:I2NPMessageHow large the message is, including any checksums, i.e. full 16 byte header- Specified by:
- getMessageSizein interface- I2NPMessage
 
 - 
getRawMessageSizepublic int getRawMessageSize() The raw header consists of a one-byte type and a 4-byte expiration in seconds only. Used by SSU only!- Specified by:
- getRawMessageSizein interface- I2NPMessage
 
 - 
toByteArraypublic byte[] toByteArray() - Specified by:
- toByteArrayin interface- I2NPMessage
 
 - 
toByteArraypublic int toByteArray(byte[] buffer) write the message to the buffer, returning the number of bytes written. the data is formatted so as to be self contained, with the type, size, expiration, unique id, as well as a checksum bundled along. Full 16 byte header for NTCP 1.- Specified by:
- toByteArrayin interface- I2NPMessage
- Returns:
- the length written
 
 - 
toByteArraypublic int toByteArray(byte[] buffer, int off)Write the message to the buffer, returning the new offset (NOT the length). the data is formatted so as to be self contained, with the type, size, expiration, unique id, as well as a checksum bundled along. Full 16 byte header for NTCP 1.- Specified by:
- toByteArrayin interface- I2NPMessage
- Parameters:
- off- the offset to start writing at
- Returns:
- the new offset (NOT the length)
- Since:
- 0.9.36 with off param
 
 - 
calculateWrittenLengthprotected abstract int calculateWrittenLength() calculate the message body's length (not including the header and footer
 - 
writeMessageBodyprotected abstract int writeMessageBody(byte[] out, int curIndex) throws I2NPMessageExceptionwrite the message body to the output array, starting at the given index.- Returns:
- the index into the array after the last byte written (NOT the length)
- Throws:
- I2NPMessageException
 
 - 
toRawByteArraypublic int toRawByteArray(byte[] buffer) Write the message with a short 5-byte header. THe header consists of a one-byte type and a 4-byte expiration in seconds only. Used by SSU only!- Specified by:
- toRawByteArrayin interface- I2NPMessage
- Returns:
- the new offset (NOT the length)
 
 - 
toRawByteArrayNTCP2public int toRawByteArrayNTCP2(byte[] buffer, int off)Write the message to the buffer, returning the new offset (NOT the length). the data is is not self contained - it does not include the size, unique id, or any checksum, but does include the type and expiration. Short 9 byte header for NTCP 2.- Specified by:
- toRawByteArrayNTCP2in interface- I2NPMessage
- Parameters:
- off- the offset to start writing at
- Returns:
- the new offset (NOT the length)
- Since:
- 0.9.36
 
 - 
readMessagepublic void readMessage(byte[] data, int offset, int dataSize, int type, I2NPMessageHandler handler) throws I2NPMessageException- Specified by:
- readMessagein interface- I2NPMessage
- Throws:
- I2NPMessageException
 
 - 
fromRawByteArraypublic static I2NPMessage fromRawByteArray(I2PAppContext ctx, byte[] buffer, int offset, int len, I2NPMessageHandler handler) throws I2NPMessageException Read the message with a short 5-byte header. THe header consists of a one-byte type and a 4-byte expiration in seconds only. Used by SSU only!- Throws:
- I2NPMessageException
 
 - 
fromRawByteArrayNTCP2public static I2NPMessage fromRawByteArrayNTCP2(I2PAppContext ctx, byte[] buffer, int offset, int len, I2NPMessageHandler handler) throws I2NPMessageException Read the message with a short 9-byte header. THe header consists of a one-byte type, 4-byte ID, and a 4-byte expiration in seconds only. Used by NTCP2 only!- Throws:
- I2NPMessageException
- Since:
- 0.9.35
 
 - 
createMessagepublic static I2NPMessage createMessage(I2PAppContext context, int type) throws I2NPMessageException Yes, this is fairly ugly, but its the only place it ever happens.- Returns:
- non-null, returns an UnknownI2NPMessage if unknown type
- Throws:
- I2NPMessageException
 
 
- 
 
-