Java - PipedOutputStream connect(PipedInputStream snk) method



Description

The Java PipedOutputStream connect(PipedInputStream snk) method connects this piped output stream to a receiver. If this object is already connected to some other piped input stream, an IOException is thrown.

Declaration

Following is the declaration for java.io.PipedOutputStream.connect(PipedInputStream snk) method.

public void connect(PipedInputStream snk)

Parameters

snk − The piped input stream to connect to.

Return Value

This method does not return a value.

Exception

IOException − If an I/O error occurs.

Example - Usage of PipedOutputStream connect(PipedInputStream snk) method

The following example shows the usage of PipedOutputStream connect(PipedInputStream snk) method.

PipedOutputStreamDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class PipedOutputStreamDemo extends PipedInputStream {
   public static void main(String[] args) {

   
      // create a new Piped input and Output Stream
      PipedOutputStream out = new PipedOutputStream();
      PipedOutputStreamDemo in = new PipedOutputStreamDemo();

      try {
         // connect input and output
         out.connect(in);

         // write something 
         out.write(70);
         out.write(71);

         // print what we wrote
         for (int i = 0; i < 2; i++) {
            System.out.println("" + (char) in.read());
         }
   
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

F
G

Example - Manually connecting PipedOutputStream to PipedInputStream using connect()

The following example shows the usage of PipedOutputStream connect(PipedInputStream snk) method.

PipedOutputStreamDemo.java

package com.tutorialspoint;

import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.IOException;

public class PipedOutputStreamDemo {
   public static void main(String[] args) {
      try {
         PipedInputStream input = new PipedInputStream();
         PipedOutputStream output = new PipedOutputStream();

         // Connect the output stream to the input stream
         output.connect(input);

         // Write data to the pipe
         output.write("Connected manually!".getBytes());
         output.close(); // Always close when done

         // Read from the pipe
         int data;
         while ((data = input.read()) != -1) {
            System.out.print((char) data);
         }

         input.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Connected manually!

Explanation

  • output.connect(input) explicitly connects the two streams.

  • After connection, output.write() sends data directly into input.read().

  • This allows two threads or parts of code to communicate via a memory pipe.

Example - Connecting streams in separate threads (producer-consumer style)

The following example shows the usage of PipedOutputStream connect(PipedInputStream snk) method.

PipedOutputStreamDemo.java

package com.tutorialspoint;

import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.IOException;

public class PipedOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      PipedInputStream input = new PipedInputStream();
      PipedOutputStream output = new PipedOutputStream();

      output.connect(input); // Manual connection

      // Producer thread
      Thread producer = new Thread(() -> {
         try {
            output.write("Hello from producer!".getBytes());
            output.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      });

      // Consumer thread
      Thread consumer = new Thread(() -> {
         try {
            int ch;
            while ((ch = input.read()) != -1) {
               System.out.print((char) ch);
            }
            input.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      });

      producer.start();
      consumer.start();
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Hello from producer!

Explanation

  • This example shows a common producer-consumer pattern.

  • connect() allows the PipedOutputStream and PipedInputStream to communicate across threads.

  • Writing and reading happens asynchronously between threads using the in-memory pipe.

java_io_pipedoutputstream.htm
Advertisements