Name

Pipe

Synopsis

A pipe is an abstraction that allows the one-way transfer of bytes from one thread to another. A pipe has a “read end” and a “write end” which are represented by objects that implement the ReadableByteChannel and WritableByteChannel interfaces. Create a new pipe with the static Pipe.open( ) method. Call the sink( ) method to obtain the Pipe.SinkChannel object that represents the write end of the pipe, and call the source( ) method to obtain the Pipe.SourceChannel object that represents the read end of the pipe.

Programmers familiar with Unix-style pipes may find the names and return values of the sink( ) and source( ) methods confusing. A Unix pipe is an interprocess communication mechanism that is tied to two specific processes, one of which is a source of bytes and one of which is a destination, or sink, for those bytes. With this conceptual model of a pipe, you would expect the source to obtain the channel it writes to with the source( ) method and the sink to obtain the channel it reads from with the sink( ) method.

This Pipe class is not a Unix-style pipe, however. While it can be used for communication between two threads, the ends of the pipe are not tied to those threads, and there need not be a single source thread and a single sink thread. Therefore, in the Pipe API it is the pipe itself that serves as the source and the sink of bytes: bytes are read from the source end of the pipe, and are written to the sink end.

public abstract class Pipe {
// Protected ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.