digitization, transformation, binary

Class InputStream in Java

Spread the love

Class InputStream in Java

InputStream is an abstract class that is the superclass of an input stream of bytes.It defines all the methods that all input stream classes provide.If not all at least the basic methods are provided. Applications that need to define a subclass of the InputStream must always provide a method that returns the next byte of input.

InputStream is a flow of data from an input source mainly from file to the application under execution or reading bytes of data from some kind of data source.

InputStream constructor takes a string representation of a file name or a file descriptor object to create the stream.

We need to connect InputStream to a FilterInputStream object to provide a useful interface.

InputStream is designed so that read(byte[]) and read(byte[], int, int) both call read(). Thus, a subclass can simply override read(), and all the read methods will work. However, for efficiency sake, read(byte[], int, int) should also be overridden with a method that can read a block of data more efficiently than reading each byte separately.

InputStream also defines a mechanism for marking a position in the stream and returning to it later, via the mark() and reset() methods. Another method, markSupported(), indicates whether or not this mark-and-reset functionality is available in a particular subclass.
The read() method reads a single byte or an array of bytes and returns the byte read or the number of bytes reads. if it reaches EOF or end of File it returns -1.

skip() method skips a specified number of bytes of input.

available() returns the number of bytes that can be read without blocking.

close() method closes the input stream and it also frees up any resource that is being used.

markSupported() returns true if the given InputStream supports mark() and reset() method.

READ  Hashmap Concept Simplified in Java

mark() method marks the current position in the stream.

reset() method returns that position. (It is applicable only if no more than specified number of bytes have been read between mark() and reset()).

Input Stream is divided into the following classes and Subclasses:

 

ClassSubclassFurther Subclass
InputStreamSequenceInputStream
PipedInputStream
FilterInputStreamDataInputStream
PushBackInputStream
BufferedInputStream
LineNumberInputStream
StringBufferInputStream
ByteArrayInputStream
FileInputStream

 

The structure of the class InputStream  is given as:

public abstract class java.io.InputStream extends java.lang.Object{
//constructor
public InputStream();
//methods
public abstract int available() throws IOException;
public void close() throws IOException;
public synchronized void mark(int readLimit);
public boolean markSupported();
public abstract int read()throws IOException;
public int read(byte b[]);throws IOException;
public int read(byte b[],int offSet,int length)throws IOException;
public synchronized void reset()throws IOException;
public long skip(long l)throws IOException;
}
 

In addition, all methods may throw an IOException if an IO error occurs apart from their regular Exceptions.

The details of the structure are given as:

public InputStream();

public InputStream() is the default constructor,this is always called by the subclasses.

public abstract int available();

public abstract int available() method returns the number of bytes that can be read without having to wait for more data to become available, or in other words, blocking.

A subclass of InputStream must implement this method.

This method returns the number of bytes of input currently available for reading.(The number of bytes that can be read without blocking).

public void close();

public void close() method closes the input source;further read attempt on a closed input will generate an IOException.It closes the input stream and releases any resources associated with it.
The implementation of the close() method in InputStream does nothing; a subclass should
override this method to handle cleanup for the stream.

public synchronized void mark(int readlimit);

public synchronized void mark(int readlimit) method marks at the current point in the input stream that will remain valid until read limit bytes are read.

This method also tells this InputStream object to remember its current position, so that the position can be restored by a call to the reset() method.

READ  Class FilterOutputStream in Java

The InputStream can read readlimit bytes beyond the marked position before the mark becomes invalid.

The implementation of the mark() method in InputStream does nothing; a subclass must
override the method to provide the mark-and-reset functionality.
Parameter
readlimit-The maximum number of bytes that can be read before the saved position can become
invalid.

public boolean markSupported();

public boolean markSupported() method returns a boolean value that indicates whether or not this object supports mark-and-reset functionality.

The markSupported() method in InputStream always returns false. A subclass that
implements the mark-and-reset functionality should override the method to return true.

This method returns true if mark/reset supported on this stream.(true if this input stream supports mark() and reset(); false otherwise.)

public abstract int read();

public abstract int read() method reads the next byte of input. The byte is returned as an integer in the range 0 to 255.

The method blocks until the byte is read, the end of the stream is encountered, or an exception is thrown.

A subclass of InputStream must implement this method.

This method returns an integer representation of the next available byte of input.(The next byte of data or -1 if the end of the stream is encountered.)

public int read(byte[] b);

public int read(byte[] b) method attempts to read up to b.length bytes into b and returns the actual number of the bytes that were successfully read.

This method reads bytes of input to fill the given array by calling read(b, 0, b.length). The
method blocks until some data is available.

A subclass does not usually need to override this method as it ca

This method returns the actual number of bytes read or -1 if the end of the stream is encountered immediately.
Parameter
b – An array of bytes to be filled from the stream.

public int read(byte[] b, int off, int len);

public int read(byte[] b, int off, int len) method attempts to read up to length bytes into b starting at b[offSet] returning the number of bytes successfully read.

READ  Popular Abstract Interfaces in Java

This method reads up to len bytes of input into the given array starting at index off.

The method blocks until some data is available.

The implementation of this method in InputStream uses read() repeatedly to fill the array.
Although it is not strictly necessary, a subclass should override this method to read a block of data more efficiently.

This method returns the actual number of bytes read or -1 if the end of the stream is encountered immediately.

Parameter
b -An array of bytes to be filled from the stream.
off -An offset into the array.
len -The number of bytes to read.

public synchronized void reset();

public synchronized void reset() method returns the input pointer to the previously set mark.

So this method restores the position of the stream to the position that was saved by a previous call to mark().

The implementation of the reset() method in InputStream throws an exception to indicate
that mark-and-reset functionality is not supported by default. A subclass must override the method to provide the functionality.

public long skip(long l);

public long skip(long l) method skips over l bytes of input and returns the number of bytes skipped.

In other words, it moves the position of the stream forward by l bytes.

The implementation of the skip() method in InputStream simply calls read(b) where b is
a byte array n bytes long. A subclass may want to override this method to implement a more efficient skipping algorithm.

This method returns the actual number of bytes skipped.
Parameter
l – The number of bytes to skip.
Apart from these InputStream class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)


Spread the love

Leave a Comment

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