PipedInputStream read Example
PipedInputStream class read example. This example shows you how to use read method.
PipedInputStream class read example.public int read() throws IOException Reads the next byte of data from this piped input stream. The value byte is returned as an int in the range 0 to 255. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.
Here is the code
/**
* @ # Read.java
* A class repersenting use to read method
* of PipedInputStream class in java.io package
* version 03 June 2008
* author Rose India
*/
import java.io.*;
public class Read {
public static void main(String args[]) throws Exception {
byte[] b = {1, 2, 3, 4, 5};
PipedOutputStream poStream = new PipedOutputStream();
PipedInputStream piStream = new PipedInputStream();
//piped input stream connect to the piped output stream
piStream.connect(poStream);
//Writes specified byte array.
poStream.write(b, 0, 5);
//Reads the next byte of data from this piped input stream.
for (int i = 0; i < b.length; i++) {
System.out.println(piStream.read());
}
// Closes piped input stream
poStream.close();
// Closes piped output stream
piStream.close();
}
} |
|