naga
Interface PacketWriter

All Known Implementing Classes:
RawPacketWriter, RegularPacketWriter

public interface PacketWriter

Interface for classes implementing packet writing strategies.

The method setPacket(byte[]) initializes for writer for output of a new packet. Implementing classes can assume that setPacket is never called unless isEmpty() returns true. The getBuffer() method should return a ByteBuffer containing the next chunk of data to output on the socket.

The implementation is similar to:

while (!packetWriter.isEmpty()) channel.write(packetWriter.getBuffer());

In other words, it is ok to split a single packet into several byte buffers each are handed in turn for every call to getBuffer(). (See RegularPacketWriter source code for an example.)

Author:
Christoffer Lerno

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.
 

Method Detail

setPacket

void setPacket(byte[] bytes)
Set the next packet to write.

Parameters:
bytes - an array of bytes representing the next packet.

isEmpty

boolean isEmpty()
Determines if the packet writer has more data to write.

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

Returns:
true if everything buffered in the writer is writen, false otherwise.

getBuffer

java.nio.ByteBuffer getBuffer()
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 isEmpty() returns false.

Returns:
the byte buffer to send data from to the socket.