public class EOFInputStream
extends java.io.InputStream
The built-in InputStream imposes a problem for clients that don't want to block. A client doesn't know what to do when available == 0 - does it mean "fetching more data" or "EOF". It is possible for an InputStream to support this scenario - the InputStream can "lie" and report 1 available byte and when the client comes to read it, it will get -1 right away. However, neither FileInputStream nor the InputStream from Process.getInputStream() do that.
This class enables clients to use their own information on whether EOF of the InputStream has been reached.
Optionally, clients may modify the available behavior.
This class offers static factory methods for creating EOFInputStream instances from File and from Process.
Modifier | Constructor and Description |
---|---|
protected |
EOFInputStream()
Implicit constructor for derived classes.
|
|
EOFInputStream(java.io.InputStream inputStream,
java.util.function.Predicate<AsyncByteStreamReader> eof)
Constructs a new EOFInputStream.
|
|
EOFInputStream(java.io.InputStream inputStream,
java.util.function.Predicate<AsyncByteStreamReader> eof,
java.util.function.ToIntFunction<AsyncByteStreamReader> available)
Constructs a new EOFInputStream.
|
Modifier and Type | Method and Description |
---|---|
int |
available() |
boolean |
eof()
Used by the consuming
AsyncByteStreamReader to check whether EOF has been reached. |
static EOFInputStream |
fromFile(java.io.File file)
Factory method for Process.getInputStream().
|
static EOFInputStream |
fromProcess(java.lang.Process process)
Factory method for Process.getInputStream().
|
int |
read() |
int |
read(byte[] b,
int off,
int len) |
public EOFInputStream(java.io.InputStream inputStream, java.util.function.Predicate<AsyncByteStreamReader> eof)
inputStream
- InputStream to override.eof
- EOF predicate. Takes a AsyncByteStreamReader
as a parameter.
It could be used to get hold of the related ByteRingBuffer
to get the
total count of read bytes or to obtain other related objects.public EOFInputStream(java.io.InputStream inputStream, java.util.function.Predicate<AsyncByteStreamReader> eof, java.util.function.ToIntFunction<AsyncByteStreamReader> available)
inputStream
- InputStream to override.eof
- EOF predicate. Takes a AsyncByteStreamReader
as a parameter.
It could be used to get hold of the related ByteRingBuffer
to get the
total count of read bytes or to obtain other related objects.available
- available function. Takes a AsyncByteStreamReader
as a parameter.
It could be used to get hold of the related ByteRingBuffer
to get the
total count of read bytes or to obtain other related objects.protected EOFInputStream()
public boolean eof()
AsyncByteStreamReader
to check whether EOF has been reached.
Invokes the eof() predicate passing the consuming AsyncByteStreamReader
.public int available() throws java.io.IOException
available
in class java.io.InputStream
java.io.IOException
InputStream
public int read() throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
InputStream
public int read(byte[] b, int off, int len) throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
InputStream
public static EOFInputStream fromProcess(java.lang.Process process)
eof() returns true iff Process.isAlive() returns false.
process
- The Process whose InputStream will be read.public static EOFInputStream fromFile(java.io.File file) throws java.io.FileNotFoundException
eof() returns true iff File.length bytes have been read by the consuming AsyncByteStreamReader
.
file
- The File whose FileInputStream will be read.java.io.FileNotFoundException
- @see FileInputStream.