The read method we used in the previous post is in fact defined in the InputStream class. The FileInputStream class that extends from the InputStream class inherits this method. The InputStream class defines several other important methods, which are inherited and implemented in its subclasses. We will now discuss some of the most frequently used methods of the  InputStream class.

read Method           

In the previous example, we used the read method, which did not take any arguments. This method reads one byte at a time. Two more variations of the read method are available in the InputStream class. These methods take one or more arguments.

The following read method takes a byte array as an argument:

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

·   The method reads the number of bytes equal to the length of the byte array. The data read is stored in the byte array.

·   It returns the number of bytes read, which could be less if the end-of-file is reached earlier. The method throws two different types of exceptions.

·   If the first byte cannot be read for any reason other than the end-of-file condition, it throws an IOException. In particular, executing this call on an already closed input stream causes this exception to be thrown.

·   If the argument b is null, the method throws a NullPointerException. 

The following read method takes three arguments:

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

The first parameter specifies the byte array in which the data will be stored.

The second argument, off, specifies the offset in the byte array where the first byte read will be stored.

The third parameter, len, specifies the number of bytes to read.

The method returns the number of bytes actually read. The method throws two types of exceptions, like in the earlier case, for the same reasons.

available Method
public int available() throws IOException

·   This method returns an estimate of the number of bytes that can be read from this input stream without blocking, or it returns 0 when it returns the end of the input stream.

·   The phrase “without blocking” is important in the definition of available method. A stream may flow continuously and keep on acquiring newer data as time advances. When we say that the method returns an estimate of the count without blocking, this means that the receiver can receive the stated number of bytes in a single subsequent read call.

·   The java.nio package, introduced since J2SE 1.4.2, solves the issues of blocking calls by providing buffers and channels.

skip Method

Our next method, skip, has the following signature:

public long skip(long n) throws IOException

·   The skip method is useful if we want to skip and discard some bytes of data from the input stream.

·   The number of bytes to discard is specified by the parameter n to the skip method.

·  As we’ll recall from earlier, streams are sequential. Therefore, by calling the skip method, we can simply jump ahead in the buffer, but we cannot come back to read whatever we skipped.

close Method
The close method closes the input stream:
void close()

Note that the garbage collector does not close an open stream on its own. Therefore, to reclaim the resources, we must always explicitly close the stream we have opened previously.

Other Methods

The InputStream class provides a few additional methods, such as:

·    mark

·    reset

·    markSupported

We encourage you to open the javadocs to learn the use of these methods

  Modified On Dec-18-2017 01:41:47 AM

Leave Comment