com.kirkwarez.kirkpack.io
Class EndianInputStream

java.lang.Object
  extended byjava.io.InputStream
      extended byjava.io.FilterInputStream
          extended bycom.kirkwarez.kirkpack.io.EndianInputStream
All Implemented Interfaces:
DataInput

public class EndianInputStream
extends FilterInputStream
implements DataInput

This stream behaves about exactly as DataInputStream, it reads primitive and some non-primitive data types from the underlying stream. The reason it doesn't extend DataInputStream is because all methods in that class are declared final. This class does implement DataInput.

The big difference is that this class takes a byte order on construction, and guarantees that the primitives will be constructed from bytes read using that byte order.

The non-primitive methods from this class are delegated to a DataInputStream, however the readLine method shouldn't be used, because the DataInputStream method is deprecated, even though it's implementing an interface... As of yet there is no replacement for this method, but you shouldn't be using it anyways since lines are pretty rare in byte streams...

Since:
3.00
Version:
3.00
Author:
Berend "Kirk" Wouda

Field Summary
static int BIG_ENDIAN
          The Big Endian byte order.
protected  int byteorder
          The byte order used by this stream.
protected  DataInputStream cache
          The InputStream that FilterInputStream parent class delegates to, only uncast.
static int LITTLE_ENDIAN
          The Little Endian byte order.
 
Fields inherited from class java.io.FilterInputStream
in
 
Fields inherited from class java.io.InputStream
 
Constructor Summary
EndianInputStream(InputStream input, int byteorder)
          Constucts a new stream that delivers primitives read using the given byte order.
 
Method Summary
 int getByteorder()
          Returns the byte order.
 boolean readBoolean()
          See the general contract of DataInput.
 byte readByte()
          See the general contract of DataInput.
 char readChar()
          See the general contract of DataInput.
 double readDouble()
          See the general contract of DataInput.
 float readFloat()
          See the general contract of DataInput.
 void readFully(byte[] buffer)
          See the general contract of DataInput.
 void readFully(byte[] buffer, int offset, int length)
          See the general contract of DataInput.
 byte[] readFully(int length)
          Creates a byte array wih the given length, fills it in with bytes from the stream completely and returns it.
 int readInt()
          See the general contract of DataInput.
 String readLine()
          Deprecated. DataInputStream uses a deprecated way. However, using a BufferedReader internally isn't going to work, since it'll mess up the stream (it buffers), and will require two objects each method call, since you can't close it. Nobody should actually use this method, because it simply doesn't make sense. A line in a byte stream?* A null terminated string I can understand... but a line? I mean, strings are usually just stored as either null-terminted strings or with some length attribute. Just read that out and bother with the lines later.
 long readLong()
          See the general contract of DataInput.
 short readShort()
          See the general contract of DataInput.
 int readUnsignedByte()
          See the general contract of DataInput.
 long readUnsignedInt()
          Reads 4 bytes and turns them into an unsigned int.
 int readUnsignedShort()
          See the general contract of DataInput.
 String readUTF()
          See the general contract of DataInput.
protected  void setByteorder(int byteorder)
          Sets the byte order.
 int skipBytes(int amount)
          See the general contract of DataInput.
 
Methods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, read, read, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cache

protected DataInputStream cache
The InputStream that FilterInputStream parent class delegates to, only uncast.


byteorder

protected int byteorder
The byte order used by this stream.


BIG_ENDIAN

public static final int BIG_ENDIAN
The Big Endian byte order.

See Also:
Constant Field Values

LITTLE_ENDIAN

public static final int LITTLE_ENDIAN
The Little Endian byte order.

See Also:
Constant Field Values
Constructor Detail

EndianInputStream

public EndianInputStream(InputStream input,
                         int byteorder)
Constucts a new stream that delivers primitives read using the given byte order.

Parameters:
input - The stream to read from.
byteorder - The byte order. This is either BIG_ENDIAN or LITTLE_ENDIAN.
Method Detail

readFully

public byte[] readFully(int length)
                 throws IOException
Creates a byte array wih the given length, fills it in with bytes from the stream completely and returns it.

Parameters:
length - The size of the buffer where the data is to be read into.
Throws:
EOFException - When the end of the steam is encountered.
IOException - When I/O errors happen.

readFully

public void readFully(byte[] buffer)
               throws IOException
See the general contract of DataInput.

Specified by:
readFully in interface DataInput
Parameters:
buffer - The buffer where the data is read into.
Throws:
EOFException - When the end of the steam is encountered.
IOException - When I/O errors happen.
See Also:
DataInput.readFully(byte[])

readFully

public void readFully(byte[] buffer,
                      int offset,
                      int length)
               throws IOException
See the general contract of DataInput.

Specified by:
readFully in interface DataInput
Parameters:
buffer - The buffer where the data is read into.
offset - The starting offset of the data in the array.
length - The number of bytes read.
Throws:
EOFException - When the end of the steam is encountered.
IOException - When I/O errors happen.
See Also:
DataInput.readFully(byte[], int, int)

skipBytes

public int skipBytes(int amount)
              throws IOException
See the general contract of DataInput.

Specified by:
skipBytes in interface DataInput
Parameters:
amount - The number of bytes to be skipped.
Throws:
IOException - When I/O errors happen.
See Also:
DataInput.skipBytes(int)

readBoolean

public boolean readBoolean()
                    throws IOException
See the general contract of DataInput.

Specified by:
readBoolean in interface DataInput
Returns:
The next boolean in this stream.
Throws:
EOFException - When the end of the steam is encountered.
IOException - When I/O errors happen.
See Also:
DataInput.readBoolean()

readByte

public byte readByte()
              throws IOException
See the general contract of DataInput.

Specified by:
readByte in interface DataInput
Returns:
The next byte in this stream.
Throws:
EOFException - When the end of the steam is encountered.
IOException - When I/O errors happen.
See Also:
DataInput.readByte()

readUnsignedByte

public int readUnsignedByte()
                     throws IOException
See the general contract of DataInput.

Specified by:
readUnsignedByte in interface DataInput
Returns:
The next unsigned byte in this stream as an int.
Throws:
EOFException - When the end of the steam is encountered.
IOException - When I/O errors happen.
See Also:
DataInput.readUnsignedByte()

readShort

public short readShort()
                throws IOException
See the general contract of DataInput.

Specified by:
readShort in interface DataInput
Returns:
The next short read from this stream using the set byte order.
Throws:
EOFException - When the end of the steam is encountered.
IOException - When I/O errors happen.
See Also:
DataInput.readShort()

readUnsignedShort

public int readUnsignedShort()
                      throws IOException
See the general contract of DataInput.

Specified by:
readUnsignedShort in interface DataInput
Returns:
The next unsigned short as an int read from this stream using the set byte order.
Throws:
EOFException - When the end of the steam is encountered.
IOException - When I/O errors happen.
See Also:
DataInput.readUnsignedShort()

readChar

public char readChar()
              throws IOException
See the general contract of DataInput.

Specified by:
readChar in interface DataInput
Returns:
The next Unicode character read from this stream, by reading two bytes and combining them. Why anyone would want to write out Unicode in such a crude way I don't know. If you can, look for cooler options. At least it's not bothered by byte order :^P
Throws:
EOFException - When the end of the steam is encountered.
IOException - When I/O errors happen.
See Also:
DataInput.readChar()

readInt

public int readInt()
            throws IOException
See the general contract of DataInput.

Specified by:
readInt in interface DataInput
Returns:
The next int read from this stream using the set byte order.
Throws:
EOFException - When the end of the steam is encountered.
IOException - When I/O errors happen.
See Also:
DataInput.readInt()

readUnsignedInt

public long readUnsignedInt()
                     throws IOException
Reads 4 bytes and turns them into an unsigned int. Returns it as a long, naturally.

Returns:
The next unsigned int read from this stream using the set byte order.
Throws:
EOFException - When the end of the steam is encountered.
IOException - When I/O errors happen.

readLong

public long readLong()
              throws IOException
See the general contract of DataInput.

Specified by:
readLong in interface DataInput
Returns:
The next long read from this stream using the set byte order.
Throws:
EOFException - When the end of the steam is encountered.
IOException - When I/O errors happen.
See Also:
DataInput.readLong()

readFloat

public float readFloat()
                throws IOException
See the general contract of DataInput.

Specified by:
readFloat in interface DataInput
Returns:
The next float read from this stream using the set byte order.
Throws:
EOFException - When the end of the steam is encountered.
IOException - When I/O errors happen.
See Also:
DataInput.readFloat(), Float.intBitsToFloat(int)

readDouble

public double readDouble()
                  throws IOException
See the general contract of DataInput.

Specified by:
readDouble in interface DataInput
Returns:
The next double read from this stream using the set byte order.
Throws:
EOFException - When the end of the steam is encountered.
IOException - When I/O errors happen.
See Also:
DataInput.readDouble(), Double.longBitsToDouble(long)

readLine

public String readLine()
                throws IOException
Deprecated. DataInputStream uses a deprecated way. However, using a BufferedReader internally isn't going to work, since it'll mess up the stream (it buffers), and will require two objects each method call, since you can't close it. Nobody should actually use this method, because it simply doesn't make sense. A line in a byte stream?* A null terminated string I can understand... but a line? I mean, strings are usually just stored as either null-terminted strings or with some length attribute. Just read that out and bother with the lines later.

See the general contract of DataInput.

Specified by:
readLine in interface DataInput
Returns:
The next line of text in this stream.
Throws:
IOException - When I/O errors occur.
See Also:
BufferedReader.readLine(), DataInputStream.readLine(), DataInput.readLine()

readUTF

public String readUTF()
               throws IOException
See the general contract of DataInput.

Specified by:
readUTF in interface DataInput
Returns:
The Unicode string read from the underlying stream.
Throws:
EOFException - When the end of the stream is encountered.
IOException - When IO errors happen.
UTFDataFormatException - When the bytes do not represent a valid UTF-8 encoding of a string.
See Also:
DataInput.readUTF()

getByteorder

public int getByteorder()
Returns the byte order.

Returns:
The byte order used by this stream.

setByteorder

protected void setByteorder(int byteorder)
Sets the byte order.

Parameters:
byteorder - The byte order used by this stream.