Java IO Tutorial - Java InputStream








The abstract base component is the InputStream class.

InputStream
 |
 +--FileInputStream 
 |
 +--ByteArrayInputStream 
 |
 +--PipedInputStream
 |
 +--FilterInputStream
 |
 +--BufferedInputStream 
 |
 +--PushbackInputStream 
 |
 +--DataInputStream 
 |
 +--ObjectInputStream

We have concrete classes of FileInputStream, ByteArrayInputStream, and PipedInputStream, FilterInputStream.





Methods

The superclass InputStream contains the basic methods to read data from an input stream, which are supported by all concrete classes.

The basic operation on an input stream is to read data from it. Some important methods defined in the InputStream class are listed in the following table.

IDMethod/Description
1read()
Reads one byte and returns the read byte as an int.
It returns -1 when the end of the input stream is reached.
2read(byte[] buffer)
Reads maximum up to the length of the specified buffer.
It returns the number of bytes read in the buffer.
It returns -1 if the end of the input stream is reached.
3read(byte[] buffer,int offset, int length)
Reads maximum up to the specified length bytes.
The data is written in the buffer starting from the offset index.
It returns the number of bytes read or -1 if the end of the input stream is reached.
3close()
Closes the input stream
4available()
Returns the estimated number of bytes that can be read from this input stream without blocking.