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.InputStream
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
- Author:
- Bela Ban
-
-
Field Summary
Fields Modifier and Type Field Description protected byte[]
buf
The transfer bufferprotected boolean
closed
Set to true whenclose()
is calledprotected java.util.concurrent.locks.Lock
lock
protected java.util.concurrent.locks.Condition
not_empty
Signalled when the buf becomes 'readable'; ie.protected java.util.concurrent.locks.Condition
not_full
Signalled when the buf becomes 'writeable'; ie.protected int
read_pos
Index into buf at which the next bytes will be read.protected int
write_pos
Index 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 int
available()
int
capacity()
void
close()
Closes the stream.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.boolean
isClosed()
int
read()
int
read(byte[] b)
int
read(byte[] b, int off, int len)
protected int
remaining()
protected static void
sanityCheck(byte[] buf, int offset, int length)
Verifies that length doesn't exceed a buffer's lengthprotected int
size()
long
skip(long n)
java.lang.String
toString()
void
write(byte[] buf)
Appends bytes to the end of the streamvoid
write(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:
read
in classjava.io.InputStream
- Throws:
java.io.IOException
-
read
public int read(byte[] b) throws java.io.IOException
- Overrides:
read
in classjava.io.InputStream
- Throws:
java.io.IOException
-
read
public int read(byte[] b, int off, int len) throws java.io.IOException
- Overrides:
read
in classjava.io.InputStream
- Throws:
java.io.IOException
-
write
public void write(byte[] buf) throws java.io.IOException
Appends 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.IOException
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:
java.io.IOException
-
skip
public long skip(long n) throws java.io.IOException
- Overrides:
skip
in classjava.io.InputStream
- Throws:
java.io.IOException
-
available
public int available() throws java.io.IOException
- Overrides:
available
in classjava.io.InputStream
- Throws:
java.io.IOException
-
capacity
public int capacity()
-
close
public void close() throws java.io.IOException
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:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.InputStream
- Throws:
java.io.IOException
-
isClosed
public boolean isClosed()
-
toString
public java.lang.String toString()
- Overrides:
toString
in 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
-
-
-