public class Pump
extends java.lang.Object
ReadStream
to a WriteStream
and performs flow control where necessary to
prevent the write stream from getting overloaded.
Instances of this class read bytes from a ReadStream
and write them to a WriteStream
. If data
can be read faster than it can be written this could result in the write queue of the WriteStream
growing
without bound, eventually causing it to exhaust all available RAM.
To prevent this, after each write, instances of this class check whether the write queue of the WriteStream
is full, and if so, the ReadStream
is paused, and a drainHandler
is set on the
WriteStream
. When the WriteStream
has processed half of its backlog, the drainHandler
will be
called, which results in the pump resuming the ReadStream
.
This class can be used to pump from any ReadStream
to any WriteStream
,
e.g. from an HttpServerRequest
to an AsyncFile
,
or from NetSocket
to a WebSocket
.
Modifier and Type | Method and Description |
---|---|
static Pump |
createPump(ReadStream rs,
WriteStream ws)
Create a new
Pump with the given ReadStream and WriteStream |
static Pump |
createPump(ReadStream rs,
WriteStream ws,
int writeQueueMaxSize)
Create a new
Pump with the given ReadStream and WriteStream and
writeQueueMaxSize |
int |
getBytesPumped()
Return the total number of bytes pumped by this pump.
|
void |
setWriteQueueMaxSize(int maxSize)
Set the write queue max size to
maxSize |
void |
start()
Start the Pump.
|
void |
stop()
Stop the Pump.
|
public static Pump createPump(ReadStream rs, WriteStream ws)
Pump
with the given ReadStream
and WriteStream
public static Pump createPump(ReadStream rs, WriteStream ws, int writeQueueMaxSize)
Pump
with the given ReadStream
and WriteStream
and
writeQueueMaxSize
public void setWriteQueueMaxSize(int maxSize)
maxSize
public void start()
public void stop()
public int getBytesPumped()