Contents | Prev | Next | Index

22.3 The Class java.io.InputStream

An input stream makes input bytes available from some source.

public abstract class InputStream {
	public abstract int read() throws IOException;
	public int read(byte[] b)
	throws IOException, NullPointerException;
	public int read(byte[] b, int off, int len)
		throws IOException, NullPointerException,
			IndexOutOfBoundsException;
	public long skip(long n) throws IOException;
	public int available() throws IOException;
	public void close() throws IOException;
	public void mark(int readlimit);
	public void reset() throws IOException;
	public boolean markSupported();
}

22.3.1 public abstract int read() throws IOException

The general contract of read is that it reads one byte from the input stream. The byte is returned as an integer in the range 0 to 255 (0x00-0xff). If no byte is available because the stream is at end of file, the value -1 is returned.

This method blocks until input data is available, end of file is detected, or an exception is thrown.

If the byte cannot be read for any reason other than end of file, an IOException is thrown. In particular, an IOException is thrown if the input stream has been closed (§22.3.6).

22.3.2 public int read(byte[] b)
throws IOException, NullPointerException

The general contract of read(b) is that it 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.

If b is null, a NullPointerException is thrown.

If the length of b is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

The first byte read is stored into element b[0], the next one into b[1], and so on. The number of bytes read is, at most, equal to the length of b. Let k be the number of bytes actually read; these bytes will be stored in elements b[0] through b[k-1], leaving elements b[k] through b[b.length-1] unaffected.

If the first byte cannot be read for any reason other than end of file, then an IOException is thrown. In particular, an IOException is thrown if the input stream has been closed (§22.15.5).

The read(b) method for class InputStream has the same effect as:

read(b, 0, b.length)

22.3.3 public int read(byte[] b, int off, int len)
throws IOException, NullPointerException,
IndexOutOfBoundsException

The general contract of read(b, off, len) is that it reads some number of bytes from the input stream and stores them into the buffer array b. 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.

This method blocks until input data is available, end of file is detected, or an exception is thrown.

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 and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

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. Let k be the number of bytes actually read; these bytes will be stored in elements b[off] through b[off+k-1], leaving elements b[off+k] through b[off+len-1] unaffected.

In every case, elements b[0] through b[off] and elements b[off+len] through b[b.length-1] are unaffected.

If the first byte cannot be read for any reason other than end of file, then an IOException is thrown. In particular, an IOException is thrown if the input stream has been closed (§22.15.5).

The read(b, off, len) method for class InputStream simple calls the method read() repeatedly. If the first such call results in an IOException, that exception is returned from the call to the read(b, off, len) method. If any subsequent call to read() results in a IOException, the exception is caught and treated as if it were end of file; the bytes read up to that point are stored into b and the number of bytes read before the exception occurred is returned.

22.3.4 public long skip(long n) throws IOException

The general contract of skip is that it 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.

22.3.5 public int available() throws IOException

The general contract of available is that it returns an integer k; the next caller of a method for this input stream, which might be the same thread or another thread, can then expect to be able to read or skip up to k bytes without blocking (waiting for input data to arrive).

The available method for class InputStream always returns 0.

22.3.6 public int close() throws IOException

The general contract of close is that it closes the input stream. A closed stream cannot perform input operations and cannot be reopened.

The close method for class InputStream does nothing and simply returns.

22.3.7 public void mark(int readlimit)

The general contract of mark is that, if the method markSupported returns true, the stream somehow remembers all the bytes read after the call to mark and stands ready to supply those same bytes again if and whenever the method reset is called. However, the stream is not required to remember any data at all if more than readlimit bytes are read from the stream before reset is called.

The mark method for class InputStream does nothing.

22.3.8 public void reset() throws IOException

The general contract of reset is:

The method reset for class InputStream always throws an IOException.

22.3.9 public boolean markSupported()

The general contract of markSupported is that if it returns true, then the stream supports the mark (§22.3.7) and reset (§22.3.8) operations. For any given instance of InputStream, this method should consistently return the same truth value whenever it is called.

The markSupported method for class InputStream returns false.


Contents | Prev | Next | Index

Java Language Specification (HTML generated by Suzette Pelouch on February 24, 1998)
Copyright © 1996 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to [email protected]