Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java - PipedInputStream connect(PipedOutputStream src) method



Description

The Java PipedInputStream connect(PipedOutputStream src) method causes this piped input stream to be connected to the piped output stream src. If this object is already connected to some other piped output stream, an IOException is thrown.

Declaration

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

public void connect(PipedOutputStream src)

Parameters

src − The piped output stream to connect to.

Return Value

This method does not return a value.

Exception

IOException − If an I/O error occurs.

Example - Usage of PipedInputStream connect(PipedOutputStream src) method

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

PipedInputStreamDemo.java

package com.tutorialspoint;

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

public class PipedInputStreamDemo {
   public static void main(String[] args) {
   
      // create a new Piped input and Output Stream
      PipedOutputStream out = new PipedOutputStream();
      PipedInputStream in = new PipedInputStream();

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

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

         // read 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 - Connecting PipedInputStream and PipedOutputStream manually

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

PipedInputStreamDemo.java

package com.tutorialspoint;

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

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

         // Manually connect input to output
         input.connect(output);

         // Write data from output
         output.write("Hello via pipe".getBytes());

         // Read the data using input
         byte[] buffer = new byte[input.available()];
         input.read(buffer);

         System.out.println("Received: " + new String(buffer));

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

Output

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

Received: Hello via pipe

Explanation

  • connect(PipedOutputStream pos) links the PipedInputStream to the specified PipedOutputStream.

  • This is an alternative to the constructor-based connection (new PipedInputStream(output)).

  • After connecting, data written to the output stream can be read from the input stream.

Example - Connecting in a multi-threaded producer-consumer scenario

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

PipedInputStreamDemo.java

package com.tutorialspoint;

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

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

      // Connect input and output using connect() method
      input.connect(output);

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

      Thread consumer = new Thread(() -> {
         try {
            byte[] buffer = new byte[1024];
            int bytesRead = input.read(buffer);
            System.out.println("Consumer received: " + new String(buffer, 0, bytesRead));
            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−

Consumer received: Data from producer thread

Explanation

  • A producer thread writes data to a PipedOutputStream.

  • A consumer thread reads from the connected PipedInputStream.

  • The connect() method ensures the two streams are linked before starting the threads.

  • This pattern is commonly used for inter-thread communication in Java.

java_io_pipedinputstream.htm
Advertisements