In this example we will discuss about the Java PipedInputStream.
Ads
TutorialsIn this example we will discuss about the Java PipedInputStream.
PipedInputStream is a class in java.io package used for providing the data bytes written into the piped output stream for this the piped input stream is required to connect with the piped output stream. In this process two threads are involved one is read data from the object of PipedInputStream and the other writes the corresponding data to the PipedOutputStream object. PipedInputStream uses a buffer to decouple the read operation from write operation. If the pipe (which provides the data bytes to the connected piped output stream) is not longer alive then the pipe is called broken.
Various of constructors are provided to create the PipedInputStream object. Commonly used constructors are as follows :
PipedInputStream() | A default constructor used for creating the object of PipedInputStream. |
PipedInputStream(int pipeSize) | A single parameterized constructor used for creating the object of PipedInputStream with the specified pipe size. |
PipedInputStream(PipedOutputStream src) | A single parameterized constructor used for creating the object of PipedInputStream which contains the reference of PipedOutputStream source. |
PipedInputStream(PipedOutputStream src, int pipeSize) | A two parameterized constructor used for creating the object of PipedInputStream which contains the reference of PipedOutputStream source with the specified pipe size. |
Methods of PipedInputStream
Commonly used methods of PipedInputStream class are as follows :
Example :
An example is being given here which will demonstrate you how to use the PipedInputStream in Java. In this example you will see how the piped output stream is required to connect with the piped input stream. In this example you will see when a output stream is written into the pipe then the piped input stream is read that output stream. In this example I have used the write(int) method of PipedOutputStream class which writes the integer value but when try to read that written output stream converted this integer value to the character hence they will print the corresponding characters.
Source Code
import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.IOException; public class PipedInputStreamExample { public static void main(String[] args) { PipedOutputStream pos = new PipedOutputStream(); PipedInputStream pis = new PipedInputStream(); try { pis.connect(pos); // Write to the PipedOutputStream pos.write(82); pos.write(79); pos.write(83); pos.write(69); pos.write(73); pos.write(78); pos.write(68); pos.write(73); pos.write(65); // read the PipedInputStream int c; while((c = pis.read() ) != -1) { System.out.print((char) c); } } catch (IOException ioe) { System.out.println(ioe); } }// close main }// close class
Output :
When you will execute the above example you will get the output as follows :
Posted on: December 11, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles
Advertisements
Ads
Ads
Discuss: Java PipedInputStream
Post your Comment