com.voytechs.jnetstream.io
Class ProtocolDataInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.voytechs.jnetstream.io.ProtocolDataInputStream
All Implemented Interfaces:
java.io.Closeable
Direct Known Subclasses:
PacketInputStream

public abstract class ProtocolDataInputStream
extends java.io.InputStream

This stream object extends the DataInputStream which defines how to read basic data types from a binary stream. This stream in addition to the Java native primitives that DataInput defines, add ability to read on a variable bit boundry. Any subset of bits can be read and returned in 'int' data type.


Field Summary
protected  int bitsLeft
          This is the bit offset wihin the bytes that we have read so far.
protected  java.io.DataInputStream in
          Our source DataInputStream since we require to provide similar interface as it is.
 
Constructor Summary
ProtocolDataInputStream(java.io.DataInputStream inputStream)
          Initialize with DataInputStream.
ProtocolDataInputStream(java.io.InputStream inputStream)
           
 
Method Summary
protected abstract  boolean isReady()
           
protected abstract  boolean isReady(int len)
           
static void main(java.lang.String[] args)
          Test function for ProtocolDataInputStream
 int read()
          Any normal read() operation invalidates our bit cache since we only allow reads to happen at the byte boudary.
 int readBits(int numOfBits)
          Read from the stream number of bits and return them as a 'int' data type while advancing the bit-based position in the stream.
 long readBitsLong(int numOfBits)
          Read from the stream number of bits and return them as a 'long' data type while advancing the bit-based position in the stream.
 boolean readBoolean()
          Read one input byte and returns true if that byte is nonzero, false if that bye is zero.
 byte readByte()
          Read one byte from the stream.
 java.math.BigInteger readerUnsignedLongLittleEndian()
           
 int readFromPacket()
          Reads a 1 byte from stream but returns it as an int.
 int readFromPacket(byte[] array)
          Reads n bytes from stream but returns it as an int.
 int readInt()
          Reads a 32bit int using Big Endian encoding from stream.
 int readIntLittleEndian()
          Reads a 32bit int using Little Endian encoding from stream.
 long readLong()
          Reads a 64bit unsigned int using Big Endian encoding from stream.
 long readLongLittleEndian()
          Reads a 64bit long int using Little Endian encoding from stream.
 int readShort()
          Reads a 32bit int using Big Endian encoding from stream.
 short readShortLittleEndian()
          Reads a 16bit short using Little Endian encoding from stream.
 int readUnsignedByte()
          Reads a 1 byte from stream but returns it as an int.
 long readUnsignedInt()
          Reads a 32bit unsigned int using Big Endian encoding from stream.
 long readUnsignedIntLittleEndian()
          Reads a 32bit int using Little Endian encoding from stream.
 java.math.BigInteger readUnsignedLong()
           
 int readUnsignedShort()
          Reads a 16bit unsigned int using Big Endian encoding from stream.
 int readUnsignedShortLittleEndian()
          Reads a 16bit unsigned short using Little Endian encoding from stream.
protected  void reThrow(java.io.IOException ioe)
          Conveniece method that checks if the stream has thrown EOFException.
 
Methods inherited from class java.io.InputStream
available, close, mark, markSupported, read, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

bitsLeft

protected int bitsLeft
This is the bit offset wihin the bytes that we have read so far. If bitsLeft == 0 then there is no real data in the chachedByte variable if bitsLeft > 1 and bitOffset < 7 then we have real data in the cached byte and should read from upto the remainder of the cache. It should always have values between 0 and 7;


in

protected java.io.DataInputStream in
Our source DataInputStream since we require to provide similar interface as it is.

Constructor Detail

ProtocolDataInputStream

public ProtocolDataInputStream(java.io.DataInputStream inputStream)
Initialize with DataInputStream. We will besides the baseclasses functions to read native data types from the stream we will define methods for accessing on variouable bit boundries.

Parameters:
inputStream - Stream to read underlying data from.

ProtocolDataInputStream

public ProtocolDataInputStream(java.io.InputStream inputStream)
Method Detail

read

public int read()
         throws java.io.IOException
Any normal read() operation invalidates our bit cache since we only allow reads to happen at the byte boudary.

Specified by:
read in class java.io.InputStream
Returns:
reads the next byte of the stream and returns it as an int. Invalidates any bit positioning within the stream and aligns the next bit read at the start of the next byte.
Throws:
java.io.IOException - any exceptions thrown by source input stream

readBits

public int readBits(int numOfBits)
             throws java.io.IOException
Read from the stream number of bits and return them as a 'int' data type while advancing the bit-based position in the stream.

Parameters:
numOfBits - Number of bits (0 to 32) to read from the stream starting at the current byte and bit position. The maximum number of bits that can be read is 32. 0 count of bits will always return value 0 and will not advance the position in the stream. Less then 0 will generate an IvalidArgumentException.
Throws:
InvalidArgumentExcpeption - is trown if numOfBits is outside the 0 to 32 range.
java.io.IOException - any IOException thrown by source input stream.

readBitsLong

public long readBitsLong(int numOfBits)
                  throws java.io.IOException
Read from the stream number of bits and return them as a 'long' data type while advancing the bit-based position in the stream.

Parameters:
numOfBits - Number of bits (0 to 64) to read from the stream starting at the current byte and bit position. The maximum number of bits that can be read is 64. 0 count of bits will always return value 0 and will not advance the position in the stream. Less then 0 will generate an IvalidArgumentException.
Throws:
InvalidArgumentExcpeption - is trown if numOfBits is outside the 0 to 64 range.
java.io.IOException - any IOException thrown by source input stream.

isReady

protected abstract boolean isReady(int len)
                            throws EOPacket
Throws:
EOPacket

isReady

protected abstract boolean isReady()
                            throws EOPacket
Throws:
EOPacket

readByte

public byte readByte()
              throws java.io.IOException,
                     EOPacket,
                     EOPacketStream
Read one byte from the stream.

Throws:
java.io.IOException
EOPacket
EOPacketStream

readBoolean

public boolean readBoolean()
                    throws java.io.IOException,
                           EOPacket,
                           EOPacketStream
Read one input byte and returns true if that byte is nonzero, false if that bye is zero.

Throws:
java.io.IOException
EOPacket
EOPacketStream

readInt

public int readInt()
            throws java.io.IOException,
                   EOPacket,
                   EOPacketStream
Reads a 32bit int using Big Endian encoding from stream.

Throws:
java.io.IOException
EOPacket
EOPacketStream

readShort

public int readShort()
              throws java.io.IOException,
                     EOPacket,
                     EOPacketStream
Reads a 32bit int using Big Endian encoding from stream.

Throws:
java.io.IOException
EOPacket
EOPacketStream

readUnsignedInt

public long readUnsignedInt()
                     throws java.io.IOException,
                            EOPacket,
                            EOPacketStream
Reads a 32bit unsigned int using Big Endian encoding from stream.

Throws:
java.io.IOException
EOPacket
EOPacketStream

readUnsignedShort

public int readUnsignedShort()
                      throws java.io.IOException,
                             EOPacket,
                             EOPacketStream
Reads a 16bit unsigned int using Big Endian encoding from stream.

Throws:
java.io.IOException
EOPacket
EOPacketStream

readLong

public long readLong()
              throws java.io.IOException,
                     EOPacket,
                     EOPacketStream
Reads a 64bit unsigned int using Big Endian encoding from stream.

Throws:
java.io.IOException
EOPacket
EOPacketStream

readShortLittleEndian

public short readShortLittleEndian()
                            throws java.io.IOException,
                                   EOPacket,
                                   EOPacketStream
Reads a 16bit short using Little Endian encoding from stream.

Throws:
java.io.IOException
EOPacket
EOPacketStream

readIntLittleEndian

public int readIntLittleEndian()
                        throws java.io.IOException,
                               EOPacket,
                               EOPacketStream
Reads a 32bit int using Little Endian encoding from stream.

Throws:
java.io.IOException
EOPacket
EOPacketStream

readUnsignedIntLittleEndian

public long readUnsignedIntLittleEndian()
                                 throws java.io.IOException,
                                        EOPacket,
                                        EOPacketStream
Reads a 32bit int using Little Endian encoding from stream.

Throws:
java.io.IOException
EOPacket
EOPacketStream

readLongLittleEndian

public long readLongLittleEndian()
                          throws java.io.IOException,
                                 EOPacket,
                                 EOPacketStream
Reads a 64bit long int using Little Endian encoding from stream.

Throws:
java.io.IOException
EOPacket
EOPacketStream

readUnsignedShortLittleEndian

public int readUnsignedShortLittleEndian()
                                  throws java.io.IOException,
                                         EOPacket,
                                         EOPacketStream
Reads a 16bit unsigned short using Little Endian encoding from stream.

Throws:
java.io.IOException
EOPacket
EOPacketStream

readUnsignedLong

public java.math.BigInteger readUnsignedLong()
                                      throws java.io.IOException,
                                             EOPacket,
                                             EOPacketStream
Throws:
java.io.IOException
EOPacket
EOPacketStream

readerUnsignedLongLittleEndian

public java.math.BigInteger readerUnsignedLongLittleEndian()
                                                    throws java.io.IOException,
                                                           EOPacket,
                                                           EOPacketStream
Throws:
java.io.IOException
EOPacket
EOPacketStream

readFromPacket

public int readFromPacket()
                   throws java.io.IOException,
                          EOPacket,
                          EOPacketStream
Reads a 1 byte from stream but returns it as an int. This is replaces the normal read() method but implements ProtocolStream specific exception reporting.

Throws:
java.io.IOException
EOPacket
EOPacketStream

readFromPacket

public int readFromPacket(byte[] array)
                   throws java.io.IOException,
                          EOPacket,
                          EOPacketStream
Reads n bytes from stream but returns it as an int. This is replaces the normal read() method but implements ProtocolStream specific exception reporting.

Throws:
java.io.IOException
EOPacket
EOPacketStream

readUnsignedByte

public int readUnsignedByte()
                     throws java.io.IOException,
                            EOPacket,
                            EOPacketStream
Reads a 1 byte from stream but returns it as an int. This is replaces the normal read() method but implements ProtocolStream specific exception reporting.

Throws:
java.io.IOException
EOPacket
EOPacketStream

reThrow

protected void reThrow(java.io.IOException ioe)
                throws java.io.IOException,
                       EOPacketStream
Conveniece method that checks if the stream has thrown EOFException. If yes then EOPacketStream exception is thrown instead otherwise the original exception is re-thrown.

This method always throws an exception. Either original or new EOPacketStream.

Parameters:
ioe - IOException to check if its EOFException.
Throws:
java.io.IOException - original exception re-thrown again.
EOPacketStream - a replacement exception is thrown which indicates that end of packet stream has been reached.

main

public static void main(java.lang.String[] args)
Test function for ProtocolDataInputStream

Parameters:
args - command line arguments