peerbase
Class PeerMessage

java.lang.Object
  extended by peerbase.PeerMessage

public class PeerMessage
extends java.lang.Object

Represents a message, composed of type and data fields, in the PeerBase system. Also provides functionality for converting messages to/from byte arrays in a portable manner. The type of every message is a 4-byte value (i.e. 4-character string).

Author:
Nadeem Abdul Hamid

Constructor Summary
PeerMessage(byte[] type, byte[] data)
          Constructs a new PeerMessage object.
PeerMessage(SocketInterface s)
          Constructs a new PeerMessage object by reading data from the given socket connection.
PeerMessage(java.lang.String type, byte[] data)
          Constructs a new PeerMessage object.
PeerMessage(java.lang.String type, java.lang.String data)
          Constructs a new PeerMessage object.
 
Method Summary
static int byteArrayToInt(byte[] byteArray)
           
 java.lang.String getMsgData()
          Returns the message data as a String.
 byte[] getMsgDataBytes()
          Returns the message data.
 java.lang.String getMsgType()
          Returns the message type as a String.
 byte[] getMsgTypeBytes()
          Returns the message type.
static byte[] intToByteArray(int integer)
          Returns a byte array containing the two's-complement representation of the integer.
The byte array will be in big-endian byte-order with a fixes length of 4 (the least significant byte is in the 4th element).

Example:
intToByteArray(258) will return { 0, 0, 1, 2 },
BigInteger.valueOf(258).toByteArray() returns { 1, 2 }.
 byte[] toBytes()
          Returns a packed representation of this message as an array of bytes.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PeerMessage

public PeerMessage(byte[] type,
                   byte[] data)
Constructs a new PeerMessage object.

Parameters:
type - the message type (4 bytes)
data - the message data

PeerMessage

public PeerMessage(java.lang.String type,
                   java.lang.String data)
Constructs a new PeerMessage object.

Parameters:
type - the message type (4 characters)
data - the message data

PeerMessage

public PeerMessage(java.lang.String type,
                   byte[] data)
Constructs a new PeerMessage object.

Parameters:
type - the message type (4 characters)
data - the message data

PeerMessage

public PeerMessage(SocketInterface s)
            throws java.io.IOException
Constructs a new PeerMessage object by reading data from the given socket connection.

Parameters:
s - a socket connection object
Throws:
java.io.IOException - if I/O error occurs
Method Detail

getMsgType

public java.lang.String getMsgType()
Returns the message type as a String.

Returns:
the message type (4-character String)

getMsgTypeBytes

public byte[] getMsgTypeBytes()
Returns the message type.

Returns:
the message type (4-byte array)

getMsgData

public java.lang.String getMsgData()
Returns the message data as a String.

Returns:
the message data

getMsgDataBytes

public byte[] getMsgDataBytes()
Returns the message data.

Returns:
the message data

toBytes

public byte[] toBytes()
Returns a packed representation of this message as an array of bytes.

Returns:
byte array of message data

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

intToByteArray

public static byte[] intToByteArray(int integer)
Returns a byte array containing the two's-complement representation of the integer.
The byte array will be in big-endian byte-order with a fixes length of 4 (the least significant byte is in the 4th element).

Example:
intToByteArray(258) will return { 0, 0, 1, 2 },
BigInteger.valueOf(258).toByteArray() returns { 1, 2 }.

Parameters:
integer - The integer to be converted.
Returns:
The byte array of length 4.

byteArrayToInt

public static int byteArrayToInt(byte[] byteArray)