Class FifoBuffer

java.lang.Object
com.trilead.ssh2.channel.FifoBuffer

class FifoBuffer extends 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 final class 
    Unit of buffer, singly linked and lazy created as needed.
    (package private) class 
    Points to a specific byte in a FifoBuffer.Page.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private 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
     
    The position at which the next read/write will happen.
    private int
    Number of bytes currently in this ring buffer
    The position at which the next read/write will happen.
  • Constructor Summary

    Constructors
    Constructor
    Description
    FifoBuffer(int pageSize, int limit)
     
    FifoBuffer(Object lock, int pageSize, int limit)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
     
    int
    read(byte[] buf, int start, int len)
     
    (package private) int
    Number of bytes readable
    private void
    If the ring is no longer needed, release the buffer.
    void
    setLimit(int newLimit)
     
    (package private) int
    Number of bytes writable
    void
    write(byte[] buf, int start, int len)
     
    int
    Write whatever readable to the specified OutputStream, then return.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • lock

      private final 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.
  • Constructor Details

    • FifoBuffer

      FifoBuffer(int pageSize, int limit)
    • FifoBuffer

      FifoBuffer(Object lock, int pageSize, int limit)
  • Method Details