com.isti.util
Class IstiDataInputStream

java.lang.Object
  extended byjava.io.InputStream
      extended byjava.io.FilterInputStream
          extended bycom.isti.util.IstiDataInputStream
All Implemented Interfaces:
java.io.DataInput

public class IstiDataInputStream
extends java.io.FilterInputStream
implements java.io.DataInput


Field Summary
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
IstiDataInputStream(java.io.InputStream in)
          Create an IstiDataInputStream that reads from the specified InputStream.
IstiDataInputStream(java.io.InputStream in, boolean little)
          Create an IstiDataInputStream that reads from the specified InputStream.
 
Method Summary
 int available()
          Returns the number of bytes that can be read from this input stream without blocking.
 void close()
          Closes this input stream and releases any system resources associated with the stream.
 boolean isBigEndian()
          Determines if the default is big-endian.
 boolean isLittleEndian()
          Determines if the default is little-endian.
 void mark(int readlimit)
          Marks the current position in this input stream.
 boolean markSupported()
          Tests if this input stream supports the mark and reset methods.
 int read()
          Reads the next byte of data from this input stream.
 int read(byte[] b)
          Reads up to byte.length bytes of data from this input stream into an array of bytes.
 int read(byte[] b, int off, int len)
          Reads up to len bytes of data from this input stream into an array of bytes.
 boolean readBoolean()
          Reads one input byte and returns true if that byte is nonzero, false if that byte is zero.
 byte readByte()
          Reads and returns one input byte.
 byte[] readByteArray(int length)
          Reads an array of bytes.
 char readChar()
          Reads a 16 bit char.
 char[] readCharArray(int length)
          Reads an array of chars.
 double readDouble()
          Reads a 64 bit double.
 double[] readDoubleArray(int length)
          Reads an array of doubles.
 float readFloat()
          Reads a 32 bit float.
 float[] readFloatArray(int length)
          Reads an array of floats.
 void readFully(byte[] b)
          Reads some bytes from an input stream and stores them into the buffer array b.
 void readFully(byte[] b, int off, int len)
          Reads len bytes from an input stream.
 int readInt()
          Reads a 32 bit int.
 int[] readIntArray(int length)
          Reads an array of ints.
 java.lang.String readLine()
          Read a line of text.
 long readLong()
          Reads a 64 bit long.
 long[] readLongArray(int length)
          Reads an array of longs.
 short readShort()
          Reads a 16 bit short.
 short[] readShortArray(int length)
          Reads an array of shorts.
 int readUnsignedByte()
          Reads one input byte, zero-extends it to type int, and returns the result, which is therefore in the range 0 through 255.
 int[] readUnsignedByteArray(int length)
          Reads an array of unsigned bytes.
 int readUnsignedShort()
          Reads an unsigned 16 bit short.
 int[] readUnsignedShortArray(int length)
          Reads an array of unsigned shorts.
 java.lang.String readUTF()
          Reads in a string that has been encoded using a modified UTF-8 format.
static java.lang.String readUTF(java.io.DataInput in)
          Reads from the stream in a representation of a Unicode character string encoded in Java modified UTF-8 format; this string of characters is then returned as a String.
 void reset()
          Repositions this stream to the position at the time the mark method was last called on this input stream.
 void setBigEndian(boolean big)
          Sets the default to big-endian or little-endian.
 void setLittleEndian(boolean little)
          Sets the default to little-endian or big-endian.
 long skip(long n)
          Skips over and discards n bytes of data from the input stream.
 int skipBytes(int n)
          Makes an attempt to skip over n bytes of data from the input stream, discarding the skipped bytes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IstiDataInputStream

public IstiDataInputStream(java.io.InputStream in)
                    throws java.io.IOException,
                           java.io.StreamCorruptedException
Create an IstiDataInputStream that reads from the specified InputStream. This is the same as an DataInputStream with the following changes: supports reading little-endian as well as big-endian binary data, uses a BufferedReader for the readLine method, and supports reading arrays. The default is initially big-endian.

Parameters:
in - the underlying InputStream from which to read
Throws:
java.io.StreamCorruptedException - The version or magic number are incorrect.
java.io.IOException - An exception occurred in the underlying stream.

IstiDataInputStream

public IstiDataInputStream(java.io.InputStream in,
                           boolean little)
                    throws java.io.IOException,
                           java.io.StreamCorruptedException
Create an IstiDataInputStream that reads from the specified InputStream. This is the same as an DataInputStream except it supports reading little-endian as well as big-endian binary data.

Parameters:
in - the underlying InputStream from which to read
little - if true default is little-endian otherwise it is big-endian.
Throws:
java.io.StreamCorruptedException - The version or magic number are incorrect.
java.io.IOException - An exception occurred in the underlying stream.
Method Detail

isBigEndian

public boolean isBigEndian()
Determines if the default is big-endian.

Returns:
true if the default is big-endian.

isLittleEndian

public boolean isLittleEndian()
Determines if the default is little-endian.

Returns:
true if the default is little-endian.

setBigEndian

public void setBigEndian(boolean big)
Sets the default to big-endian or little-endian.

Parameters:
big - if true default is big-endian otherwise it is little-endian.

setLittleEndian

public void setLittleEndian(boolean little)
Sets the default to little-endian or big-endian.

Parameters:
little - if true default is little-endian otherwise it is big-endian.

read

public int read()
         throws java.io.IOException
Reads the next byte of data from this input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Returns:
the next byte of data, or -1 if the end of the stream is reached.
Throws:
java.io.IOException - if an I/O error occurs.
See Also:
FilterInputStream.in

read

public int read(byte[] b)
         throws java.io.IOException
Reads up to byte.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available.

Parameters:
b - the buffer into which the data is read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
java.io.IOException - if an I/O error occurs.
See Also:
FilterInputStream.read(byte[], int, int)

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Reads up to len bytes of data from this input stream into an array of bytes. This method blocks until some input is available.

Parameters:
b - the buffer into which the data is read.
off - the start offset of the data.
len - the maximum number of bytes read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
java.io.IOException - if an I/O error occurs.
See Also:
FilterInputStream.in

skip

public long skip(long n)
          throws java.io.IOException
Skips over and discards n bytes of data from the input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. The actual number of bytes skipped is returned.

Parameters:
n - the number of bytes to be skipped.
Returns:
the actual number of bytes skipped.
Throws:
java.io.IOException - if an I/O error occurs.

available

public int available()
              throws java.io.IOException
Returns the number of bytes that can be read from this input stream without blocking.

Returns:
the number of bytes that can be read from the input stream without blocking.
Throws:
java.io.IOException - if an I/O error occurs.
See Also:
FilterInputStream.in

close

public void close()
           throws java.io.IOException
Closes this input stream and releases any system resources associated with the stream.

Throws:
java.io.IOException - if an I/O error occurs.
See Also:
FilterInputStream.in

mark

public void mark(int readlimit)
Marks the current position in this input stream. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.

The readlimit argument tells this input stream to allow that many bytes to be read before the mark position gets invalidated.

Parameters:
readlimit - the maximum limit of bytes that can be read before the mark position becomes invalid.
See Also:
FilterInputStream.in, FilterInputStream.reset()

reset

public void reset()
           throws java.io.IOException
Repositions this stream to the position at the time the mark method was last called on this input stream.

Stream marks are intended to be used in situations where you need to read ahead a little to see what's in the stream. Often this is most easily done by invoking some general parser. If the stream is of the type handled by the parse, it just chugs along happily. If the stream is not of that type, the parser should toss an exception when it fails. If this happens within readlimit bytes, it allows the outer code to reset the stream and try another parser.

Throws:
java.io.IOException - if the stream has not been marked or if the mark has been invalidated.
See Also:
FilterInputStream.in, FilterInputStream.mark(int)

markSupported

public boolean markSupported()
Tests if this input stream supports the mark and reset methods.

Returns:
true if this stream type supports the mark and reset method; false otherwise.
See Also:
FilterInputStream.in, InputStream.mark(int), InputStream.reset()

readFully

public void readFully(byte[] b)
               throws java.io.IOException
Reads some bytes from an input stream and stores them into the buffer array b. The number of bytes read is equal to the length of b.

This method blocks until one of the following conditions occurs:

If b is null, a NullPointerException is thrown. If b.length is zero, then no bytes are read. Otherwise, the first byte read is stored into element b[0], the next one into b[1], and so on. If an exception is thrown from this method, then it may be that some but not all bytes of b have been updated with data from the input stream.

Specified by:
readFully in interface java.io.DataInput
Parameters:
b - the buffer into which the data is read.
Throws:
java.io.EOFException - if this stream reaches the end before reading all the bytes.
java.io.IOException - if an I/O error occurs.

readFully

public void readFully(byte[] b,
                      int off,
                      int len)
               throws java.io.IOException
Reads len bytes from an input stream.

This method blocks until one of the following conditions occurs:

If b is null, a NullPointerException is thrown. If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown. If len is zero, then no bytes are read. Otherwise, the first byte read is stored into element b[off], the next one into b[off+1], and so on. The number of bytes read is, at most, equal to len.

Specified by:
readFully in interface java.io.DataInput
Parameters:
b - the buffer into which the data is read.
off - an int specifying the offset into the data.
len - an int specifying the number of bytes to read.
Throws:
java.io.EOFException - if this stream reaches the end before reading all the bytes.
java.io.IOException - if an I/O error occurs.

skipBytes

public int skipBytes(int n)
              throws java.io.IOException
Makes an attempt to skip over n bytes of data from the input stream, discarding the skipped bytes. However, it may skip over some smaller number of bytes, possibly zero. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. This method never throws an EOFException. The actual number of bytes skipped is returned.

Specified by:
skipBytes in interface java.io.DataInput
Parameters:
n - the number of bytes to be skipped.
Returns:
the number of bytes skipped, which is always n.
Throws:
java.io.EOFException - if this stream reaches the end before skipping all the bytes.
java.io.IOException - if an I/O error occurs.

readBoolean

public boolean readBoolean()
                    throws java.io.IOException
Reads one input byte and returns true if that byte is nonzero, false if that byte is zero. This method is suitable for reading the byte written by the writeBoolean method of interface DataOutput.

Specified by:
readBoolean in interface java.io.DataInput
Returns:
the boolean value read.
Throws:
java.io.EOFException - if this stream reaches the end before reading all the bytes.
java.io.IOException - if an I/O error occurs.

readByte

public byte readByte()
              throws java.io.IOException
Reads and returns one input byte. The byte is treated as a signed value in the range -128 through 127, inclusive. This method is suitable for reading the byte written by the writeByte method of interface DataOutput.

Specified by:
readByte in interface java.io.DataInput
Returns:
the 8-bit value read.
Throws:
java.io.EOFException - if this stream reaches the end before reading all the bytes.
java.io.IOException - if an I/O error occurs.

readUnsignedByte

public int readUnsignedByte()
                     throws java.io.IOException
Reads one input byte, zero-extends it to type int, and returns the result, which is therefore in the range 0 through 255. This method is suitable for reading the byte written by the writeByte method of interface DataOutput if the argument to writeByte was intended to be a value in the range 0 through 255.

Specified by:
readUnsignedByte in interface java.io.DataInput
Returns:
the unsigned 8-bit value read.
Throws:
java.io.EOFException - if this stream reaches the end before reading all the bytes.
java.io.IOException - if an I/O error occurs.

readShort

public short readShort()
                throws java.io.IOException
Reads a 16 bit short.

Specified by:
readShort in interface java.io.DataInput
Returns:
the 16 bit short read.
Throws:
java.io.EOFException - If end of file is reached.
java.io.IOException - If other I/O error has occurred.

readUnsignedShort

public int readUnsignedShort()
                      throws java.io.IOException
Reads an unsigned 16 bit short.

Specified by:
readUnsignedShort in interface java.io.DataInput
Returns:
the 16 bit short read.
Throws:
java.io.EOFException - If end of file is reached.
java.io.IOException - If other I/O error has occurred.

readChar

public char readChar()
              throws java.io.IOException
Reads a 16 bit char.

Specified by:
readChar in interface java.io.DataInput
Returns:
the 16 bit char read.
Throws:
java.io.EOFException - If end of file is reached.
java.io.IOException - If other I/O error has occurred.

readInt

public int readInt()
            throws java.io.IOException
Reads a 32 bit int.

Specified by:
readInt in interface java.io.DataInput
Returns:
the 32 bit integer read.
Throws:
java.io.EOFException - If end of file is reached.
java.io.IOException - If other I/O error has occurred.

readLong

public long readLong()
              throws java.io.IOException
Reads a 64 bit long.

Specified by:
readLong in interface java.io.DataInput
Returns:
the read 64 bit long.
Throws:
java.io.EOFException - If end of file is reached.
java.io.IOException - If other I/O error has occurred.

readFloat

public float readFloat()
                throws java.io.IOException
Reads a 32 bit float.

Specified by:
readFloat in interface java.io.DataInput
Returns:
the 32 bit float read.
Throws:
java.io.EOFException - If end of file is reached.
java.io.IOException - If other I/O error has occurred.

readDouble

public double readDouble()
                  throws java.io.IOException
Reads a 64 bit double.

Specified by:
readDouble in interface java.io.DataInput
Returns:
the 64 bit double read.
Throws:
java.io.EOFException - If end of file is reached.
java.io.IOException - If other I/O error has occurred.

readLine

public java.lang.String readLine()
                          throws java.io.IOException
Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

Specified by:
readLine in interface java.io.DataInput
Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
Throws:
java.io.IOException - If an I/O error occurs

readUTF

public java.lang.String readUTF()
                         throws java.io.IOException
Reads in a string that has been encoded using a modified UTF-8 format. The general contract of readUTF is that it reads a representation of a Unicode character string encoded in Java modified UTF-8 format; this string of characters is then returned as a String.

First, two bytes are read and used to construct an unsigned 16-bit integer in exactly the manner of the readUnsignedShort method . This integer value is called the UTF length and specifies the number of additional bytes to be read. These bytes are then converted to characters by considering them in groups. The length of each group is computed from the value of the first byte of the group. The byte following a group, if any, is the first byte of the next group.

If the first byte of a group matches the bit pattern 0xxxxxxx (where x means "may be 0 or 1"), then the group consists of just that byte. The byte is zero-extended to form a character.

If the first byte of a group matches the bit pattern 110xxxxx, then the group consists of that byte a and a second byte b. If there is no byte b (because byte a was the last of the bytes to be read), or if byte b does not match the bit pattern 10xxxxxx, then a UTFDataFormatException is thrown. Otherwise, the group is converted to the character:

(char)(((a& 0x1F) << 6) | (b & 0x3F))
 
If the first byte of a group matches the bit pattern 1110xxxx, then the group consists of that byte a and two more bytes b and c. If there is no byte c (because byte a was one of the last two of the bytes to be read), or either byte b or byte c does not match the bit pattern 10xxxxxx, then a UTFDataFormatException is thrown. Otherwise, the group is converted to the character:


 (char)(((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F))
 
If the first byte of a group matches the pattern 1111xxxx or the pattern 10xxxxxx, then a UTFDataFormatException is thrown.

If end of file is encountered at any time during this entire process, then an EOFException is thrown.

After every group has been converted to a character by this process, the characters are gathered, in the same order in which their corresponding groups were read from the input stream, to form a String, which is returned.

The writeUTF method of interface DataOutput may be used to write data that is suitable for reading by this method.

Specified by:
readUTF in interface java.io.DataInput
Returns:
a Unicode string.
Throws:
java.io.EOFException - if this stream reaches the end before reading all the bytes.
java.io.IOException - if an I/O error occurs.
java.io.UTFDataFormatException - if the bytes do not represent a valid UTF-8 encoding of a string.

readUTF

public static final java.lang.String readUTF(java.io.DataInput in)
                                      throws java.io.IOException
Reads from the stream in a representation of a Unicode character string encoded in Java modified UTF-8 format; this string of characters is then returned as a String. The details of the modified UTF-8 representation are exactly the same as for the readUTF method of DataInput.

Parameters:
in - a data input stream.
Returns:
a Unicode string.
Throws:
java.io.EOFException - if the input stream reaches the end before all the bytes.
java.io.IOException - if an I/O error occurs.
java.io.UTFDataFormatException - if the bytes do not represent a valid UTF-8 encoding of a Unicode string.
See Also:
DataInputStream.readUnsignedShort()

readByteArray

public byte[] readByteArray(int length)
                     throws java.io.IOException
Reads an array of bytes.

Parameters:
length - array length
Returns:
the array.
Throws:
java.io.EOFException - If end of file is reached.
java.io.IOException - If other I/O error has occurred.

readUnsignedByteArray

public int[] readUnsignedByteArray(int length)
                            throws java.io.IOException
Reads an array of unsigned bytes.

Parameters:
length - array length
Returns:
the array.
Throws:
java.io.EOFException - If end of file is reached.
java.io.IOException - If other I/O error has occurred.

readShortArray

public short[] readShortArray(int length)
                       throws java.io.IOException
Reads an array of shorts.

Parameters:
length - array length
Returns:
the array.
Throws:
java.io.EOFException - If end of file is reached.
java.io.IOException - If other I/O error has occurred.

readUnsignedShortArray

public int[] readUnsignedShortArray(int length)
                             throws java.io.IOException
Reads an array of unsigned shorts.

Parameters:
length - array length
Returns:
the array.
Throws:
java.io.EOFException - If end of file is reached.
java.io.IOException - If other I/O error has occurred.

readCharArray

public char[] readCharArray(int length)
                     throws java.io.IOException
Reads an array of chars.

Parameters:
length - array length
Returns:
the array.
Throws:
java.io.EOFException - If end of file is reached.
java.io.IOException - If other I/O error has occurred.

readIntArray

public int[] readIntArray(int length)
                   throws java.io.IOException
Reads an array of ints.

Parameters:
length - array length
Returns:
the array.
Throws:
java.io.EOFException - If end of file is reached.
java.io.IOException - If other I/O error has occurred.

readLongArray

public long[] readLongArray(int length)
                     throws java.io.IOException
Reads an array of longs.

Parameters:
length - array length
Returns:
the array.
Throws:
java.io.EOFException - If end of file is reached.
java.io.IOException - If other I/O error has occurred.

readFloatArray

public float[] readFloatArray(int length)
                       throws java.io.IOException
Reads an array of floats.

Parameters:
length - array length
Returns:
the array.
Throws:
java.io.EOFException - If end of file is reached.
java.io.IOException - If other I/O error has occurred.

readDoubleArray

public double[] readDoubleArray(int length)
                         throws java.io.IOException
Reads an array of doubles.

Parameters:
length - array length
Returns:
the array.
Throws:
java.io.EOFException - If end of file is reached.
java.io.IOException - If other I/O error has occurred.