The PipedInputStream
and PipedOutputStream
classes are part of the Java I/O library and are used for inter-thread communication.
The PipedOutputStream
class is used to write data to a PipedInputStream
instance, while the PipedInputStream
class is used to read the data from the connected PipedOutputStream
.
To use these classes, you need to create an instance of both the PipedInputStream
and PipedOutputStream
classes, and then connect them using the connect()
method of PipedOutputStream
.
Here’s an example:
PipedInputStream pis = new PipedInputStream(); PipedOutputStream pos = new PipedOutputStream(); // Connect the streams pos.connect(pis);
Once the streams are connected, you can write data to the PipedOutputStream
using its write()
method and read data from the PipedInputStream
using its read()
method.
Here’s an example of how to write data to a PipedOutputStream
and read it from a PipedInputStream
:
// Write data to the PipedOutputStream pos.write("Hello, World!".getBytes()); // Read data from the PipedInputStream byte[] buffer = new byte[1024]; int bytesRead = pis.read(buffer); String data = new String(buffer, 0, bytesRead); System.out.println(data);
In this example, we write the string “Hello, World!” to the PipedOutputStream
and then read it from the PipedInputStream
. The read()
method blocks until data is available, so this example will print “Hello, World!” to the console.
It’s worth noting that the PipedInputStream
and PipedOutputStream
classes are designed for use in a single JVM. If you need to communicate between processes or across a network, you should use a different mechanism such as sockets or RMI.
Example of PipedInputStream and PipedOutputStream classes using threads:
Sure, here’s an example of using PipedInputStream
and PipedOutputStream
with threads:
import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; public class PipedExample { public static void main(String[] args) throws IOException { final PipedOutputStream pos = new PipedOutputStream(); final PipedInputStream pis = new PipedInputStream(pos); Thread t1 = new Thread(() -> { try { pos.write("Hello from Thread 1".getBytes()); pos.close(); } catch (IOException e) { e.printStackTrace(); } }); Thread t2 = new Thread(() -> { try { int data; while ((data = pis.read()) != -1) { System.out.print((char) data); } pis.close(); } catch (IOException e) { e.printStackTrace(); } }); t1.start(); t2.start(); } }
In this example, we create a PipedOutputStream
and a PipedInputStream
. We pass the PipedOutputStream
to the PipedInputStream
constructor to connect the two streams.
We then create two threads, t1
and t2
. t1
writes a message to the PipedOutputStream
and closes it when it’s done. t2
reads from the PipedInputStream
and prints the data to the console until the end of the stream is reached, then closes the stream.
When we run the program, we’ll see the message “Hello from Thread 1” printed to the console. Note that the order of the messages may vary depending on the timing of the threads.