naga.packetreader
public class DelimiterPacketReader extends java.lang.Object implements PacketReader
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.
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_READ_BUFFER_SIZE |
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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.
|
public static final int DEFAULT_READ_BUFFER_SIZE
public DelimiterPacketReader(byte delimiter)
delimiter
- the byte delimiter to use.public DelimiterPacketReader(byte delimiter, int readBufferSize, int maxPacketSize)
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.java.lang.IllegalArgumentException
- if maxPacketSize < readBufferSize or if
readBufferSize < 1.public int getMaxPacketSize()
public void setMaxPacketSize(int maxPacketSize)
This method is thread-safe, but will not affect reads in progress.
maxPacketSize
- the new maximum packet size.public java.nio.ByteBuffer getBuffer() throws ProtocolViolationException
getBuffer
in interface PacketReader
ProtocolViolationException
- if the internal buffer already exceeds the maximum size.public byte[] getNextPacket() throws ProtocolViolationException
PacketReader
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.
getNextPacket
in interface PacketReader
ProtocolViolationException
- if a protocol violation was detected when
parsing the next packet.