Package org.jenkinsci.remoting.nio
Class FifoBuffer
java.lang.Object
org.jenkinsci.remoting.nio.FifoBuffer
- All Implemented Interfaces:
Closeable
,AutoCloseable
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
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Explains the reason of the buffer close. -
Constructor Summary
ConstructorDescriptionFifoBuffer
(int limit) FifoBuffer
(int pageSize, int limit) FifoBuffer
(Object lock, int pageSize, int limit) -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Indicates that there will be no more write.Returns Exception with stacktrace of the command, which invoked the buffer close.Wraps the reader end of it toInputStream
Wraps writer end of it toOutputStream
.boolean
isClosed()
Returns true if the write end of the buffer is already closed, and that no more new data will arrive.int
peek
(int offset, byte[] data) 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
)int
read
(byte[] buf) int
read
(byte[] buf, int start, int len) int
readable()
Number of bytes available in this buffer that are readable.int
readNonBlocking
(byte[] buf) int
readNonBlocking
(byte[] buf, int start, int len) int
Read bytes from a channel and stores it into this buffer.int
Read from this buffer write as much as possible to the channel until the channel gets filled up.void
setLimit
(int newLimit) Set limit to the number of maximum bytes this buffer can hold.int
writable()
Number of bytes writable.void
write
(byte[] buf) void
write
(byte[] buf, int start, int len) int
writeNonBlock
(ByteBuffer buf) Non-blocking write.
-
Constructor Details
-
FifoBuffer
public FifoBuffer(int pageSize, int limit) -
FifoBuffer
public FifoBuffer(int limit) -
FifoBuffer
-
-
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 returntrue
when the close operation is actually requested. -
getCloseCause
Returns Exception with stacktrace of the command, which invoked the buffer close.- Returns:
- Close cause or
null
- Since:
- 3.3
-
send
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
Non-blocking write.- Returns:
- Number of writes written, possibly 0.
-
receive
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
- Throws:
InterruptedException
IOException
-
write
- 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 interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
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
- Throws:
InterruptedException
-
read
- Throws:
InterruptedException
- See Also:
-
readNonBlocking
public int readNonBlocking(byte[] buf) -
readNonBlocking
public int readNonBlocking(byte[] buf, int start, int len) - See Also:
-
getOutputStream
Wraps writer end of it toOutputStream
. -
getInputStream
Wraps the reader end of it toInputStream
-