All About Class InputStream in Java

0
163
Spread the love
  • 29
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    138
    Shares

InputStream in Java
Class InputStream in Java

InputStream in Java

Class InputStream in Java

InputStream is an abstract class that is the super class 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 constractor 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.

 

Input Stream is divided into following classes and Sub classes:

 

Class Sub class Further Subclass
InputStream SequenceInputStream
PipedInputStream
FilterInputStream DataInputStream
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();
//the default constructor,this is always called by the subclasses.
//methods
public int available()throws IOException;//returns the number of bytes
// of input currently available for reading.
public void close() throws IOException;//closes the input source;further
// read attempt on a closed input will generate an IOException.
public synchronized void mark(int readLimit);//places mark at the current
//point in the input stream that will remain valid until read limit bytes 
//are read.
public boolean markSupported();//returns true if mark/reset supported on 
//this stream.
public abstract int read()throws IOException;//returns an integer 
//representation of the next available byte of input.
public int read(byte b[]);throws IOException//attempts to read up to b.length
// bytes into b and returns the actual number of the bytes that were successfully
// read.
public int read(byte b[],int offSet,int length)throws IOException;//attempts to 
//read up to length bytes into b starting at b[offSet] returning the number of
// bytes successfully read. 
public synchronized void reset()throws IOException;//returns the input pointer
// to the previously set mark.
public long skip(long l)throws IOException;//skips over l bytes of input and 
//returning the number of bytes skipped.
}
 

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

The read() method reads a single byte or an array of bytes and returns the byte read or the number of bytes read. 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 are being used.
markSupported() returns true if the given InputStream supports mark() and reset() method.mark() method marks the current position in the stream and reset() method returns that position.(It is applicable only if no more than specified number of bytes have been read between mark() and reset()).

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 29
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    138
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here