Class FifoBuffer

java.lang.Object
org.jenkinsci.remoting.nio.FifoBuffer
All Implemented Interfaces:
Closeable, AutoCloseable

public class FifoBuffer extends Object implements Closeable
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.
Since:
2.38
Author:
Kohsuke Kawaguchi
  • Constructor Details

    • FifoBuffer

      public FifoBuffer(int pageSize, int limit)
    • FifoBuffer

      public FifoBuffer(int limit)
    • FifoBuffer

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

    • setLimit

      public void setLimit(int newLimit)
      Set limit to the number of maximum bytes this buffer can hold. Write methods will block if the size reaches the limit.
    • readable

      public int readable()
      Number of bytes available in this buffer that are readable.
      Returns:
      -1 if the buffer is closed and there's no more data to read. May return non-negative value if the buffer close is requested, but not performed
    • writable

      public int writable()
      Number of bytes writable.
      Returns:
      Number of bytes we can write to the buffer. If the buffer is closed, may return the value beyond the limit (JENKINS-37514)
    • isClosed

      public boolean isClosed()
      Returns true if the write end of the buffer is already closed, and that no more new data will arrive. Note that there still might be a data left in the buffer to be read. The method may also return true when the close operation is actually requested.
    • getCloseCause

      @CheckForNull public FifoBuffer.CloseCause getCloseCause()
      Returns Exception with stacktrace of the command, which invoked the buffer close.
      Returns:
      Close cause or null
      Since:
      3.3
    • send

      public int send(WritableByteChannel ch) throws IOException
      Read from this buffer write as much as possible to the channel until the channel gets filled up.
      Returns:
      number of bytes that were written. -1 if this buffer is EOF-ed and there will never be any more data to write.
      Throws:
      IOException
    • writeNonBlock

      public int writeNonBlock(ByteBuffer buf)
      Non-blocking write.
      Returns:
      Number of writes written, possibly 0.
    • receive

      public int receive(ReadableByteChannel ch) throws IOException
      Read bytes from a channel and stores it into this buffer.
      Returns:
      number of bytes read, or -1 if the given channel has reached EOF and no further read is possible.
      Throws:
      IOException - receive error
    • write

      public void write(byte[] buf) throws InterruptedException, IOException
      Throws:
      InterruptedException
      IOException
    • write

      public void write(byte[] buf, int start, int len) throws InterruptedException, IOException
      Throws:
      InterruptedException
      IOException
    • close

      public void close()
      Indicates that there will be no more write. Once the remaining bytes are drained by the reader, the read method will start returning EOF signals.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • peek

      public int peek(int offset, byte[] data, int start, int len)
      Peek the specified number of bytes (len) at the specified offset in this buffer (offset) and places it into the specified position (start) of the array (data)
      Returns:
      number of bytes actually peeked. Can be 0 if the offset goes beyond the current readable size in this buffer. Never negative.
    • peek

      public int peek(int offset, byte[] data)
    • read

      public int read(byte[] buf) throws InterruptedException
      Throws:
      InterruptedException
    • read

      public int read(byte[] buf, int start, int len) throws InterruptedException
      Throws:
      InterruptedException
      See Also:
    • readNonBlocking

      public int readNonBlocking(byte[] buf)
    • readNonBlocking

      public int readNonBlocking(byte[] buf, int start, int len)
      See Also:
    • getOutputStream

      public OutputStream getOutputStream()
      Wraps writer end of it to OutputStream.
    • getInputStream

      public InputStream getInputStream()
      Wraps the reader end of it to InputStream