com.tffenterprises.io
Class ByteArrayInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.tffenterprises.io.ByteArrayInputStream
All Implemented Interfaces:
AccountingInput, java.io.Closeable, java.io.DataInput

public class ByteArrayInputStream
extends java.io.InputStream
implements java.io.DataInput, AccountingInput

com.tffenterprises.io.ByteArrayInputStream was originally intended as a drop-in replacement for java.io.ByteArrayInputStream. It can function in exactly the same way as the standard java class, with the advantage that the method read(byte[] b) neither declares nor throws any exceptions.

It also departs from the original in that ByteArrayInputStream also implements the java.io.DataInput interface, with methods that only throw the exception java.io.EOFException as exceptions (when the buffer has run out of bytes).

A final point in which it differs from the original is that it implements the AccountingInput interface, keeping track of the number of read/skipped bytes.

The buffer that a ByteArrayInputStream encapsulates is a byte array. Internal variables keeps track of the next byte to be supplied by the read method (the variable pos) and of the last byte that can be supplied by the stream (the variable last.

The current implementation makes no effort to be thread safe.

Version:
1.0d1 $Date: 2002/10/12 19:59:22 $
Author:
Guillaume Lessard

Field Summary
private  byte[] buf
          The byte array from which the bytes are to be read.
private  int first
          The first byte that can be read from the byte array.
private  int last
          The last byte that can be read from the byte array.
private  int mark
          The earliest byte that can be read from the byte array.
private  int pos
          The current read position in the byte array.
 
Constructor Summary
private ByteArrayInputStream()
          Private default constructor, useful for a hypothetical clone() method.
  ByteArrayInputStream(byte[] buf)
          Creates a ByteArrayInputStream so that it uses buf as its buffer array.
  ByteArrayInputStream(byte[] buf, int offset, int length)
          Creates a ByteArrayInputStream that uses buf as its buffer array.
 
Method Summary
 int available()
          Returns the number of bytes that remain to be read in the byte array.
 void close()
          Closes this stream.
 long consumed()
          Returns the number of bytes that have been read from the byte array.
 void mark(int readLimit)
          Set the current marked position in the stream.
 boolean markSupported()
          Reports that the mark() method from InputStream is supported.
 int read()
          Reads the next byte of data from the byte array.
 int read(byte[] b)
          Reads a number of bytes of data into an array of bytes from this input stream.
 int read(byte[] b, int offset, int length)
          Reads a number of bytes of data into an array of bytes from this input stream.
 boolean readBoolean()
          Returns a boolean value from the encapsulated byte array.
 byte readByte()
          Returns a byte value from the encapsulated byte array.
 char readChar()
          Returns a char value from the byte array.
 double readDouble()
          Returns a double value from the encapsulated byte array.
 float readFloat()
          Returns a float value from the encapsulated byte array.
 void readFully(byte[] b)
          Reads some bytes from an input stream and stores them into the buffer array b.
 void readFully(byte[] b, int offset, int length)
          Reads some byte from an input stream and stores them into the buffer array b, starting from the offset offset.
 int readInt()
          Returns an int value from the encapsulated byte array.
 java.lang.String readLine()
          Returns a string representing one line of data (encoded as ISO-8859-1).
 long readLong()
          Returns a long value from the encapsulated byte array.
 short readShort()
          Returns a short value from the encapsulated byte array.
 int readUnsignedByte()
          Returns an unsigned byte value from the encapsulated byte array.
 int readUnsignedShort()
          Returns an unsigned short value from the bencapsulated yte array.
 java.lang.String readUTF()
          Returns a String that had been encoded as the java modified UTF-8 format.
 void reset()
          Resets the buffer to the marked position.
 long skip(long n)
          Skips n bytes of input from this input stream.
 int skipBytes(int n)
          Attempt to skip n bytes from the buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

buf

private byte[] buf
The byte array from which the bytes are to be read.


pos

private int pos
The current read position in the byte array.


last

private int last
The last byte that can be read from the byte array.


first

private int first
The first byte that can be read from the byte array.


mark

private int mark
The earliest byte that can be read from the byte array.

Constructor Detail

ByteArrayInputStream

private ByteArrayInputStream()
Private default constructor, useful for a hypothetical clone() method.


ByteArrayInputStream

public ByteArrayInputStream(byte[] buf)
                     throws java.lang.IllegalArgumentException
Creates a ByteArrayInputStream so that it uses buf as its buffer array. The buffer array is not copied.

Using this constructor is equivalent to using ByteArrayInputStream(buf, 0, buf.length).

Parameters:
buf - the input buffer.
Throws:
java.lang.IllegalArgumentException - if buf is null.

ByteArrayInputStream

public ByteArrayInputStream(byte[] buf,
                            int offset,
                            int length)
                     throws java.lang.IllegalArgumentException
Creates a ByteArrayInputStream that uses buf as its buffer array. The initial value of pos is offset and the initial value of count is offset+len. The buffer array is not copied.

Unlike the java.io.ByteArrayInputStream class, elements buf[0] through buf[pos-1] cannot become available for input.

Parameters:
buf - the input buffer.
offset - the offset in the buffer of the first byte to read.
length - the maximum number of bytes to read from the buffer.
Throws:
java.lang.IllegalArgumentException - if buf is null.
Method Detail

available

public int available()
Returns the number of bytes that remain to be read in the byte array. The value returned is last - pos, which is the number of bytes remaining to be read from the byte array.

Overrides:
available in class java.io.InputStream
Returns:
the number of bytes taht remain to be read in the byte array.

consumed

public long consumed()
Returns the number of bytes that have been read from the byte array. The value returned is pos - first.

Specified by:
consumed in interface AccountingInput
Returns:
the number of bytes that have been read from the byte array.

markSupported

public boolean markSupported()
Reports that the mark() method from InputStream is supported.

Overrides:
markSupported in class java.io.InputStream
Returns:
true, to indicate that the mark() method works.

mark

public void mark(int readLimit)
Set the current marked position in the stream. ByteArrayInputStream objects are marked at their initial position when constructed. They may be marked at another position within the buffer by this method.

Overrides:
mark in class java.io.InputStream
Parameters:
readLimit - the number of bytes to be read before the marked position gets invalidated. This parameter is ignored.

reset

public void reset()
Resets the buffer to the marked position. The marked position is the initial offset in the buffer, unless another position was marked.

Overrides:
reset in class java.io.InputStream

close

public void close()
Closes this stream.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream

read

public int read()
Reads the next byte of data from the byte array. The value 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 cannot block.

Specified by:
read in class java.io.InputStream
Returns:
the next byte of data, or -1 if the end of the array is reached.

read

public int read(byte[] b)
Reads a number of bytes of data into an array of bytes from this input stream. If pos equals count, then -1 is returned to indicate an end-of-file situation. Otherwise, the number k of bytes read is equal to the smaller of buf.length and count-pos. If k is positive, then bytes buf[pos] through buf[pos+k-1] are copied into b[0] through b[k-1] in the manner performed by System.arraycopy. The value k is added into pos and k is returned.

This read method cannot block.

Overrides:
read in class java.io.InputStream
Parameters:
b - the buffer into which the data is read from the encapsulated byte array.
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.

read

public int read(byte[] b,
                int offset,
                int length)
Reads a number of bytes of data into an array of bytes from this input stream. If pos equals last, then -1 is returned to indicate an end-of-file situation. Otherwise, the number k of bytes read is equal to the smaller of buf.length and last-pos. If k is positive, then bytes buf[pos] through buf[pos+k-1] are copied into b[0] through b[k-1] in the manner performed by System.arraycopy. The value k is added into pos and k is returned.

This read method cannot block.

Overrides:
read in class java.io.InputStream
Parameters:
b - the buffer into which to copy the bytes to be read.
offset - the offset at which to start copying bytes into b.
length - the number of bytes to be read and copied.
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.

skip

public long skip(long n)
Skips n bytes of input from this input stream. Fewer bytes might be skipped if the end of the input stream is reached. The actual number of bytes to be skipped is equal to the smaller of n and last-pos. The number of bytes that were skipped is returned.

This skip method cannot block.

Overrides:
skip in class java.io.InputStream
Parameters:
n - the number of bytes to be skipped.
Returns:
the number of bytes that were actually skipped.

readBoolean

public boolean readBoolean()
                    throws java.io.EOFException
Returns a boolean value from the encapsulated byte array.

Specified by:
readBoolean in interface java.io.DataInput
Returns:
a boolean value from the encapsulated byte array.
Throws:
java.io.EOFException - if EOF is reached before the bytes are read.

readByte

public final byte readByte()
                    throws java.io.EOFException
Returns a byte value from the encapsulated byte array.

Specified by:
readByte in interface java.io.DataInput
Returns:
a byte value from the encapsulated byte array.
Throws:
java.io.EOFException - if EOF is reached before the bytes are read.

readUnsignedByte

public int readUnsignedByte()
                     throws java.io.EOFException
Returns an unsigned byte value from the encapsulated byte array.

Specified by:
readUnsignedByte in interface java.io.DataInput
Returns:
an unsigned byte value from the encapsulated byte array.
Throws:
java.io.EOFException - if EOF is reached before the bytes are read.

readShort

public final short readShort()
                      throws java.io.EOFException
Returns a short value from the encapsulated byte array.

Specified by:
readShort in interface java.io.DataInput
Returns:
a short value from the encapsulated byte array.
Throws:
java.io.EOFException - if EOF is reached before the bytes are read.

readUnsignedShort

public int readUnsignedShort()
                      throws java.io.EOFException
Returns an unsigned short value from the bencapsulated yte array.

Specified by:
readUnsignedShort in interface java.io.DataInput
Returns:
an unsigned short value from the bencapsulated yte array.
Throws:
java.io.EOFException - if EOF is reached before the bytes are read.

readChar

public final char readChar()
                    throws java.io.EOFException
Returns a char value from the byte array.

Specified by:
readChar in interface java.io.DataInput
Returns:
a char value from the byte array.
Throws:
java.io.EOFException - if EOF is reached before the bytes are read.

readInt

public int readInt()
            throws java.io.EOFException
Returns an int value from the encapsulated byte array.

Specified by:
readInt in interface java.io.DataInput
Returns:
an int value from the encapsulated byte array.
Throws:
java.io.EOFException - if EOF is reached before the bytes are read.

readLong

public long readLong()
              throws java.io.EOFException
Returns a long value from the encapsulated byte array.

Specified by:
readLong in interface java.io.DataInput
Returns:
a long value from the encapsulated byte array.
Throws:
java.io.EOFException - if EOF is reached before the bytes are read.

readFloat

public final float readFloat()
                      throws java.io.EOFException
Returns a float value from the encapsulated byte array.

Specified by:
readFloat in interface java.io.DataInput
Returns:
a float value from the encapsulated byte array.
Throws:
java.io.EOFException - if EOF is reached before the bytes are read.

readDouble

public final double readDouble()
                        throws java.io.EOFException
Returns a double value from the encapsulated byte array.

Specified by:
readDouble in interface java.io.DataInput
Returns:
a double value from the encapsulated byte array.
Throws:
java.io.EOFException - if EOF is reached before the bytes are read.

readLine

public java.lang.String readLine()
Returns a string representing one line of data (encoded as ISO-8859-1). It reads successive bytes, converting each byte separately into a character, until it encounters a line terminator or end of file; the characters read are then returned as a String. Note that because this method processes bytes, it does not support input of the full Unicode character set.

If end of file is encountered before even one byte can be read, then null is returned. Otherwise, each byte that is read is converted to type char by zero-extension. If the character '\n' is encountered, it is discarded and reading ceases. If the character '\r' is encountered, it is discarded and, if the following byte converts to the character '\n', then that is discarded also; reading then ceases. If end of file is encountered before either of the characters '\n' and '\r' is encountered, reading ceases. Once reading has ceased, a String is returned that contains all the characters read and not discarded, taken in order. Note that every character in this string will have a value less than ?, that is, (char)256.

Specified by:
readLine in interface java.io.DataInput
Returns:
a String representing one line of data.

readUTF

public java.lang.String readUTF()
                         throws java.io.EOFException,
                                java.io.UTFDataFormatException
Returns a String that had been encoded as the java modified UTF-8 format.

Specified by:
readUTF in interface java.io.DataInput
Returns:
a String that had been encoded as the java modified UTF-8 format.
Throws:
java.io.EOFException - if EOF is reached before the bytes are read.
java.io.UTFDataFormatException - if the UTF data is malformed.

readFully

public void readFully(byte[] b)
               throws java.io.EOFException
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.

Specified by:
readFully in interface java.io.DataInput
Parameters:
b - the buffer into which to copy the bytes to be read.
Throws:
java.io.EOFException - if the end of the input buffer is reached before all the bytes have been read.

readFully

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

Specified by:
readFully in interface java.io.DataInput
Parameters:
b - the buffer into which to copy the bytes to be read.
offset - the offset at which to start copying bytes into b.
length - the number of bytes to be read and copied.
Throws:
java.io.EOFException - if the end of the input buffer is reached before all the bytes have been read.

skipBytes

public int skipBytes(int n)
Attempt to skip n bytes from the buffer.

Specified by:
skipBytes in interface java.io.DataInput
Parameters:
n - the number of bytes to be skipped.
Returns:
the actual number of bytes that were skipped.