net.i2p.data.i2cp
Class I2CPMessageImpl

java.lang.Object
  extended by net.i2p.data.DataStructureImpl
      extended by net.i2p.data.i2cp.I2CPMessageImpl
All Implemented Interfaces:
DataStructure, I2CPMessage
Direct Known Subclasses:
BandwidthLimitsMessage, CreateLeaseSetMessage, CreateSessionMessage, DestLookupMessage, DestReplyMessage, DestroySessionMessage, DisconnectMessage, GetBandwidthLimitsMessage, GetDateMessage, MessagePayloadMessage, MessageStatusMessage, ReceiveMessageBeginMessage, ReceiveMessageEndMessage, ReconfigureSessionMessage, ReportAbuseMessage, RequestLeaseSetMessage, SendMessageMessage, SessionStatusMessage, SetDateMessage

public abstract class I2CPMessageImpl
extends DataStructureImpl
implements I2CPMessage

Defines the base message implementation.

Author:
jrandom

Constructor Summary
I2CPMessageImpl()
           
 
Method Summary
protected abstract  void doReadMessage(java.io.InputStream buf, int size)
          Read in the payload part of the message (after the initial 4 byte size and 1 byte type)
protected abstract  byte[] doWriteMessage()
          Write out the payload part of the message (not including the 4 byte size and 1 byte type)
 void readBytes(java.io.InputStream in)
          Load up the current object with data from the given stream.
 void readMessage(java.io.InputStream in)
          Validate the type and size of the message, and then read the message into the data structures.
 void readMessage(java.io.InputStream in, int length, int type)
          Read the body into the data structures
 void writeBytes(java.io.OutputStream out)
          Write out the data structure to the stream, using the format defined in the I2P data structure specification.
 void writeMessage(java.io.OutputStream out)
          Write out the full message to the stream, including the 4 byte size and 1 byte type header.
 
Methods inherited from class net.i2p.data.DataStructureImpl
calculateHash, fromBase64, fromByteArray, read, toBase64, toByteArray
 
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.i2cp.I2CPMessage
getType
 
Methods inherited from interface net.i2p.data.DataStructure
calculateHash, fromBase64, fromByteArray, toBase64, toByteArray
 

Constructor Detail

I2CPMessageImpl

public I2CPMessageImpl()
Method Detail

readMessage

public void readMessage(java.io.InputStream in)
                 throws I2CPMessageException,
                        java.io.IOException
Validate the type and size of the message, and then read the message into the data structures.

Specified by:
readMessage in interface I2CPMessage
Parameters:
in - stream to read from
Throws:
java.io.IOException
I2CPMessageException - if the stream doesn't contain a valid message that this class can read.

readMessage

public void readMessage(java.io.InputStream in,
                        int length,
                        int type)
                 throws I2CPMessageException,
                        java.io.IOException
Read the body into the data structures

Specified by:
readMessage in interface I2CPMessage
Parameters:
length - number of bytes in the message payload
in - stream to read from
type - type of message (should equal getType())
Throws:
java.io.IOException
I2CPMessageException - if the stream doesn't contain a valid message that this class can read.

doReadMessage

protected abstract void doReadMessage(java.io.InputStream buf,
                                      int size)
                               throws I2CPMessageException,
                                      java.io.IOException
Read in the payload part of the message (after the initial 4 byte size and 1 byte type)

Parameters:
buf - InputStream
size - payload size
Throws:
I2CPMessageException
java.io.IOException

doWriteMessage

protected abstract byte[] doWriteMessage()
                                  throws I2CPMessageException,
                                         java.io.IOException
Write out the payload part of the message (not including the 4 byte size and 1 byte type)

Returns:
byte array
Throws:
I2CPMessageException
java.io.IOException

writeMessage

public void writeMessage(java.io.OutputStream out)
                  throws I2CPMessageException,
                         java.io.IOException
Write out the full message to the stream, including the 4 byte size and 1 byte type header.

Specified by:
writeMessage in interface I2CPMessage
Parameters:
out - OutputStream
Throws:
java.io.IOException
I2CPMessageException - if the current object doesn't have sufficient data to write a properly formatted message.

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

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