public final class Pipe
extends java.lang.Object
This class uses a buffer to decouple source and sink. This buffer has a user-specified maximum size. When a producer thread outruns its consumer the buffer fills up and eventually writes to the sink will block until the consumer has caught up. Symmetrically, if a consumer outruns its producer reads block until there is data to be read. Limits on the amount of time spent waiting for the other party can be configured with timeouts on the source and the sink.
When the sink is closed, source reads will continue to complete normally until the buffer has
been exhausted. At that point reads will return -1, indicating the end of the stream. But if the
source is closed first, writes to the sink will immediately fail with an IOException
.
Modifier and Type | Class and Description |
---|---|
(package private) class |
Pipe.PipeSink |
(package private) class |
Pipe.PipeSource |
Modifier and Type | Field and Description |
---|---|
(package private) Buffer |
buffer |
private Sink |
foldedSink |
(package private) long |
maxBufferSize |
private Sink |
sink |
(package private) boolean |
sinkClosed |
private Source |
source |
(package private) boolean |
sourceClosed |
Constructor and Description |
---|
Pipe(long maxBufferSize) |
Modifier and Type | Method and Description |
---|---|
void |
fold(Sink sink)
Writes any buffered contents of this pipe to
sink , then replace this pipe's source
with sink . |
Sink |
sink() |
Source |
source() |
final long maxBufferSize
final Buffer buffer
boolean sinkClosed
boolean sourceClosed
private final Sink sink
private final Source source
@Nullable private Sink foldedSink
public final Source source()
public final Sink sink()
public void fold(Sink sink) throws java.io.IOException
sink
, then replace this pipe's source
with sink
. This pipe's source is closed and attempts to read it will throw an
IllegalStateException
.
This method must not be called while concurrently accessing this pipe's source. It is safe,
however, to call this while concurrently writing this pipe's sink.java.io.IOException