net.i2p.data

## Class Payload

- java.lang.Object
- - net.i2p.data.DataStructureImpl
  - - net.i2p.data.Payload

- All Implemented Interfaces:
    Serializable, DataStructure

      public class Payload
      extends DataStructureImpl

  Message payload container for I2P communications with encryption support.

  Payload provides message content delivery in I2P:

  - Message Content: Contains actual data being delivered
  - Encryption Support: Standard encryption wrapping per I2P spec
  - I2CP Protocol: Primarily used in client-router communication
  - Legacy Feature: Previously supported end-to-end encryption

  Historical Context:

  - End-to-End Encryption: Previously encrypted messages between clients
  - Protocol Change: End-to-end encryption removed in I2P
  - Current Usage: Use get/setEncryptedData() methods instead
  - I2CP Focus: Now mainly used for I2CP message transport

  Current Structure:

  - Encrypted Data: Primary payload content for transmission
  - Unencrypted Data: Legacy support for decrypted content access
  - Size Limits: Maximum 64KB to prevent OOM attacks
  - Format Compliance: Follows I2P data structure specification

  Usage Patterns:

  - Modern Usage: Use getEncryptedData()/setEncryptedData() for payload
  - I2CP Messages: Transport messages between client and router
  - Legacy Support: getUnencryptedData() for backward compatibility
  - Size Management: Monitor payload size to prevent resource exhaustion

  Security Considerations:

  - Size Validation: Enforce 64KB maximum payload size
  - Memory Protection: Prevent OOM attacks through large payloads
  - Content Validation: Verify payload integrity and format
  - Encryption Context: Understand current encryption model in I2P

  Performance Aspects:

  - Efficient Storage: Minimal overhead for payload data
  - Fast Access: Direct byte array access methods
  - Memory Management: Careful allocation and cleanup
  - Size Checking: Quick validation of payload limits

  Migration Notes:

  - From End-to-End: Previously handled client-to-client encryption
  - To Transport Only: Now focuses on message transport
  - API Simplification: Use encrypted data methods for clarity
  - Backward Compatibility: Legacy methods still available
  See Also:
    Serialized Form

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `Payload()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `equals(Object object)`
    `byte[]` | `getEncryptedData()` the real data
    `int` | `getSize()`
    `byte[]` | `getUnencryptedData()` Retrieve the unencrypted body of the message.
    `int` | `hashCode()`
    `void` | `readBytes(InputStream in)` Load up the current object with data from the given stream.
    `void` | `setEncryptedData(byte[] data)` the real data
    `void` | `setUnencryptedData(byte[] data)` Populate the message body with data.
    `String` | `toString()`
    `int` | `writeBytes(byte[] target, int offset)` Writes the encrypted payload to the target array.
    `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

      `calculateHash, fromBase64, fromByteArray, read, toBase64, toByteArray`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### Payload

          public Payload()

  - ### Method Detail

    - #### equals

          public boolean equals(Object object)

      Overrides:
        `equals` in class `Object`

    - #### getEncryptedData

          public byte[] getEncryptedData()

      the real data

    - #### getSize

          public int getSize()

    - #### getUnencryptedData

          public byte[] getUnencryptedData()

      Retrieve the unencrypted body of the message. Deprecated. Unless you are doing encryption, use getEncryptedData() instead.
      Returns:
        body of the message, or null if the message has either not been decrypted yet or if the hash is not correct

    - #### hashCode

          public int hashCode()

      Overrides:
        `hashCode` in class `Object`

    - #### readBytes

          public void readBytes(InputStream in)
                         throws DataFormatException,
                                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. Warning - many classes will throw IllegalStateException if data is already set.
      Parameters:
        `in` - stream to read from
      Throws:
        `DataFormatException` - if the data is improperly formatted
        `IOException` - if there was a problem reading the stream

    - #### setEncryptedData

          public void setEncryptedData(byte[] data)

      the real data
      Throws:
        `IllegalArgumentException` - if bigger than 64KB

    - #### setUnencryptedData

          public void setUnencryptedData(byte[] data)

      Populate the message body with data. This does not automatically encrypt yet. Deprecated. Unless you are doing encryption, use setEncryptedData() instead.
      Throws:
        `IllegalArgumentException` - if bigger than 64KB

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### writeBytes

          public int writeBytes(byte[] target,
                                int offset)

      Writes the encrypted payload to the target array.
      Returns:
        the written length (NOT the new offset)

    - #### writeBytes

          public void writeBytes(OutputStream out)
                          throws DataFormatException,
                                 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
        `IOException` - if there was a problem writing to the stream