BufferedInputStream class



 Adds no new methods to those of  InputStream See  InputStream class  for its inherited methods.  They are:


available( )                   Tells how many bytes can be gotten without blocking.

close( )                         Close the stream.

mark( int readAheadLimit )  Mark the present position in the stream.

markSupported( )         Tell whether this stream supports the mark(...) operation.

read( )                          Read a single byte, returning it as the low end of an int.

read( byte[ ] b )             Read bytes into a byte array.

read( byte[ ] b, int off, int len)  Read bytes into a portion of a byte array.

ready( )                         Tell whether this stream is ready to be read.

reset( )                          Reset the stream to the beginning.

skip( long n )                Skip characters.


  BufferedInputStream is just a (faster) byte InputStream class which buffers the incoming bytes, usually from a FileInputStream, and enables use of .mark(limit) and .reset( ).


  Extends FilterInputStream


  Its constructors will take any InputStream object. Usually constructed from a FileInputStream.


   This code reads and prints bytes from a text file:


import java.io.*;


FileInputStream fis = null;

try  { fis = new FileInputStream(new File("test.txt")); }

catch (IOException ex) { }

BufferedInputStream bis = new BufferedInputStream(fis);

try  {

                int c; 

                while ((c = bis.read()) != -1) { System.out.print(String.valueOf( (char) c )); }  }   // there’s no toString( ) for byte

catch (IOException ex) { }

finally { try { if (bis != null) bis.close( ); }

              catch (IOException ex) { }  }