Package org.jgroups.util
Class BlockingInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.jgroups.util.BlockingInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class BlockingInputStream extends java.io.InputStreamBounded 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
- Author:
- Bela Ban
-
-
Field Summary
Fields Modifier and Type Field Description protected byte[]bufThe transfer bufferprotected booleanclosedSet to true whenclose()is calledprotected java.util.concurrent.locks.Locklockprotected java.util.concurrent.locks.Conditionnot_emptySignalled when the buf becomes 'readable'; ie.protected java.util.concurrent.locks.Conditionnot_fullSignalled when the buf becomes 'writeable'; ie.protected intread_posIndex into buf at which the next bytes will be read.protected intwrite_posIndex into buf at which bytes will be written.
-
Constructor Summary
Constructors Constructor Description BlockingInputStream()BlockingInputStream(int capacity)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()intcapacity()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 intremaining()protected static voidsanityCheck(byte[] buf, int offset, int length)Verifies that length doesn't exceed a buffer's lengthprotected intsize()longskip(long n)java.lang.StringtoString()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.
-
-
-
Field Detail
-
closed
protected boolean closed
Set to true whenclose()is called
-
buf
protected final byte[] buf
The transfer buffer
-
read_pos
protected int read_pos
Index into buf at which the next bytes will be read. Has to be between 0 and buf.length-1
-
write_pos
protected int write_pos
Index into buf at which bytes will be written. Has to be between 0 and buf.length-1
-
lock
protected final java.util.concurrent.locks.Lock lock
-
not_full
protected final java.util.concurrent.locks.Condition not_full
Signalled when the buf becomes 'writeable'; ie. is not full anymore
-
not_empty
protected final java.util.concurrent.locks.Condition not_empty
Signalled when the buf becomes 'readable'; ie. is not empty anymore
-
-
Method Detail
-
read
public int read() throws java.io.IOException- Specified by:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
read
public int read(byte[] b) throws java.io.IOException- Overrides:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
read
public int read(byte[] b, int off, int len) throws java.io.IOException- Overrides:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
write
public void write(byte[] buf) throws java.io.IOExceptionAppends bytes to the end of the stream- Parameters:
buf-- Throws:
java.io.IOException- See Also:
write(byte[],int,int)
-
write
public void write(byte[] buf, int offset, int length) throws java.io.IOExceptionAppends 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:
java.io.IOException
-
skip
public long skip(long n) throws java.io.IOException- Overrides:
skipin classjava.io.InputStream- Throws:
java.io.IOException
-
available
public int available() throws java.io.IOException- Overrides:
availablein classjava.io.InputStream- Throws:
java.io.IOException
-
capacity
public int capacity()
-
close
public void close() throws java.io.IOExceptionCloses 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 interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream- Throws:
java.io.IOException
-
isClosed
public boolean isClosed()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
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-
-
-