Package com.trilead.ssh2.channel
Class FifoBuffer
- java.lang.Object
-
- com.trilead.ssh2.channel.FifoBuffer
-
class FifoBuffer extends java.lang.ObjectFIFO 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 Classes Modifier and Type Class Description (package private) static classFifoBuffer.PageUnit of buffer, singly linked and lazy created as needed.(package private) classFifoBuffer.PointerPoints to a specific byte in aFifoBuffer.Page.
-
Field Summary
Fields Modifier and Type Field Description private booleanclosedSet to true when the writer closes the write end.private intlimitCap to the # of bytes that we can hold.private java.lang.Objectlockprivate intpageSizeprivate FifoBuffer.PointerrThe position at which the next read/write will happen.private intszNumber of bytes currently in this ring bufferprivate FifoBuffer.PointerwThe position at which the next read/write will happen.
-
Constructor Summary
Constructors Constructor Description FifoBuffer(int pageSize, int limit)FifoBuffer(java.lang.Object lock, int pageSize, int limit)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()private FifoBuffer.PagenewPage()intread(byte[] buf, int start, int len)(package private) intreadable()Number of bytes readableprivate voidreleaseRing()If 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(java.io.OutputStream out)Write whatever readable to the specified OutputStream, then return.
-
-
-
Field Detail
-
lock
private final java.lang.Object lock
-
sz
private int sz
Number of bytes currently in this ring buffer
-
limit
private int limit
Cap to the # of bytes that we can hold.
-
pageSize
private final int pageSize
-
r
private FifoBuffer.Pointer r
The position at which the next read/write will happen.
-
w
private FifoBuffer.Pointer w
The position at which the next read/write will happen.
-
closed
private boolean closed
Set to true when the writer closes the write end.
-
-
Method Detail
-
setLimit
public void setLimit(int newLimit)
-
newPage
private FifoBuffer.Page newPage()
-
readable
int readable()
Number of bytes readable
-
writable
int writable()
Number of bytes writable
-
write
public void write(byte[] buf, int start, int len) throws java.lang.InterruptedException- Throws:
java.lang.InterruptedException
-
close
public void close()
-
releaseRing
private void releaseRing()
If the ring is no longer needed, release the buffer.
-
read
public int read(byte[] buf, int start, int len) throws java.lang.InterruptedException- Throws:
java.lang.InterruptedException- See Also:
InputStream.read(byte[],int,int)
-
writeTo
public int writeTo(java.io.OutputStream out) throws java.io.IOExceptionWrite whatever readable to the specified OutputStream, then return.- Throws:
java.io.IOException
-
-