public abstract class GeneratedInputStream extends InputStream
InputStream
that calls a method to generate blocks of data.
This can be used to replace a pipe in many situations where you can't spin
up a new thread. It's also useful for testing.
To use, implement nextBuffer()
. Each time this method is called, it
should return an arbitrary-size byte[]
containing the next
chunk of data. At EOF, it returns null
.
Constructor and Description |
---|
GeneratedInputStream() |
Modifier and Type | Method and Description |
---|---|
int |
available()
Returns the number of bytes available in the current buffer —
assumes that a call to
nextBuffer() will block. |
void |
close()
By default, closing the stream simply sets a flag such that all
subsequent reads will throw.
|
void |
mark(int readlimit)
By default, this method does nothing.
|
boolean |
markSupported()
By default this method returns
false . |
protected abstract byte[] |
nextBuffer()
Returns the next buffer of generated data,
null when
there's no more data. |
int |
read() |
int |
read(byte[] b)
Attempts to completely fill the passed buffer, calling
nextBuffer()
until it is either full or EOF is reached. |
int |
read(byte[] b,
int off,
int len) |
void |
reset()
By default this method throws
IOException . |
long |
skip(long n)
This method calls
read() until either the requisite number of
bytes have been read, or EOF is reached. |
public int available() throws IOException
nextBuffer()
will block.available
in class InputStream
IOException
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class InputStream
IOException
public void mark(int readlimit)
mark
in class InputStream
public boolean markSupported()
false
. Subclasses may
override.markSupported
in class InputStream
public void reset() throws IOException
IOException
. Subclasses may
override.reset
in class InputStream
IOException
public int read() throws IOException
read
in class InputStream
IOException
public int read(byte[] b, int off, int len) throws IOException
read
in class InputStream
IOException
public int read(byte[] b) throws IOException
nextBuffer()
until it is either full or EOF is reached. Note that this differs from a
"normal" stream, which makes a single attempt to read the underlying
data.read
in class InputStream
IOException
public long skip(long n) throws IOException
read()
until either the requisite number of
bytes have been read, or EOF is reached.skip
in class InputStream
IOException
protected abstract byte[] nextBuffer() throws IOException
null
when
there's no more data. Buffers may be any size.IOException