naga.packetreader
Class RegularPacketReader

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

public class RegularPacketReader
extends java.lang.Object
implements PacketReader

Reads packet of the format

[header 1-4 bytes] => content size
[content] => 0-255/0-65535/0-16777215/0-2147483646

Note that the maximum size for 4 bytes is a signed 32 bit int, not unsigned.

Author:
Christoffer Lerno

Constructor Summary
RegularPacketReader(int headerSize, boolean bigEndian)
          Creates a regular packet reader with the given header size.
 
Method Summary
 java.nio.ByteBuffer getBuffer()
          Return the next buffer to use.
 byte[] getNextPacket()
          Return the next packet or null if no complete packet can be constructed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RegularPacketReader

public RegularPacketReader(int headerSize,
                           boolean bigEndian)
Creates a regular packet reader with the given header size.

Parameters:
headerSize - the header size, 1 - 4 bytes.
bigEndian - big endian (largest byte first) or little endian (smallest byte first)
Method Detail

getBuffer

public java.nio.ByteBuffer getBuffer()
                              throws ProtocolViolationException
Return the next buffer to use.

Specified by:
getBuffer in interface PacketReader
Returns:
the next buffer to use.
Throws:
ProtocolViolationException - if the header was read and the size of the content is larger or equal to Integer.MAX_VALUE.

getNextPacket

public byte[] getNextPacket()
                     throws ProtocolViolationException
Return the next packet or null if no complete packet can be constructed.

Specified by:
getNextPacket in interface PacketReader
Returns:
the next packet available or null if none is available.
Throws:
ProtocolViolationException - if the size of the packet is larger or equal to Integer.MAX_VALUE.