InputStream in Java

InputStream class in Java is an abstract class that is meant for reading binary data from a file to a Java application. It is the superclass of all Java Input stream classes and contains methods for reading binary data from the file.

InputStream class in Java is defined in java.io package and it implements the Closeable interface. InputStream and all its subclasses are available from the initial version of Java.

Java InputStream class Methods

There are different methods available in the InputStream class. Among these methods, only the read() method is an abstract method and the remaining all are concrete methods. Except for markSupported(), and mark() method all other methods throws IOException.

Modifier and typeMethodDescription
intavailable()Returns number of bytes can be read or skipped over from the input stream without blocking
abstract intread()Reads the next one byte of data from the input stream.
intread(byte[] b)Reads some number of bytes and store them into the buffer array b.
intread(byte[] b, int offset, int len)It reads up to len bytes of data from the input stream and stores them into the buffer array b.
booleanmarkSupported()mark and reset methods supported or not.
voidmark(int readlimit)Marks the current position in this input stream.
voidreset()Reset the cursor position.
longskip(long n)Skips over and discards n bytes of data from the input stream.
voidskipNBytes(long n)Skips over and discards exactly n bytes of data from the input stream.
voidclose()It closes the stream and releases any system resources associated with the stream

Difference between read() and read(byte[] b)? Both methods return type is an int but read() is an abstract method and generally, it returns the one-byte value from the file as int data type value. Whereas read(byte[] b) returns the number of bytes read from the stream into a byte array.

Other methods after Java 1.8 versions are,

Return valueMethodDescriptionSince
Java
intreadAllBytes()Reads all remaining bytes from the input stream.9
byte[]ReadNBytes(int len)Reads up to a specified number of bytes.11
intreadNBytes(byte[] b, int off, int len)It Reads the requested number of bytes into the given byte array.9
longtransferTo(OutputStream out)Reads all bytes from the input stream and writes the bytes to the given output stream in the order that they are read.9
Static InputStreamnullInputStream()Returns a new InputStream that reads no bytes.11

Sub classes of Java InputStream

It has different sub classes based on the sources and formats to the data. It has totally 9 sub classes.

  • FileInputStream
  • ByteArrayInputStream
  • FilterInputStream
  • ObjectInputStream
  • PipedInputStream
  • SequenceInputStream
  • StringBufferInputStream

Among these 9 classes FileInputStream, DataInputStream and ObjectInputStream are important classes,

FileInputStreamRead one-byte data from the file as it is in row format. It is a basic data source for all other Input stream classes.
DataInputStreamRead data in primitive data (int, long, ..) size format. It is used to add the capability to another input stream and output stream to write data as primitive types.
ObjectInputStreamIt read data in Object format. It is used to perform Object Serialization.
BufferedInputStreamRead data as buffers. It is used to improve the performance of other streams.
SequenceInputStreamIt is used to read data from multiple InputStreams sequentially.

The DataInputStream and ObjectInputStream class are implemented from DataInput and ObjectInput interface respectively. And the ObjectInput interface is a sub-interface of DataInput interface.

ObjectInput => ObjectInputStream
DataInput => DataInputStream

// DataInput.java
package java.io;
interface DataInput {
……
}

// ObjectInput.java
package java.io;
public interface ObjectInput extends
DataInput, AutoCloseable {
……
}

None of these subclasses having a 0 parameter constructor because it is a stream-based connection where one end is a Java application. Therefore second end information must be passed as input to the constructor.

Among all 9 Input Stream subclasses, only FileInputStream can directly connect to the file. The remaining all other 8 classes need the help from FileInputStream class to connect to the file. Therefore, the basic source of connecting to file and reading data is FileInputStream class and the remaining classes are depending on it.

InputStream vs OutputStream

InputStreamOutputStream
It is a subclass of the Closeable interface.It is a subclass of the Closeable and Flushable interface.
It is used for reading binary data from the file.It is used for writing binary data into the file.
InputStream class is a superclass of all input stream type classes for reading binary data from the file.OutputStream class is a superclass of all output stream type classes for writing binary data into the file.
It contains total 9 sub classes. Important sub classes are FileInputStream, DataInputStream and ObjectInputStream.It contains total 8 sub classes. Important sub classes are FileOutputStream, DataOutputStream, PrintStream and ObjectOutputStream.
FileInputStream is the basic source for connecting to the file to read data. The remaining all subclasses are dependent on FileInputStream. Without FileInputStream we can’t read data from the file.FileOutputStream is the basic source for connecting to the file to write data. The remaining sub classes are dependent on FileOutputStream. Without FileOutputStream we can’t write data to the file.

Similarity,

  • Both are subclasses of java.lang.Object class and implements the Closeable interface.
  • Both belong to the java.io package.
  • They are abstract classes.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *