InputStream class

 

The following classes discussed here can use InputStream's methods: ByteArrayInputStream class,  BufferedInputStream class, 

DataInputStream class,  FileInputStream class,  ObjectInputStream class,  PipedInputStream class,  PushBackInputStream class

 

 

  InputStream is the abstract class which defines the nine basic methods for all the byte input stream classes named above.  All those classes are subclasses of  InputStream.

 

  Reader methods will read character files in formats such as UTF-16 (returning two meaningful bytes per character) and UTF-8 (returning one meaningful byte per character).  They can also read ASCII files and other 8-bit local machine encodings, because of the fact that these 8-bit encodings "fit" under UTF-8. 

 

  InputStream's read(...) methods return the byte(s) in the right-hand or lower end of an int. Minus one is returned in that int to signify end-of-stream.

 

  The nine InputStream methods are described below.  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.

 

 

  All of these methods except markSupported(...) can throw an IOException, so their use requires try-catch blocks. 

 

 

int  .available( ) method

 

    Retuns the number of bytes still available to be read. This snippet reads a file one byte at a time from a Windows c:\\test.txt file:

 

import java.io.*;

 

try {

    DataInputStream dis = new DataInputStream( new BufferedInputStream( new FileInputStream("c:\\test.txt")));

    while(dis.available( ) != 0)

      System.out.print( (char) dis.readByte( ) );

}

catch (IOException io) { }

 

 

void  .close( )  method

 

     Operates just like Reader’s .close( ) method.

 

 

 

void  .mark( readaheadlimit ) ,    

boolean .markSupported( )  and

void .reset( )  

 

     These operate just like Reader’s marking methods.

 

 

int  .read( )  method

 

     Although this method is actually abstract in InputStream, all the other InputStream classes implement it just this way.

 

     It returns the next single available byte of data in an int, or -1 if end of stream is reached.  You must cast the returned int to get a printable char.  i.e.

 

                        import java.io.*;

 

//  assume pre-existing file test.txt contains "Mary had a little lamb."

try {

      DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream("c:\\test.txt")));

      System.out.print( (char) in.read( ) );    // prints the M in Mary...

      System.out.print( (char) in.read( ) );    // prints the a in Mary...

}

catch (IOException io) { }

 

 

int .read( byte[ ] )  method

 

     Reads a file into an array, up to the end of the array’s length. So the length of the array determines how many bytes get read from the file.  If the array is declared to be longer than the length of the file, you get the whole file.  If the array is declared to be shorter than the length of the file, you get just that many bytes until the array is full.

 

                        import java.io.*;

 

//  assume pre-existing file test.txt contains "Mary had a little lamb."  23 chars plus 2 EOL chars

try {

        byte[ ] ba = new byte[ 30 ];

        DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream("c:\\test.txt")));

        System.out.print( in.read( ba ) );        // returns the bytes-read count of 25

        System.out.println( " ");

        for ( int x = 0; x < ba.length;  x++ ) System.out.print( (char) ba[ x ] );  // prints the file now in the array

        }

catch (IOException io) { }

 

 

int  .read( byte[ ],  offset , len )  method

 

     Reads len bytes from a file into an array, starting the reading at the beginning of the file and putting them into the array starting at offset.

 

The snippet replaces the array’s word “lamb” with the word “goat,” which it obtains from a file, and it prints:

 

Mary had a little lamb.

Mary had a little goat.

 

                        import java.io.*;

 

//  assume pre-existing file goat.txt contains "goat"  4 chars plus 2 EOL chars

//  assume pre-existing file test.txt contains "Mary had a little lamb."  23 chars plus 2 EOL chars

try {

        byte[ ] ba = new byte[ 25 ];

        DataInputStream dis = new DataInputStream( new BufferedInputStream( new FileInputStream("c:\\test.txt")));

        dis.read( ba );

        for ( int x = 0; x < ba.length;  x++ ) System.out.print( (char) ba[ x ] );  // prints the file now in the array

 

        DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream("c:\\goat.txt")));

        in.read( ba, 18, 4 );                // reads "goat" into position 18, returning a 4 count

 

        for ( int x = 0; x < ba.length;  x++ ) System.out.print( (char) ba[ x ] );  // prints the file now in the array

        }

        catch (IOException io) { }

 

 

long  .skip( count )  method

 

     See the description of  skip(...)  under Reader class.  It has the same syntax and acts the same.