Package com.trilead.ssh2.channel
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 class
Unit of buffer, singly linked and lazy created as needed.(package private) class
Points to a specific byte in aFifoBuffer.Page
. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
Set to true when the writer closes the write end.private int
Cap to the # of bytes that we can hold.private final Object
private final int
private FifoBuffer.Pointer
The position at which the next read/write will happen.private int
Number of bytes currently in this ring bufferprivate FifoBuffer.Pointer
The 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 TypeMethodDescriptionvoid
close()
private FifoBuffer.Page
newPage()
int
read
(byte[] buf, int start, int len) (package private) int
readable()
Number of bytes readableprivate void
If the ring is no longer needed, release the buffer.void
setLimit
(int newLimit) (package private) int
writable()
Number of bytes writablevoid
write
(byte[] buf, int start, int len) int
writeTo
(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
-