天天看点

Java NIO PipeJava NIO Pipe

Java Nio 

1

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/index.html">Java NIO Tutorial</a>

2

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/overview.html">Java NIO Overview</a>

3

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/channels.html">Java NIO Channel</a>

4

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/buffers.html">Java NIO Buffer</a>

5

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/scatter-gather.html">Java NIO Scatter / Gather</a>

6

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/channel-to-channel-transfers.html">Java NIO Channel to Channel Transfers</a>

7

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/selectors.html">Java NIO Selector</a>

8

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/file-channel.html">Java NIO FileChannel</a>

9

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/socketchannel.html">Java NIO SocketChannel</a>

10

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/server-socket-channel.html">Java NIO ServerSocketChannel</a>

11

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/datagram-channel.html">Java NIO DatagramChannel</a>

12

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/pipe.html">Java NIO Pipe</a>

13

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/nio-vs-io.html">Java NIO vs. IO</a>

Java NIO PipeJava NIO Pipe

Rate article:

Share article:

<a target="_blank" href="https://twitter.com/share">Tweet</a>

Table of Contents

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/pipe.html#creating-a-pipe">Creating a Pipe</a>

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/pipe.html#writing-to-a-pipe">Writing to a Pipe</a>

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/pipe.html#reading-from-a-pipe">Reading from a Pipe</a>

A Java NIO Pipe is a one-way data connection between two threads. A <code>Pipe</code> has a source channel and a sink channel. You write data to the sink channel. This data can then be read from the source channel.

Here is an illustration of the <code>Pipe</code> principle:

Java NIO PipeJava NIO Pipe

Java NIO: Pipe Internals

<a target="_blank"></a>

You open a <code>Pipe</code> by calling the <code>Pipe.open()</code> method. Here is how that looks:

To write to a <code>Pipe</code> you need to access the sink channel. Here is how that is done:

You write to a <code>SinkChannel</code> by calling it's <code>write()</code> method, like this:

To read from a <code>Pipe</code> you need to access the source channel. Here is how that is done:

To read from the source channel you call its <code>read()</code> method like this:

The <code>int</code> returned by the <code>read()</code> method tells how many bytes were read into the buffer.

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/nio-vs-io.html">Next:   Java NIO vs. IO</a>