Pipes in IO provides a link between two threads running in JVM at the same time. So, Pipes are used both as source or destination.
- PipedInputStream is also piped with PipedOutputStream. So, data can be written using PipedOutputStream and can be written using PipedInputStream.But, using both threads at the same time will create a deadlock for the threads.
- A pipe is said to be broken if a thread that was providing data bytes to the connected piped output stream is no longer alive.
public class PipedInputStream extends InputStream
- PipedInputStream() : creates a PipedInputStream, that it is not connected.
- PipedInputStream(int pSize) : creates a PipedInputStream, that it is not connected with specified pipe size.
- PipedInputStream(PipedOutputStream outStream) : creates a PipedInputStream, that it is connected to PipedOutputStream – ‘outStream’.
- PipedInputStream(PipedOutputStream outStream, int pSize) : creates a Piped Input Stream that is connected to Piped Output Stream with the specified pipe size.
- int read(): 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.
// Java program illustrating the working of read() method
PipedInputStream geek_input =
PipedOutputStream geek_output =
// Use of connect() : connecting geek_input with geek_output
// Use of read() method :
"using read() : "
"using read() : "
"using read() : "
using read() : G using read() : E using read() : K
- read(byte buffer, int offset, int maxlen) : java.io.PipedInputStream.read(byte buffer, int offset, int maxlen) reads upto maxlen bytes of the data from Piped Input Stream to the array of buffers. The method blocks if end of Stream is reached or exception is thrown.
public int read(byte buffer, int offset, int maxlen) Parameters : buffer : the destination buffer into which the data is to be read offset : starting in the destination array - 'buffer'. maxlen : maximum length of array to be read Return : next 'maxlen' bytes of the data as an integer value return -1 is end of stream is reached Exception : -> IOException : if in case IO error occurs. -> NullPointerException : if buffer is null. -> IndexOutOfBoundsException : if offset is -ve or maxlen is -ve or maxlen > buffer.length - offset.
- receive(int byte) : java.io.PipedInputStream.receive(int byte) receives byte of the data. If no input is available, then the method blocks.
protected void receive(int byte) Parameters : byte : the bytes of the data received Return : void Exception : -> IOException : if in case IO error occurs or pipe is broken.
- close() : java.io.PipedInputStream.close() closes the Piped Input Stream and releases the allocated resources.
public void close() Parameters : -------------- Return : void Exception : -> IOException : if in case IO error occurs.
- connect(PipedOutputStream source) : java.io.PipedInputStream.connect(PipedOutputStream source) connects the Piped Input Stream to the ‘source’ Piped Output Stream and in case ‘source’ is pipes with some other stream, IO exception is thrown
public void connect(PipedOutputStream source) Parameters : source : the Piped Output Stream to be connected to Return : void Exception : -> IOException : if in case IO error occurs.
- available() : java.io.PipedInputStream.available() returns no. of bytes that can be read from Input Stream without actually being blocked.
public int available() Parameters : ------------- Return : no. of bytes that can be read from Input Stream without actually being blocked. 0, if the stream is already closed but by invoking close() method Exception : -> IOException : if in case IO error occurs.
Java program explaining the working of PipedInputStream class methods :
Use of available() : 5 Using read(buffer, offset, maxlen) : GEEKS Closing the stream
Next Article: Java.io.PipedOutputStream class in Java
This article is contributed by Mohit Gupta 🙂. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Java.lang.Class class in Java | Set 1
- Java.lang.Class class in Java | Set 2
- Using predefined class name as Class or Variable name in Java
- Java.util.TimeZone Class (Set-2) | Example On TimeZone Class
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Difference between Abstract Class and Concrete Class in Java
- In Java, Can we call the main() method of a class from another class?
- Does JVM create object of Main class (the class with main())?
- Inner Class And Anonymous Inner Class that Implements Runnable | Concurrent Programming Approach 3
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.Lang.Float class in Java
- Java.io.BufferedInputStream class in Java