Java NIO Pipe

Source: Internet
Author: User


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:

Java Nio:pipe Internals
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.