Skip to content
Related Articles

Related Articles

Java.io.PipedOutputStream class in Java

Improve Article
Save Article
  • Last Updated : 20 Nov, 2021
Improve Article
Save Article

Java.io.PipedInputStream class in Java

io.PipedOutputStream class in Java

Pipes in IO provide 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.
  • PipedOutputStream is sending end of the pipe. Data is written to the PipedOutputStream. The pipe is said to be broken if the PipedInputStream, that was reading the data is no more.

Declaration:  

public class PipedOutputStream
  extends OutputStream

Constructor:  

  • PipedOutputStream() : creates a PipedOutputStream, that it is not connected.
  • PipedOutputStream(PipedOutputStream inStream) : creates a PipedOutputStream, that it 
    is connected to PipedInputStream – ‘inStream’.

Methods: 

  • write() : java.io.PipedOutputStream.write(int byte) writes a specified byte to the Piped Output Stream. 
    Syntax : 
public void write(int byte)
Parameters : 
byte : byte to be written
Return :                                               
void
Exception :
-> IOException : if in case IO error occurs.
  • write(byte[] buffer, int offset, int maxlen) : java.io.PipedOutputStream.write(byte[] buffer, int offset, int maxlen) writes maxlen bytes of the data from buffer to the Piped Output Stream. The method blocks if no bytes are written to the Stream. 
    Syntax : 
public void write(byte[] buffer, int offset, int maxlen)
Parameters : 
buffer : data of the buffer
offset : starting in the destination array - 'buffer'.
maxlen : maximum length of array to be read
Return :                                               
void
Exception :
-> IOException : if in case IO error occurs.

Java




// Java program illustrating the working of PipedInputStream
// write(byte[] buffer, int offset, int maxlen)
 
import java.io.*;
public class NewClass
{
    public static void main(String[] args) throws IOException
    {
        PipedInputStream geek_input = new PipedInputStream();
        PipedOutputStream geek_output = new PipedOutputStream();
 
        // Use of connect() : connecting geek_input with geek_output
        geek_input.connect(geek_output);
 
        byte[] buffer = {'J', 'A', 'V', 'A'};
 
        // Use of write(byte[] buffer, int offset, int maxlen)
        geek_output.write(buffer, 0, 4);
        int a = 5;
        System.out.print("Use of write(buffer, offset, maxlen) : ");
        while(a>0)
        {
            System.out.print(" " + (char) geek_input.read());
        }
    }
}

Output: 

Use of write(buffer, offset, maxlen) :  J A V A
  • close() : java.io.PipedOutputStream.close() closes the Piped Output Stream and releases the allocated resources. 
    Syntax : 
public void close()
Parameters : 
--------------
Return :                                               
void
Exception :
-> IOException : if in case IO error occurs.
  • connect(PipedInputStream destination) : java.io.PipedOutputStream.connect(PipedInputStream destination connects the Piped Output Stream to the ‘destination’ Piped Input Stream and in case ‘destination’ is pipes with some other stream, IO exception is thrown 
    Syntax : 
public void connect(PipedInputStream destination)
Parameters : 
destination : the Piped Input Stream to be connected to
Return :                                               
void
Exception :
-> IOException : if in case IO error occurs.
  • flush() : java.io.PipedOutputStream.flush() flushes the Output Stream. 
    Syntax : 
public void flush()
Parameters : 
------------
Return :                                               
void
Exception :
-> IOException : if in case IO error occurs.

Java code illustrating the working of PipedOutputStream class methods : 

Java




// Java program illustrating the working of PipedInputStream
// write(), write(byte[] buffer, int offset, int maxlen),
// close(), flush(), connect()
 
import java.io.*;
public class NewClass
{
    public static void main(String[] args) throws IOException
    {
        PipedInputStream geek_input = new PipedInputStream();
        PipedOutputStream geek_output = new PipedOutputStream();
        try
        {
            // Use of connect() : connecting geek_input with geek_output
            geek_input.connect(geek_output);
 
            // Use of write(int byte) :
            geek_output.write(71);
            geek_output.write(69);
            geek_output.write(69);
            geek_output.write(75);
            geek_output.write(83);
 
            // Use of flush() method :
            geek_output.flush();
            System.out.println("Use of flush() method : ");
 
            int i = 5;
            while(i > 0)
            {
                System.out.print(" " + (char) geek_input.read());
                i--;
            }
 
            // USe of close() method :
            System.out.println("\nClosing the Output stream");
            geek_output.close();
 
        }
        catch (IOException except)
        {
            except.printStackTrace();
        }
    }
}

Output: 

Use of flush() method : 
 G E E K S
Closing the Output stream

This article is contributed by Mohit Gupta 🙂. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. 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.
 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!