naga.packetwriter
Class RawPacketWriter

java.lang.Object
  extended by naga.packetwriter.RawPacketWriter
All Implemented Interfaces:
PacketWriter

public class RawPacketWriter
extends java.lang.Object
implements PacketWriter

Writes a byte packet to the stream without doing any changes to it.

This is the commonly case when one wants to output text or similarly delimited data.

Author:
Christoffer Lerno

Constructor Summary
RawPacketWriter()
          Creates a new writer.
 
Method Summary
 java.nio.ByteBuffer getBuffer()
          The current byte buffer to write to the socket.
 boolean isEmpty()
          Determines if the packet writer has more data to write.
 void setPacket(byte[] bytes)
          Set the next packet to write.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RawPacketWriter

public RawPacketWriter()
Creates a new writer.

Method Detail

setPacket

public void setPacket(byte[] bytes)
Description copied from interface: PacketWriter
Set the next packet to write.

Specified by:
setPacket in interface PacketWriter
Parameters:
bytes - an array of bytes representing the next packet.

getBuffer

public java.nio.ByteBuffer getBuffer()
Description copied from interface: PacketWriter
The current byte buffer to write to the socket.

Note that the socket does no rewinding or similar of the buffer, the only way it interacts with the buffer is by calling SocketChannel.write(ByteBuffer), so the implementing class needs to make sure that the buffer is in the right state.

This code will not be called unless PacketWriter.isEmpty() returns false.

Specified by:
getBuffer in interface PacketWriter
Returns:
the byte buffer to send data from to the socket.

isEmpty

public boolean isEmpty()
Description copied from interface: PacketWriter
Determines if the packet writer has more data to write.

Classes will never invoke PacketWriter.setPacket(byte[]) unless isEmpty returns true.

Specified by:
isEmpty in interface PacketWriter
Returns:
true if everything buffered in the writer is writen, false otherwise.