PipedInputStream class

 

 

 ■  PipedInputStream provides all the methods of InputStream.See  InputStream class  for descriptions of ByteArrayInputStreamís 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.

 

 

 ■  Multiple code examples to illustrate the use of PipedInputStream methods are not provided here because they are directly similar to those provided for PipedReader in the the pipes explanation.  See pipes  and simply substitute byte streams for the character streams demonstrated there.

 

This snippet simply writes the ASCII numeric values for the A-E letters as 6 separate bytes into a pipe using a PipedOutputStream stream, connects to it, and then immediately reads the letters back individually from the other end of the pipe using PipedInputStream.It also prints them. Normally all this would be done across threads.

 

import java.io.*;

 

PipedInputStream pis = new PipedInputStream( );

byte[ ] ba = {65,66,67,68,69,70};

try {

††† PipedOutputStream pos = new PipedOutputStream( pis );††††††††††††††† // connection created here, in the constructor

††† for ( int x = 0;x < ba.length;x++ ) {

†††††††††† pos.write( ba, x, 1 );

†††† }

†††† for ( int x = 0;x < 26;x++ ) {

†††††††††††† System.out.print( (char) pis.read( ) );

††††† }

} catch (IOException e) { }