public class InboundBuffer<E> extends Object
write(E) any number of elements and shall
cooperate with the buffer to not overload it.
write(E) method indicates
whether it can continue safely adding more elements or stop.
The producer can set a drainHandler to be signaled when it can resume writing again. When a write
returns false, the drain handler will be called when the buffer becomes writable again. Note that subsequent
call to write will not prevent the drain handler to be called.
handler to consume elements.
handler.handler.pause(), resume() and fetch(long) methods:
resume() sets the flowing modepause() sets the fetch mode and resets the demand to 0fetch(long) requests a specific amount of elements and adds it to the actual demandIllegalStateException is thrown.
Other methods can be called from any thread.
The handlers will always be called from a context thread.
WARNING: this class is mostly useful for implementing the ReadStream
and has little or no use within a regular application.| Modifier and Type | Field and Description |
|---|---|
static Object |
END_SENTINEL
A reusable sentinel for signaling the end of a stream.
|
| Constructor and Description |
|---|
InboundBuffer(Context context) |
InboundBuffer(Context context,
long highWaterMark) |
| Modifier and Type | Method and Description |
|---|---|
InboundBuffer<E> |
clear()
Clear the buffer synchronously.
|
InboundBuffer<E> |
drainHandler(Handler<Void> handler)
Set an
handler to be called when the buffer is drained and the producer can resume writing to the buffer. |
InboundBuffer<E> |
emptyHandler(Handler<Void> handler)
Set an
handler to be called when the buffer becomes empty. |
InboundBuffer<E> |
exceptionHandler(Handler<Throwable> handler)
Set an
handler to be called when an exception is thrown by an handler. |
boolean |
fetch(long amount)
Request a specific
amount of elements to be fetched, the amount is added to the actual demand. |
InboundBuffer<E> |
handler(Handler<E> handler)
Set an
handler to be called with elements available from this buffer. |
boolean |
isEmpty() |
boolean |
isPaused() |
boolean |
isWritable() |
InboundBuffer<E> |
pause()
Pause the buffer, it sets the buffer in
fetch mode and clears the actual demand. |
E |
read()
Read the most recent element synchronously.
|
boolean |
resume()
Resume the buffer, and sets the buffer in
flowing mode. |
int |
size() |
boolean |
write(E element)
Write an
element to the buffer. |
boolean |
write(Iterable<E> elements)
Write an
iterable of elements. |
public static final Object END_SENTINEL
public InboundBuffer(Context context)
public InboundBuffer(Context context, long highWaterMark)
public boolean write(E element)
element to the buffer. The element will be delivered synchronously to the handler when
it is possible, otherwise it will be queued for later delivery.element - the element to addfalse when the producer should stop writingpublic boolean write(Iterable<E> elements)
iterable of elements.elements - the elements to addfalse when the producer should stop writing#write(E)public boolean fetch(long amount)
amount of elements to be fetched, the amount is added to the actual demand.
Pending elements in the buffer will be delivered asynchronously on the context to the handler.
This method can be called from any thread.true when the buffer will be drainedpublic E read()
null if no element was in the bufferpublic InboundBuffer<E> clear()
public InboundBuffer<E> pause()
fetch mode and clears the actual demand.public boolean resume()
flowing mode.
Pending elements in the buffer will be delivered asynchronously on the context to the handler.
This method can be called from any thread.true when the buffer will be drainedpublic InboundBuffer<E> handler(Handler<E> handler)
handler to be called with elements available from this buffer.handler - the handlerpublic InboundBuffer<E> drainHandler(Handler<Void> handler)
handler to be called when the buffer is drained and the producer can resume writing to the buffer.handler - the handler to be calledpublic InboundBuffer<E> emptyHandler(Handler<Void> handler)
handler to be called when the buffer becomes empty.handler - the handler to be calledpublic InboundBuffer<E> exceptionHandler(Handler<Throwable> handler)
handler to be called when an exception is thrown by an handler.handler - the handlerpublic boolean isEmpty()
public boolean isWritable()
public boolean isPaused()
fetch mode and the demand is 0.public int size()
Copyright © 2020. All rights reserved.