eap.freeciv
Class PacketSource

java.lang.Object
  extended byeap.freeciv.PacketSource

public class PacketSource
extends Object

Allows you to read packets from an InputStream. All of the Field objects read their data from a PacketSource.


Constructor Summary
PacketSource(InputStream in)
          Creates a new packet source which will use a default packet broker.
PacketSource(InputStream in, PacketBroker broker)
          Creates a new packet source which will read from the given underlying stream and use the given packet broker to determine the structure of each packet.
 
Method Summary
 int readByte()
          Reads the next byte in the packet.
 void readBytes(byte[] data)
          Fill a byte array by reading from the stream.
 Packet readPacket()
          Reads the next packet from the data stream.
 int remainingBytes()
          Returns the number of unread bytes in the current packet, according to the size field in the packet header.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PacketSource

public PacketSource(InputStream in,
                    PacketBroker broker)
Creates a new packet source which will read from the given underlying stream and use the given packet broker to determine the structure of each packet.


PacketSource

public PacketSource(InputStream in)
Creates a new packet source which will use a default packet broker.

Method Detail

remainingBytes

public int remainingBytes()
Returns the number of unread bytes in the current packet, according to the size field in the packet header.


readPacket

public Packet readPacket()
                  throws IOException
Reads the next packet from the data stream. This method blocks until the next packet can be read.

Currently, this method checks each packet for validity by saving the packet bytes verbatim from the stream, and then writing the packet to a byte array and comparing the two. If they are different, it prints an error message to stderr and exits. You can turn this off by setting CHECK_PACKETS and recompiling.

Returns:
the next packet or null if the underlying stream was at EOF when this method was called.
Throws:
EOFException - if the underlying stream reached EOF in the middle of the packet.
IOException - if the data did not conform to the proper structure, such as if the actual packet size was different from the size in the packet header. I should really create a new exception for Packet structure problems, but for now it was easier to use IOException for everything.

readByte

public int readByte()
             throws IOException
Reads the next byte in the packet. All of the Field classes use this method or readBytes(byte[]) to read from the underlying stream. These methods keep track of the "bytes reamining" accounting. This method blocks until the next byte is available.

Returns:
the next byte in the stream as an unsigned integer.
Throws:
IOException - if you try to read past the end of the current packet
EOFException - if you try to read when you are at EOF.

readBytes

public void readBytes(byte[] data)
               throws IOException
Fill a byte array by reading from the stream. This method blocks until all the bytes are read.

Throws:
IOException - if you try to read past the end of the current packet
EOFException - if you reach EOF.