naga.packetreader
Class RawPacketReader

java.lang.Object
  extended by naga.packetreader.RawPacketReader
All Implemented Interfaces:
PacketReader

public class RawPacketReader
extends java.lang.Object
implements PacketReader

This packet reader reads as many bytes as possible from the stream and then bundles those bytes into a packet.

Author:
Christoffer Lerno

Field Summary
static int DEFAULT_BUFFER_SIZE
           
 
Constructor Summary
RawPacketReader()
          Create a new reader instance with the default buffer size.
RawPacketReader(int bufferSize)
          Create a new reader instance.
 
Method Summary
 java.nio.ByteBuffer getBuffer()
          Return the currently used byte buffer.
 byte[] getNextPacket()
          Return the next packet constructed from the data read in the buffers.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
See Also:
Constant Field Values
Constructor Detail

RawPacketReader

public RawPacketReader(int bufferSize)
Create a new reader instance. With a given read buffer size (this is how many bytes the packet reader will max read in a single pass).

Parameters:
bufferSize - the buffer size to use.

RawPacketReader

public RawPacketReader()
Create a new reader instance with the default buffer size.

Method Detail

getBuffer

public java.nio.ByteBuffer getBuffer()
Description copied from interface: PacketReader
Return the currently used byte buffer. The NIOSocket will use this byte buffer and perform a SocketChannel.read(ByteBuffer) on it.

The reader is guaranteed not to have this method called more than once before a call to PacketReader#getNextPacket() is made.

Specified by:
getBuffer in interface PacketReader
Returns:
the byte buffer to use.

getNextPacket

public byte[] getNextPacket()
                     throws ProtocolViolationException
Description copied from interface: PacketReader
Return the next packet constructed from the data read in the buffers.

This call may or may not have been proceeded by a call to getBuffer().

The calling thread will call this method repeatedly until it returns null.

Specified by:
getNextPacket in interface PacketReader
Returns:
a byte array containing the data of a packet, or null if not packet can be created yet from the data read.
Throws:
ProtocolViolationException - if a protocol violation was detected when parsing the next packet.