maspack.util
Class BinaryInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by maspack.util.BinaryInputStream
All Implemented Interfaces:
java.io.Closeable

public class BinaryInputStream
extends java.io.FilterInputStream

A data input stream class that can be set to convert its input from little-endian to big-endian. Characters can also be specified to be either word or byte length. The code is a mash-up of Elliotte Rusty Harold's LittleEndianInputStream, Java source code, and Antonio Sanchez's BinaryStreamReader.

Author:
John E. Lloyd, Antonio Sanchez, Elliotte Harold, Dec 16, 2013

Field Summary
static int BIG_ENDIAN
           
static int BYTE_CHAR
           
static int LITTLE_ENDIAN
           
 
Constructor Summary
BinaryInputStream(java.io.InputStream in)
          Creates a new BinaryInputStream from an input stream.
BinaryInputStream(java.io.InputStream in, int flags)
          Creates a new BinaryInputStream from an input stream and prescribed set of flag values.
 
Method Summary
 int getByteCount()
          Returns the total number of bytes that have been read by this stream.
 int getFlags()
          Gets the flags for this stream.
 boolean isLittleEndian()
          Returns true if this stream converts its input from little-endian where appropriate.
 boolean readBoolean()
          Reads a boolean value from this stream.
 byte readByte()
          Reads a byte value from this stream.
 char readChar()
          Reads a character value from this stream.
 double readDouble()
          Reads a double value from this stream.
 float readFloat()
          Reads a float value from this stream.
 void readFully(byte[] b)
          Reads an array of bytes from this stream.
 void readFully(byte[] b, int off, int len)
          Reads len bytes from an input stream and stores them in buf starting at off.
 int readInt()
          Reads an integer value from this stream.
 long readLong()
          Reads a long value from this stream.
 short readShort()
          Reads a short value from this stream.
 int readUnsignedByte()
          Reads an unsigned byte value from this stream.
 int readUnsignedShort()
          Reads an unsigned short value from this stream.
 java.lang.String readUTF()
          Reads a UTF-8 string from this stream.
 void setByteChar(boolean bytechar)
          Sets whether or not this stream uses byte-length characters as opposed to 2-byte word characters.
 void setFlags(int flags)
          Sets the flags for this stream.
 void setLittleEndian(boolean little)
          Sets whether or not this stream converts its input from little-endian.
 int skipBytes(int n)
          Skips over n bytes from this stream.
 boolean usesByteChar()
          Returns true if this stream uses byte-length characters as opposed to 2-byte word characters.
 
Methods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, read, read, read, reset, skip
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BIG_ENDIAN

public static final int BIG_ENDIAN
See Also:
Constant Field Values

LITTLE_ENDIAN

public static final int LITTLE_ENDIAN
See Also:
Constant Field Values

BYTE_CHAR

public static final int BYTE_CHAR
See Also:
Constant Field Values
Constructor Detail

BinaryInputStream

public BinaryInputStream(java.io.InputStream in)
Creates a new BinaryInputStream from an input stream. Flag values are set to their defaults so that the stream will be big-endian and use word characters instead of byte characters.

Parameters:
in - underlying input stream

BinaryInputStream

public BinaryInputStream(java.io.InputStream in,
                         int flags)
Creates a new BinaryInputStream from an input stream and prescribed set of flag values.

Parameters:
in - underlying input stream
flags - flag values
Method Detail

setFlags

public void setFlags(int flags)
Sets the flags for this stream.

Parameters:
flags - new flag values

getFlags

public int getFlags()
Gets the flags for this stream.

Returns:
flags for this stream

isLittleEndian

public boolean isLittleEndian()
Returns true if this stream converts its input from little-endian where appropriate.

Returns:
true if this stream converts from little-endian

setLittleEndian

public void setLittleEndian(boolean little)
Sets whether or not this stream converts its input from little-endian.

Parameters:
little - if true, enables little-endian conversion.

usesByteChar

public boolean usesByteChar()
Returns true if this stream uses byte-length characters as opposed to 2-byte word characters.

Returns:
true if this stream uses byte-length characters

setByteChar

public void setByteChar(boolean bytechar)
Sets whether or not this stream uses byte-length characters as opposed to 2-byte word characters.

Parameters:
bytechar - if true, enables byte-length characters.

readFully

public final void readFully(byte[] b)
                     throws java.io.IOException
Reads an array of bytes from this stream. The number of bytes read is equal to the length of the array.

Parameters:
b - array in which to store the bytes
Throws:
java.io.IOException

readFully

public final void readFully(byte[] b,
                            int off,
                            int len)
                     throws java.io.IOException
Reads len bytes from an input stream and stores them in buf starting at off.

Parameters:
b - array in which to store the bytes
off - starting offset for storing bytes
len - number of bytes to read
Throws:
java.io.IOException

skipBytes

public final int skipBytes(int n)
                    throws java.io.IOException
Skips over n bytes from this stream.

Parameters:
n - number of bytes to skip
Returns:
the number of bytes actually skipped
Throws:
java.io.IOException

readBoolean

public final boolean readBoolean()
                          throws java.io.IOException
Reads a boolean value from this stream.

Returns:
the read boolean value
Throws:
java.io.IOException

readByte

public final byte readByte()
                    throws java.io.IOException
Reads a byte value from this stream.

Returns:
the read byte value
Throws:
java.io.IOException

readUnsignedByte

public final int readUnsignedByte()
                           throws java.io.IOException
Reads an unsigned byte value from this stream.

Returns:
the read unsigned byte value
Throws:
java.io.IOException

readShort

public final short readShort()
                      throws java.io.IOException
Reads a short value from this stream. If isLittleEndian() returns true, then the byte order is switched from little-endian to big-endian.

Returns:
the read short value
Throws:
java.io.IOException

readUnsignedShort

public final int readUnsignedShort()
                            throws java.io.IOException
Reads an unsigned short value from this stream. If isLittleEndian() returns true, then the byte order is switched from little-endian to big-endian.

Returns:
the read unsigned short value
Throws:
java.io.IOException

readChar

public final char readChar()
                    throws java.io.IOException
Reads a character value from this stream. The character is either one or two bytes, depending on whether usesByteChar() returns true or false. In the latter case, if isLittleEndian() returns true, then the byte order is switched from little-endian to big-endian.

Returns:
the read character value
Throws:
java.io.IOException

readInt

public final int readInt()
                  throws java.io.IOException
Reads an integer value from this stream. If isLittleEndian() returns true, then the byte order is switched from little-endian to big-endian.

Returns:
the read integer value
Throws:
java.io.IOException

readLong

public final long readLong()
                    throws java.io.IOException
Reads a long value from this stream. If isLittleEndian() returns true, then the byte order is switched from little-endian to big-endian.

Returns:
the read long value
Throws:
java.io.IOException

readFloat

public final float readFloat()
                      throws java.io.IOException
Reads a float value from this stream. If isLittleEndian() returns true, then the byte order is switched from little-endian to big-endian.

Returns:
the read float value
Throws:
java.io.IOException

readDouble

public final double readDouble()
                        throws java.io.IOException
Reads a double value from this stream. If isLittleEndian() returns true, then the byte order is switched from little-endian to big-endian.

Returns:
the read double value
Throws:
java.io.IOException

readUTF

public final java.lang.String readUTF()
                               throws java.io.IOException
Reads a UTF-8 string from this stream.

Returns:
the read UTF string
Throws:
java.io.IOException

getByteCount

public int getByteCount()
Returns the total number of bytes that have been read by this stream.

Returns:
number of bytes read