Package org.jgroups.util
Class BlockingInputStream
java.lang.Object
java.io.InputStream
org.jgroups.util.BlockingInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
Bounded input stream. A consumer reads bytes until the end of the stream is reached, or the input stream is closed.
The producer writes bytes to the tail and blocks if the capacity has been reached (until the consumer reads more bytes).
This class is for only 1 producer and 1 consumer; multiple producers/consumers will most likely yield incorrect results !
Note that the implementation of this class is optimized for reading and adding a few K at a time; performance will be suboptimal if single bytes are added and read.
- Since:
- 2.12.2
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final byte[]The transfer bufferprotected booleanSet to true whenclose()is calledprotected final Lockprotected final ConditionSignalled when the buf becomes 'readable'; ie.protected final ConditionSignalled when the buf becomes 'writeable'; ie.protected intIndex into buf at which the next bytes will be read.protected intIndex into buf at which bytes will be written. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintintcapacity()voidclose()Closes the stream.protected voidcompact()Moves the bytes between [read_pos and write_pos] read_pos bytes to the left, such that the new read_pos is 0 and the write_pos is write_pos - read_pos.booleanisClosed()intread()intread(byte[] b) intread(byte[] b, int off, int len) protected intprotected static voidsanityCheck(byte[] buf, int offset, int length) Verifies that length doesn't exceed a buffer's lengthprotected intsize()longskip(long n) toString()voidwrite(byte[] buf) Appends bytes to the end of the streamvoidwrite(byte[] buf, int offset, int length) Appends bytes to the end of the stream.Methods inherited from class java.io.InputStream
mark, markSupported, nullInputStream, readAllBytes, readNBytes, readNBytes, reset, skipNBytes, transferTo
-
Field Details
-
closed
protected boolean closedSet to true whenclose()is called -
buf
protected final byte[] bufThe transfer buffer -
read_pos
protected int read_posIndex into buf at which the next bytes will be read. Has to be between 0 and buf.length-1 -
write_pos
protected int write_posIndex into buf at which bytes will be written. Has to be between 0 and buf.length-1 -
lock
-
not_full
Signalled when the buf becomes 'writeable'; ie. is not full anymore -
not_empty
Signalled when the buf becomes 'readable'; ie. is not empty anymore
-
-
Constructor Details
-
BlockingInputStream
public BlockingInputStream() -
BlockingInputStream
public BlockingInputStream(int capacity)
-
-
Method Details
-
read
- Specified by:
readin classInputStream- Throws:
IOException
-
read
- Overrides:
readin classInputStream- Throws:
IOException
-
read
- Overrides:
readin classInputStream- Throws:
IOException
-
write
Appends bytes to the end of the stream- Parameters:
buf-- Throws:
IOException- See Also:
-
write
Appends bytes to the end of the stream. If the number of bytes to be written is greater than the remaining capacity, write() will block until the bytes can be added, or the stream is closed.This method will try to append partial buffers to the stream, e.g. if the remaining capacity is 2K, but the length of the buffer is 5K, 2K will be written and then write() will block until the remaining 3K can be added.
- Parameters:
buf- The buffer to be added to the end of the streamoffset- The offset within buf at which bytes are readlength- The number of bytes to be added- Throws:
IOException
-
skip
- Overrides:
skipin classInputStream- Throws:
IOException
-
available
- Overrides:
availablein classInputStream- Throws:
IOException
-
capacity
public int capacity() -
close
Closes the stream. Writes to a closed stream will fail, reads will successfully read the bytes that are already in the buffer and then return -1 (EOF)- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException
-
isClosed
public boolean isClosed() -
toString
-
size
protected int size() -
remaining
protected int remaining() -
compact
protected void compact()Moves the bytes between [read_pos and write_pos] read_pos bytes to the left, such that the new read_pos is 0 and the write_pos is write_pos - read_pos. Lock must be held. -
sanityCheck
protected static void sanityCheck(byte[] buf, int offset, int length) Verifies that length doesn't exceed a buffer's length- Parameters:
buf-offset-length-
-