org.klomp.snark

## Class Message

- java.lang.Object
- - org.klomp.snark.Message

-     class Message
      extends Object

  Represents a BitTorrent protocol message for peer communication.

  This class encapsulates all BitTorrent protocol message types including:

  - Control messages: keep-alive, choke, unchoke, interested, uninterested
  - Piece messages: have, bitfield, request, piece, cancel
  - Extension messages for protocol extensions
  - Fast peer extension messages (BEP 6)
  - DHT messages (BEP 5, BEP 52)

  Messages are created and queued for sending through a DataOutputStream. The sendMessage() method translates messages to wire format.

  Message structure includes:

  - type - The message type byte
  - piece - The piece index (for have, request, piece, cancel)
  - begin - The offset within the piece
  - length - The data length
  - data - Optional payload data
  See Also:
    `PeerState`

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `(package private) static byte` | `ALLOWED_FAST`
    `(package private) int` | `begin`
    `(package private) static byte` | `BITFIELD`
    `(package private) static byte` | `CANCEL`
    `(package private) static byte` | `CHOKE`
    `(package private) byte[]` | `data`
    `(package private) static byte` | `EXTENSION`
    `(package private) static byte` | `HASH_REJECT`
    `(package private) static byte` | `HASH_REQUEST`
    `(package private) static byte` | `HASHES`
    `(package private) static byte` | `HAVE`
    `(package private) static byte` | `HAVE_ALL`
    `(package private) static byte` | `HAVE_NONE`
    `(package private) static byte` | `INTERESTED`
    `(package private) static byte` | `KEEP_ALIVE`
    `(package private) int` | `len`
    `(package private) int` | `length`
    `(package private) int` | `off`
    `(package private) int` | `piece`
    `(package private) static byte` | `PIECE`
    `(package private) static byte` | `PORT`
    `(package private) static byte` | `REJECT`
    `(package private) static byte` | `REQUEST`
    `(package private) static byte` | `SUGGEST`
    `(package private) byte` | `type`
    `(package private) static byte` | `UNCHOKE`
    `(package private) static byte` | `UNINTERESTED`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `Message(byte type)` For types KEEP_ALIVE, CHOKE, UNCHOKE, INTERESTED, UNINTERESTED, HAVE_ALL, HAVE_NONE
    `Message(byte[] data)` For type BITFIELD
    `Message(byte type, int piece)` For types HAVE, PORT, SUGGEST, ALLOWED_FAST
    `Message(byte type, int piece, int begin, int length)` For types REQUEST, REJECT, CANCEL
    `Message(int id, byte[] data)` For type EXTENSION
    `Message(int piece, int begin, int length, DataLoader loader)` For type PIECE with deferred data

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `(package private) void` | `sendMessage(DataOutputStream dos)` Utility method for sending a message through a DataStream.
    `String` | `toString()`

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

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

- - ### Field Detail

    - #### ALLOWED_FAST

          static final byte ALLOWED_FAST

      See Also:
        Constant Field Values

    - #### begin

          final int begin

    - #### BITFIELD

          static final byte BITFIELD

      See Also:
        Constant Field Values

    - #### CANCEL

          static final byte CANCEL

      See Also:
        Constant Field Values

    - #### CHOKE

          static final byte CHOKE

      See Also:
        Constant Field Values

    - #### data

          byte[] data

    - #### EXTENSION

          static final byte EXTENSION

      See Also:
        Constant Field Values

    - #### HASH_REJECT

          static final byte HASH_REJECT

      See Also:
        Constant Field Values

    - #### HASH_REQUEST

          static final byte HASH_REQUEST

      See Also:
        Constant Field Values

    - #### HASHES

          static final byte HASHES

      See Also:
        Constant Field Values

    - #### HAVE

          static final byte HAVE

      See Also:
        Constant Field Values

    - #### HAVE_ALL

          static final byte HAVE_ALL

      See Also:
        Constant Field Values

    - #### HAVE_NONE

          static final byte HAVE_NONE

      See Also:
        Constant Field Values

    - #### INTERESTED

          static final byte INTERESTED

      See Also:
        Constant Field Values

    - #### KEEP_ALIVE

          static final byte KEEP_ALIVE

      See Also:
        Constant Field Values

    - #### len

          final int len

    - #### length

          final int length

    - #### off

          final int off

    - #### piece

          final int piece

    - #### PIECE

          static final byte PIECE

      See Also:
        Constant Field Values

    - #### PORT

          static final byte PORT

      See Also:
        Constant Field Values

    - #### REJECT

          static final byte REJECT

      See Also:
        Constant Field Values

    - #### REQUEST

          static final byte REQUEST

      See Also:
        Constant Field Values

    - #### SUGGEST

          static final byte SUGGEST

      See Also:
        Constant Field Values

    - #### type

          final byte type

    - #### UNCHOKE

          static final byte UNCHOKE

      See Also:
        Constant Field Values

    - #### UNINTERESTED

          static final byte UNINTERESTED

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### Message

          Message(byte type)

      For types KEEP_ALIVE, CHOKE, UNCHOKE, INTERESTED, UNINTERESTED, HAVE_ALL, HAVE_NONE

    - #### Message

          Message(byte[] data)

      For type BITFIELD

    - #### Message

          Message(byte type,
                  int piece)

      For types HAVE, PORT, SUGGEST, ALLOWED_FAST

    - #### Message

          Message(byte type,
                  int piece,
                  int begin,
                  int length)

      For types REQUEST, REJECT, CANCEL

    - #### Message

          Message(int id,
                  byte[] data)

      For type EXTENSION

    - #### Message

          Message(int piece,
                  int begin,
                  int length,
                  DataLoader loader)

      For type PIECE with deferred data

  - ### Method Detail

    - #### sendMessage

          void sendMessage(DataOutputStream dos)
                    throws IOException

      Utility method for sending a message through a DataStream.
      Throws:
        `IOException`

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`