Java NiO 13, Java NIO Pipe (pipeline)

Source: Internet
Author: User

Last update time: 2014-06-23

A Java NiO pipeline is a one-way data connection between two threads. A pipe has a source channel and a sinking channel. You write data to a sinking channel. This data is then read from the source channel.

Here's a plumbing principle:


Create a pipeline

You can open a pipeline by calling the Pipe.open () method, like this:

Pipe pipe = Pipe.open ();

Write a pipeline

In order to write a pipeline, you need to access this sinking channel, like this:

Pipe.sinkchannel Sinkchannel = Pipe.sink ();

Write a sinkchannel by calling the 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);}

read from a pipe

In order to read data from a pipeline, you need to access this source channel, like this:

Pipe.sourcechannel Sourcechannel = Pipe.source ();

In order to read the data from the source channel, you can call the Read () method, like this:

Bytebuffer buf = bytebuffer.allocate; int bytesread = Inchannel.read (BUF);

The int value returned by the Read method tells you how many bytes were read in buffer.


Translation Address: http://tutorials.jenkov.com/java-nio/pipe.html

Java NiO 13, Java NIO Pipe (pipeline)

Related Article

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.