Input Streams - PART 2
date
Jan 21, 2024
slug
java-input-stream
status
Published
tags
IO
summary
type
Post
InputStream
In Java, the foundational class for reading data from a source is the input stream. True to its name, "InputStream" encapsulates the flow of input data from a specific source to the Java program. This class serves as a fundamental component in handling various input operations within Java's Input/Output system.
It has total of 9 methods as shown in the figure(). One of which is read() which is abstract method.

Methods
public abstract int read() throws IOException;

This method of the InputStream class reads a next byte from the stream and it return value is in the range of [0,255] and returns value will be -1 end of the stream is detected or an exception is thrown.
NOTE: This method blocks the current thread until next byte is available to read, or an exception is thrown. Most I/O operations can be slow so if program is doing something else that is of importance, it is good idea to do I/O operations in it’s own thread.
Example:
public int read(byte b[]) throws IOException
Reading a single byte at a time is inefficient due to this reason, InputStream provides two overloaded read() methods to read data in bulk. One of which is read(byte b[]). This methods ATTEMPTS to fill the array input b.
let’s say we declare variable b as
Read method will be attempt to read 1024 bytes of data from the stream.
- If there is no data available this method will return 0 and write nothing.
- If there is data less 1024 bytes. It will write upto available number of bytes into array b and return number of bytes written.
- if there is data more 1024 bytes. It will write upto 1024 bytes.
- If it detects end of the stream it return -1.
public int read(byte b[], int off, int len) throws IOException
This method “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. The number of bytes actually read is returned as an integer.”
This method takes a byte array
b
, an offset off
, and a length len
as parameters. The code begins with input validations, checking for a null byte array and ensuring that the offset and length parameters fall within acceptable ranges. In the case of a null array or invalid parameters, it throws a NullPointerException
or an IndexOutOfBoundsException
accordingly. A special case is handled where, if the specified length is zero, the method returns 0, indicating that no bytes were read. The core functionality involves reading bytes from the source, populating the byte array, and returning the count of bytes read. The return value is the count of bytes read (i
), returning -1 if the end of the input is reached before reading any bytes.- close() method
Close method is part of Closeable Interface implemented by InputStream.
This method closes resources such as file handles or ports by inputstream. It is necessary to close the inputstream after it’s use to avoid memory leakes. So it’s common practice to use try - resouces with InputStream.
For example:
Reference