public class VisibleBufferedInputStream
extends java.io.InputStream
Modifier and Type | Field and Description |
---|---|
private byte[] |
buffer
The buffer.
|
private int |
endIndex
How far is the buffer filled with valid data.
|
private int |
index
Current read position in the buffer.
|
private static int |
MINIMUM_READ
If a direct read to byte array is called that would require a smaller read from the wrapped
stream that MINIMUM_READ then first fill the buffer and serve the bytes from there.
|
private static int |
STRING_SCAN_SPAN
In how large spans is the C string zero-byte scanned.
|
private boolean |
timeoutRequested
socket timeout has been requested
|
private java.io.InputStream |
wrapped
The wrapped input stream.
|
Constructor and Description |
---|
VisibleBufferedInputStream(java.io.InputStream in,
int bufferSize)
Creates a new buffer around the given stream.
|
Modifier and Type | Method and Description |
---|---|
int |
available() |
void |
close() |
private void |
compact()
Compacts the unread bytes of the buffer to the beginning of the buffer.
|
private void |
doubleBuffer()
Doubles the size of the buffer.
|
boolean |
ensureBytes(int n)
Ensures that the buffer contains at least n bytes.
|
boolean |
ensureBytes(int n,
boolean block)
Ensures that the buffer contains at least n bytes.
|
byte[] |
getBuffer()
Returns direct handle to the used buffer.
|
int |
getIndex()
Returns the current read position in the buffer.
|
private void |
moveBufferTo(byte[] dest)
Moves bytes from the buffer to the beginning of the destination buffer.
|
int |
peek()
Reads a byte from the buffer without advancing the index pointer.
|
int |
read() |
int |
read(byte[] to,
int off,
int len) |
private boolean |
readMore(int wanted,
boolean block)
Reads more bytes into the buffer.
|
byte |
readRaw()
Reads byte from the buffer without any checks.
|
int |
scanCStringLength()
Scans the length of the next null terminated string (C-style string) from the stream.
|
void |
setTimeoutRequested(boolean timeoutRequested) |
long |
skip(long n) |
private static final int MINIMUM_READ
private static final int STRING_SCAN_SPAN
private final java.io.InputStream wrapped
private byte[] buffer
private int index
private int endIndex
private boolean timeoutRequested
public VisibleBufferedInputStream(java.io.InputStream in, int bufferSize)
in
- The stream to buffer.bufferSize
- The initial size of the buffer.public int read() throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
public int peek() throws java.io.IOException
java.io.IOException
- if something wrong happenspublic byte readRaw()
ensureBytes(int)
method must have been called.java.lang.ArrayIndexOutOfBoundsException
- If ensureBytes was not called to make sure the buffer
contains the byte.public boolean ensureBytes(int n) throws java.io.IOException
n
- The amount of bytes to ensure exists in bufferjava.io.IOException
- If reading of the wrapped stream failed.public boolean ensureBytes(int n, boolean block) throws java.io.IOException
n
- The amount of bytes to ensure exists in bufferblock
- whether or not to block the IOjava.io.IOException
- If reading of the wrapped stream failed.private boolean readMore(int wanted, boolean block) throws java.io.IOException
wanted
- How much should be at least read.java.io.IOException
- If reading of the wrapped stream failed.private void doubleBuffer()
private void compact()
private void moveBufferTo(byte[] dest)
dest
- The destination buffer.public int read(byte[] to, int off, int len) throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
public long skip(long n) throws java.io.IOException
skip
in class java.io.InputStream
java.io.IOException
public int available() throws java.io.IOException
available
in class java.io.InputStream
java.io.IOException
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.InputStream
java.io.IOException
public byte[] getBuffer()
ensureBytes(int)
to prefill required
bytes the buffer and getIndex()
to fetch the current position of the buffer.public int getIndex()
public int scanCStringLength() throws java.io.IOException
java.io.IOException
- If reading of stream fails.java.io.EOFException
- If the stream did not contain any null terminators.public void setTimeoutRequested(boolean timeoutRequested)