Class FifoBuffer
java.lang.Object
com.trilead.ssh2.channel.FifoBuffer
FIFO buffer for a reader thread and a writer thread to collaborate.
Unlike a ring buffer, which uses a fixed memory regardless of the number of bytes currently in the buffer,
this implementation uses a single linked list to reduce the memory footprint when the reader
closely follows the writer, regardless of the capacity limit set in the constructor.
In trilead, the writer puts the data we receive from the network, and the user code acts as a reader.
A user code normally drains the buffer more quickly than what the network delivers, so this implementation
saves memory while simultaneously allowing us to advertise a bigger window size for a large latency network.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final classUnit of buffer, singly linked and lazy created as needed.(package private) classPoints to a specific byte in aFifoBuffer.Page. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanSet to true when the writer closes the write end.private intCap to the # of bytes that we can hold.private final Objectprivate final intprivate FifoBuffer.PointerThe position at which the next read/write will happen.private intNumber of bytes currently in this ring bufferprivate FifoBuffer.PointerThe position at which the next read/write will happen. -
Constructor Summary
ConstructorsConstructorDescriptionFifoBuffer(int pageSize, int limit) FifoBuffer(Object lock, int pageSize, int limit) -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()private FifoBuffer.PagenewPage()intread(byte[] buf, int start, int len) (package private) intreadable()Number of bytes readableprivate voidIf the ring is no longer needed, release the buffer.voidsetLimit(int newLimit) (package private) intwritable()Number of bytes writablevoidwrite(byte[] buf, int start, int len) intwriteTo(OutputStream out) Write whatever readable to the specified OutputStream, then return.
-
Field Details
-
lock
-
sz
private int szNumber of bytes currently in this ring buffer -
limit
private int limitCap to the # of bytes that we can hold. -
pageSize
private final int pageSize -
r
The position at which the next read/write will happen. -
w
The position at which the next read/write will happen. -
closed
private boolean closedSet to true when the writer closes the write end.
-
-
Constructor Details
-
FifoBuffer
FifoBuffer(int pageSize, int limit) -
FifoBuffer
FifoBuffer(Object lock, int pageSize, int limit)
-
-
Method Details
-
setLimit
public void setLimit(int newLimit) -
newPage
-
readable
int readable()Number of bytes readable -
writable
int writable()Number of bytes writable -
write
- Throws:
InterruptedException
-
close
public void close() -
releaseRing
private void releaseRing()If the ring is no longer needed, release the buffer. -
read
- Throws:
InterruptedException- See Also:
-
writeTo
Write whatever readable to the specified OutputStream, then return.- Throws:
IOException
-