Java Nio
1 |
Java NIO Tutorial |
2 |
Java NIO Overview |
3 |
Java NIO Channel |
4 |
Java NIO Buffer |
5 |
Java NIO Scatter/gather |
6 |
Java NIO Channel to channel transfers |
7 |
Java NIO Selector |
8 |
Java NIO FileChannel |
9 |
Java NIO Socketchannel |
10 |
Java NIO Serversocketchannel |
11 |
java NIO Datagramchannel |
12 |
Java NIO Pipe |
13 |
Java NIO vs. IO
|
Java NIO Pipe
by Jakob JenkovConnect with me:
Rate article: Share Article:tweet
Table of Contents
- Creating a Pipe
- Writing to a Pipe
- Reading from a Pipe
A Java NIO Pipe is a one-way data connection between and threads. A has Pipe
a source channel and a sink channel. You write data to the sink channel. This data can and is read from the source channel.
Here's an illustration of the Pipe
principle:
Creating a Pipe
You open a by Pipe
calling the Pipe.open()
method. Here's how this looks:
Pipe pipe = Pipe.open ();
Writing to a Pipe
To write to a you Pipe
need to access the sink channel. Here's how it's done:
Pipe.sinkchannel Sinkchannel = Pipe.sink ();
You write to a SinkChannel
by calling it's write()
method, like this:
String NewData = "New String to write to file ..." + System.currenttimemillis (); Bytebuffer buf = bytebuffer.allocate; Buf.clear (); Buf.put (Newdata.getbytes ()); Buf.flip (); while ( Buf.hasremaining ()) { sinkchannel.write (BUF);}
Reading from a Pipe
To read from a you Pipe
need to access the source channel. Here's how it's done:
Pipe.sourcechannel Sourcechannel = Pipe.source ();
To-read from the source channel, it read()
method like this:
Bytebuffer buf = bytebuffer.allocate; int bytesread = Inchannel.read (BUF);
The int
returned by the read()
method tells how to many bytes were read into the buffer.
Next: Java NIO vs. IO
Java NIO Pipe