Pipe Read Example
This program demonstrates reading data from a Pipe.
Problem Statement
Write a Java program that reads data from a Pipe using Pipe.SourceChannel.
Source Code
| 1 | import java.nio.*; |
| 2 | import java.nio.channels.*; |
| 3 | public class PipeReadExample { |
| 4 | public static void main(String[] args) throws Exception { |
| 5 | System.out.println("Eduinq Pipe Read Example"); |
| 6 | Pipe pipe=Pipe.open(); |
| 7 | Pipe.SinkChannel sink=pipe.sink(); |
| 8 | Pipe.SourceChannel source=pipe.source(); |
| 9 | ByteBuffer buffer=ByteBuffer.allocate(48); |
| 10 | sink.write(ByteBuffer.wrap("Eduinq Pipe Read Data".getBytes())); |
| 11 | source.read(buffer); |
| 12 | buffer.flip(); |
| 13 | while(buffer.hasRemaining()){ System.out.print((char)buffer.get()); } |
| 14 | sink.close(); source.close(); |
| 15 | } |
| 16 | } |
Program Output
Eduinq Pipe Read Example Eduinq Pipe Read Data
Explanation
Uses Pipe.source() for reading. - Transfers data from sink to source. - Exception handling ensures safe operations. - In enterprise systems, Pipe supports producer consumer models. Conclusion: Shows reading with Pipe.