Class InputStream


  extended by Object
      extended by InputStream

public class InputStream
extends Object

The InputStream class provides for reading bytes from a binary stream and reconstructing from them data in the primitive types. There is also a facility for reconstructing a String from data in Java modified UTF-8 format.

See Also:
OutputStream

Constructor Summary
InputStream(ByteArray array)
          Creates a new InputStream that will read the contents of given ByteArray.
 
Method Summary
 int available()
          Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.
 int read()
          Reads the next byte of data from the input stream.
 int read(ByteArray b)
          Reads some number of bytes from the input stream and stores them into the buffer array b.
 int read(ByteArray b, int off, int len)
          Reads up to len bytes of data from the 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.
 int readByte()
          Reads and returns one input byte.
 char readChar()
          Reads an input char and returns the char value.
 void readFully(ByteArray b)
          Reads some bytes from an input stream and stores them into the buffer array b.
 void readFully(ByteArray b, int off, int len)
          Reads len bytes from a input stream.
 int readInt()
          Reads four input bytes and returns an int value.
 long readLong()
          Reads eight input bytes and returns a long value.
 int readShort()
          Reads two input bytes and returns a short value.
 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 readUnsignedShort()
          Reads two input bytes and returns an int value in the range 0 through 65535.
 String readUTF()
          Reads in a string that has been encoded using a modified UTF-8 format.
 Value readValue()
          Reads a Value from input stream.
 int skip(int n)
          Makes an attempt to skip over n bytes of data from the input stream, discarding the skipped bytes.
 
Methods inherited from class Object
toString, equals, hashCode
 
Methods inherited from
equals, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InputStream

public InputStream(ByteArray array)
Creates a new InputStream that will read the contents of given ByteArray.

Parameters:
array - ByteArray to read.
Method Detail

read

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

readBoolean

public boolean readBoolean()
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 OutputStream.writeBoolean(boolean).

Returns:
the boolean value read.

readByte

public int readByte()
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 OutputStream.writeByte(int).

Returns:
the 8-bit value read.

readChar

public char readChar()
Reads an input char and returns the char value. A Unicode char is made up of two bytes. Let a be the first byte read and b be the second byte. The value returned is:

(char)((a << 8) | (b & 0xff))
 
This method is suitable for reading bytes written by OutputStream.writeChar(char).

Returns:
the Unicode char read.

read

public int read(ByteArray b)
Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.

Parameters:
b - the buffer into which the data is read.
Returns:
the total number of bytes read into the buffer, or -1 is there is no more data because the end of the stream has been reached.

read

public int read(ByteArray b,
                int off,
                int len)
Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read, possibly zero. The number of bytes actually read is returned as an integer.

Parameters:
b - the buffer into which the data is read.
off - the start offset in array b at which the data is written.
len - the maximum number of bytes to 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.

readFully

public void readFully(ByteArray b)
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.

Parameters:
b - the buffer into which the data is read.

readFully

public void readFully(ByteArray b,
                      int off,
                      int len)
Reads len bytes from a 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.

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.

readInt

public int readInt()
Reads four input bytes and returns an int value. Let a be the first byte read, b be the second byte, c be the third byte, and d be the fourth byte. The value returned is:

 
 (((a & 0xff) << 24) | ((b & 0xff) << 16) |
  ((c & 0xff) << 8) | (d & 0xff))
 
This method is suitable for reading bytes written by the OutputStream.writeInt(int).

Returns:
the int value read.

readLong

public long readLong()
Reads eight input bytes and returns a long value. Let a be the first byte read, b be the second byte, c be the third byte, d be the fourth byte, e be the fifth byte, f be the sixth byte, g be the seventh byte, and h be the eighth byte. The value returned is:

 
 (((long)(a & 0xff) << 56) |
  ((long)(b & 0xff) << 48) |
  ((long)(c & 0xff) << 40) |
  ((long)(d & 0xff) << 32) |
  ((long)(e & 0xff) << 24) |
  ((long)(f & 0xff) << 16) |
  ((long)(g & 0xff) <<  8) |
  ((long)(h & 0xff)))
 

This method is suitable for reading bytes written by the OutputStream.writeLong(long).

Returns:
the long value read.

readShort

public int readShort()
Reads two input bytes and returns a short value. Let a be the first byte read and b be the second byte. The value returned is:

(short)((a << 8) * | (b & 0xff))
 
This method is suitable for reading the bytes written by the OutputStream.writeShort(int).

Returns:
the 16-bit value read.

readUnsignedByte

public 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. This method is suitable for reading the byte written by the OutputStream.writeByte(int) if the argument to writeByte was intended to be a value in the range 0 through 255.

Returns:
the unsigned 8-bit value read.

readUnsignedShort

public int readUnsignedShort()
Reads two input bytes and returns an int value in the range 0 through 65535. Let a be the first byte read and b be the second byte. The value returned is:

(((a & 0xff) << 8) | (b & 0xff))
 
This method is suitable for reading the bytes written by the OutputStream.writeShort(int) if the argument to writeShort was intended to be a value in the range 0 through 65535.

Returns:
the unsigned 16-bit value read.

readUTF

public String readUTF()
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.

Returns:
a Unicode string.

readValue

public Value readValue()
Reads a Value from input stream.

Returns:
Read value.

skip

public int skip(int n)
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. The actual number of bytes skipped is returned.

Parameters:
n - the number of bytes to be skipped.
Returns:
the number of bytes skipped, which is always n.

available

public int available()
Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream. The next caller might be the same thread or another thread.

Returns:
the number of bytes that can be read from this input stream without blocking.