naga.packetreader
Class DelimiterPacketReader

java.lang.Object
  extended by naga.packetreader.DelimiterPacketReader
All Implemented Interfaces:
PacketReader
Direct Known Subclasses:
AsciiLinePacketReader, ZeroDelimitedPacketReader

public class DelimiterPacketReader
extends java.lang.Object
implements PacketReader

Class to read a byte stream delimited by a byte marking the end of a packet.

Since packets read with delimiters may potentially grow unbounded, you can also supply a maximum buffer size to prevent an attacker from causing an out of memory by continously sending data without the delimiter.

The delimiter will never appear in the packet itself.

Author:
Christoffer Lerno

Field Summary
static int DEFAULT_READ_BUFFER_SIZE
           
 
Constructor Summary
DelimiterPacketReader(byte delimiter)
          Create a new reader with the default min buffer size and unlimited max buffer size.
DelimiterPacketReader(byte delimiter, int readBufferSize, int maxPacketSize)
          Create a new reader with the given min and max buffer size delimited by the given byte.
 
Method Summary
 java.nio.ByteBuffer getBuffer()
          Return the currently used byte buffer.
 int getMaxPacketSize()
          Get the current maximum buffer size.
 byte[] getNextPacket()
          Return the next packet constructed from the data read in the buffers.
 void setMaxPacketSize(int maxPacketSize)
          Set the new maximum packet size.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_READ_BUFFER_SIZE

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

DelimiterPacketReader

public DelimiterPacketReader(byte delimiter)
Create a new reader with the default min buffer size and unlimited max buffer size.

Parameters:
delimiter - the byte delimiter to use.

DelimiterPacketReader

public DelimiterPacketReader(byte delimiter,
                             int readBufferSize,
                             int maxPacketSize)
Create a new reader with the given min and max buffer size delimited by the given byte.

Parameters:
delimiter - the byte value of the delimiter.
readBufferSize - the size of the read buffer (i.e. how many bytes are read in a single pass) - this only has effect on read efficiency and memory requirements.
maxPacketSize - the maximum number of bytes read before throwing a ProtocolException. -1 means the packet has no size limit.
Throws:
java.lang.IllegalArgumentException - if maxPacketSize < readBufferSize or if readBufferSize < 1.
Method Detail

getMaxPacketSize

public int getMaxPacketSize()
Get the current maximum buffer size.

Returns:
the current maximum size.

setMaxPacketSize

public void setMaxPacketSize(int maxPacketSize)
Set the new maximum packet size.

This method is thread-safe, but will not affect reads in progress.

Parameters:
maxPacketSize - the new maximum packet size.

getBuffer

public java.nio.ByteBuffer getBuffer()
                              throws ProtocolViolationException
Return the currently used byte buffer.

Specified by:
getBuffer in interface PacketReader
Returns:
the byte buffer to use.
Throws:
ProtocolViolationException - if the internal buffer already exceeds the maximum size.

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.