Package com.trilead.ssh2.channel
Class FifoBuffer
- java.lang.Object
-
- com.trilead.ssh2.channel.FifoBuffer
-
class FifoBuffer extends java.lang.Object
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 Classes Modifier and Type Class Description (package private) static class
FifoBuffer.Page
Unit of buffer, singly linked and lazy created as needed.(package private) class
FifoBuffer.Pointer
Points to a specific byte in aFifoBuffer.Page
.
-
Field Summary
Fields Modifier and Type Field Description private boolean
closed
Set to true when the writer closes the write end.private int
limit
Cap to the # of bytes that we can hold.private java.lang.Object
lock
private int
pageSize
private FifoBuffer.Pointer
r
The position at which the next read/write will happen.private int
sz
Number of bytes currently in this ring bufferprivate FifoBuffer.Pointer
w
The 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 void
close()
private FifoBuffer.Page
newPage()
int
read(byte[] buf, int start, int len)
(package private) int
readable()
Number of bytes readableprivate void
releaseRing()
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(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.IOException
Write whatever readable to the specified OutputStream, then return.- Throws:
java.io.IOException
-
-