## net.i2p.data.i2np
Class I2NPMessageImpl

    java.lang.Object
      net.i2p.data.DataStructureImpl
          net.i2p.data.i2np.I2NPMessageImpl

All Implemented Interfaces:
  DataStructure, I2NPMessage

Direct Known Subclasses:
  DatabaseLookupMessage, DatabaseSearchReplyMessage, DatabaseStoreMessage, DataMessage, DateMessage, DeliveryStatusMessage, GarlicMessage, SSUDemo.FooMessage, TunnelBuildMessage, TunnelBuildReplyMessage, TunnelCreateMessage, TunnelCreateStatusMessage, TunnelDataMessage, TunnelGatewayMessage

    public abstract class I2NPMessageImpl

extends DataStructureImpl
implements I2NPMessage

Defines the base message implementation.

Author:
  jrandom

Nested Class Summary
`static interface` `I2NPMessageImpl.Builder` interface for extending the types of messages handled

Field Summary
`protected I2PAppContext` `_context`
`static int` `CHECKSUM_LENGTH`
`static long` `DEFAULT_EXPIRATION_MS`

Fields inherited from interface net.i2p.data.i2np.I2NPMessage
`MAX_ID_VALUE, MAX_SIZE`

Constructor Summary
`I2NPMessageImpl(I2PAppContext context)`

Method Summary
`protected abstract int` `calculateWrittenLength()` calculate the message body's length (not including the header and footer
`static I2NPMessage` `createMessage(I2PAppContext context, int type)` Yes, this is fairly ugly, but its the only place it ever happens.
`static I2NPMessage` `fromRawByteArray(I2PAppContext ctx, byte[] buffer, int offset, int len)`
`static I2NPMessage` `fromRawByteArray(I2PAppContext ctx, byte[] buffer, int offset, int len, I2NPMessageHandler handler)`
`long` `getMessageExpiration()` Date after which the message should be dropped (and the associated uniqueId forgotten)
`int` `getMessageSize()` How large the message is, including any checksums
`int` `getRawMessageSize()` How large the raw message is
`long` `getUniqueId()` Replay resistent message Id
`protected void` `read()`
`int` `readBytes(byte[] data, int type, int offset)`
`void` `readBytes(java.io.InputStream in)` Load up the current object with data from the given stream.
`int` `readBytes(java.io.InputStream in, int type, byte[] buffer)` Read the body into the data structures, after the initial type byte, using the current class's format as defined by the I2NP specification
`void` `readMessage(byte[] data, int offset, int dataSize, int type, I2NPMessageHandler handler)`
`static void` `registerBuilder(I2NPMessageImpl.Builder builder, int type)`
`void` `setMessageExpiration(long exp)`
`void` `setUniqueId(long id)`
`byte[]` `toByteArray()`
`int` `toByteArray(byte[] buffer)` write the message to the buffer, returning the number of bytes written.
`int` `toRawByteArray(byte[] buffer)` write the message to the buffer, returning the number of bytes written.
`protected void` `verifyUnwritten()`
`void` `writeBytes(java.io.OutputStream out)` Write out the data structure to the stream, using the format defined in the I2P data structure specification.
`protected abstract int` `writeMessageBody(byte[] out, int curIndex)` write the message body to the output array, starting at the given index.
`protected void` `written()`

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

### _context

    protected I2PAppContext _context

### DEFAULT_EXPIRATION_MS

    public static final long DEFAULT_EXPIRATION_MS

See Also:
  Constant Field Values

### CHECKSUM_LENGTH

    public static final int CHECKSUM_LENGTH

See Also:
  Constant Field Values

Constructor Detail

### I2NPMessageImpl

    public I2NPMessageImpl(I2PAppContext context)

Method Detail

### registerBuilder

    public static final void registerBuilder(I2NPMessageImpl.Builder builder,
                                             int type)

### 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.

  Specified by:
    `readBytes` in interface `DataStructure`
  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

### readBytes

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

  Description copied from interface: `I2NPMessage`
  Read the body into the data structures, after the initial type byte, using the current class's format as defined by the I2NP specification

  Specified by:
    `readBytes` in interface `I2NPMessage`
  Parameters:
    `in` - stream to read from
    `type` - I2NP message type
    `buffer` - scratch buffer to be used when reading and parsing
  Returns:
    size of the message read (including headers)
  Throws:
    `I2NPMessageException` - if the stream doesn't contain a valid message that this class can read.
    `java.io.IOException` - if there is a problem reading from the stream

### readBytes

    public int readBytes(byte[] data,
                         int type,
                         int offset)
                  throws I2NPMessageException,
                         java.io.IOException

  Specified by:
    `readBytes` in interface `I2NPMessage`
  Throws:
    `I2NPMessageException`
    `java.io.IOException`

### 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.

  Specified by:
    `writeBytes` in interface `DataStructure`
  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

### getUniqueId

    public long getUniqueId()

  Replay resistent message Id

  Specified by:
    `getUniqueId` in interface `I2NPMessage`

### setUniqueId

    public void setUniqueId(long id)

  Specified by:
    `setUniqueId` in interface `I2NPMessage`

### getMessageExpiration

    public long getMessageExpiration()

  Date after which the message should be dropped (and the associated uniqueId forgotten)

  Specified by:
    `getMessageExpiration` in interface `I2NPMessage`

### setMessageExpiration

    public void setMessageExpiration(long exp)

  Specified by:
    `setMessageExpiration` in interface `I2NPMessage`

### getMessageSize

    public int getMessageSize()

  Description copied from interface: `I2NPMessage`
  How large the message is, including any checksums

  Specified by:
    `getMessageSize` in interface `I2NPMessage`

### getRawMessageSize

    public int getRawMessageSize()

  Description copied from interface: `I2NPMessage`
  How large the raw message is

  Specified by:
    `getRawMessageSize` in interface `I2NPMessage`

### toByteArray

    public byte[] toByteArray()

  Specified by:
    `toByteArray` in interface `DataStructure`
  Overrides:
    `toByteArray` in class `DataStructureImpl`

### toByteArray

    public int toByteArray(byte[] buffer)

  Description copied from interface: `I2NPMessage`
  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.

  Specified by:
    `toByteArray` in interface `I2NPMessage`

### calculateWrittenLength

    protected abstract int calculateWrittenLength()

  calculate the message body's length (not including the header and footer

### writeMessageBody

    protected abstract int writeMessageBody(byte[] out,
                                            int curIndex)
                                     throws I2NPMessageException

  write the message body to the output array, starting at the given index.

  Returns:
    the index into the array after the last byte written
  Throws:
    `I2NPMessageException`

### toRawByteArray

    public int toRawByteArray(byte[] buffer)

  Description copied from interface: `I2NPMessage`
  write the message to the buffer, returning the number of bytes written. 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.

  Specified by:
    `toRawByteArray` in interface `I2NPMessage`

### readMessage

    public void readMessage(byte[] data,
                            int offset,
                            int dataSize,
                            int type,
                            I2NPMessageHandler handler)
                     throws I2NPMessageException,
                            java.io.IOException

  Specified by:
    `readMessage` in interface `I2NPMessage`
  Throws:
    `I2NPMessageException`
    `java.io.IOException`

### fromRawByteArray

    public static I2NPMessage fromRawByteArray(I2PAppContext ctx,
                                               byte[] buffer,
                                               int offset,
                                               int len)
                                        throws I2NPMessageException

  Throws:
    `I2NPMessageException`

### fromRawByteArray

    public static I2NPMessage fromRawByteArray(I2PAppContext ctx,
                                               byte[] buffer,
                                               int offset,
                                               int len,
                                               I2NPMessageHandler handler)
                                        throws I2NPMessageException

  Throws:
    `I2NPMessageException`

### verifyUnwritten

    protected void verifyUnwritten()

### written

    protected void written()

### read

    protected void read()

### createMessage

    public static I2NPMessage createMessage(I2PAppContext context,
                                            int type)
                                     throws I2NPMessageException

  Yes, this is fairly ugly, but its the only place it ever happens.

  Throws:
    `I2NPMessageException`