public class MemoryStream extends InputStream
You can also use it to provide a standard Streaming interface to a non-Streaming data source/sink, by setting it to be read-only/write-only and overriding the methods that provide or use the data.
Modifier and Type | Field and Description |
---|---|
int |
maxBufferSize |
Constructor and Description |
---|
MemoryStream() |
MemoryStream(int maxCapacity) |
Modifier and Type | Method and Description |
---|---|
int |
available()
Get the number of bytes in the buffer.
|
int |
bytesInBuffer()
Get the number of bytes in the buffer.
|
protected int |
canWrite()
Returns the number of bytes that can be written to the buffer without blocking
|
void |
close()
Close the MemoryStream for input.
|
protected void |
doGenerateData(Handle h)
This method should be overriden if you are using a separate thread to provide
data, but don't want to use getOutputStream() to write data into the buffer.
|
protected void |
flush() |
protected void |
generateInputError(IOException e)
This will cause an IOException to be thrown on calling one of the read() methods.
|
InputStream |
getInputStream()
Return an InputStream to read from the MemoryStream - which in this implementation
is itself.
|
OutputStream |
getOutputStream()
Get an OutputStream to write to the MemoryStream for reading back from the
InputStream.
|
protected boolean |
inputWasClosed()
Returns if the close() was called on the MemoryStream.
|
protected void |
noMoreData()
Used if you are placing data into the Stream using the direct write() methods.
|
static Object[] |
pipe(int bufferSize)
Create a MemoryStream and return an InputStream and an OutputStream for it.
|
static void |
pipe2(InputStream[] inputs,
OutputStream[] outputs,
int bufferSize)
Create two pipes and return their Input/Output streams such that inputs[0]
and outputs[0] should be used by an entity on one side of the connection
and inputs[1] and outputs[1] should be used by an entity on the other side.
|
int |
read()
This method reads an unsigned byte from the input stream and returns it
as an int in the range of 0-255.
|
int |
read(byte[] dest,
int offset,
int length)
This method read bytes from a stream and stores them into a
caller supplied buffer.
|
protected void |
readBlocked(int waitingFor)
Use of this method is optional - this is called if a read is requested but there is no data available.
|
protected Handle |
startGeneratingData()
This is used to start the thread that provides data.
|
protected boolean |
waitForDataRequest()
Call this from doGenerateData().
|
protected boolean |
waitForDataRequest(TimeOut waitFor)
Call this from doGenerateData().
|
protected void |
write(byte[] source,
int offset,
int length)
Place data into the buffer.
|
finalize, hashCode, mark, markSupported, read, reset, skip
public MemoryStream()
public MemoryStream(int maxCapacity)
protected void doGenerateData(Handle h)
This method should basically loop calling waitForDataRequest(). If waitForDataRequest() returns false this indicates that no more input is needed and the method should exit. Otherwise it should gather data and then call write() to put the data in the buffer.
When there is no more data to write it can then simply exit and the output portion will be closed.
h
- a Handle that can be used to stop the doGenerateData(). If shouldStop
is true then the method should exit immediately.protected Handle startGeneratingData()
protected final boolean waitForDataRequest()
protected final boolean waitForDataRequest(TimeOut waitFor) throws TimedOutException
waitFor
- a length of time to wait for. If it is null then it will wait forever.TimedOutException
- if the request timed out with no read request being made.protected void noMoreData()
protected void generateInputError(IOException e)
e
- the IOException to be thrown.protected final boolean inputWasClosed()
protected final int canWrite()
protected final void write(byte[] source, int offset, int length) throws IOException
source
- the source of the bytes.offset
- the offset in the source of the data.length
- the number of bytes to write.IOException
- only if close() has been called on the MemoryStream so
that no further reads are possible.protected final void flush() throws IOException
IOException
public OutputStream getOutputStream()
public void close() throws IOException
close
in class InputStream
IOException
- If an error occurs, which can only happen
in a subclasspublic int read() throws IOException
InputStream
This method will block until the byte can be read.
read
in class InputStream
IOException
- If an error occurspublic int read(byte[] dest, int offset, int length) throws IOException
InputStream
off
into the buffer and attempts to read
len
bytes. This method can return before reading the
number of bytes requested. The actual number of bytes read is
returned as an int. A -1 is returned to indicate the end of the
stream.
This method will block until some data can be read.
This method operates by calling the single byte read()
method
in a loop until the desired number of bytes are read. The read loop
stops short if the end of the stream is encountered or if an IOException
is encountered on any read operation except the first. If the first
attempt to read a bytes fails, the IOException is allowed to propagate
upward. And subsequent IOException is caught and treated identically
to an end of stream condition. Subclasses can (and should if possible)
override this method to provide a more efficient implementation.
read
in class InputStream
dest
- The array into which the bytes read should be storedoffset
- The offset into the array to start storing byteslength
- The requested number of bytes to readIOException
- If an error occurs.protected void readBlocked(int waitingFor) throws IOException
waitingFor
- the number of bytes that is requested.IOException
public int bytesInBuffer()
public int available()
available
in class InputStream
public static Object[] pipe(int bufferSize)
bufferSize
- an optional maximum buffer size to use. If it is 0 or less then no
maximum is applied.public static void pipe2(InputStream[] inputs, OutputStream[] outputs, int bufferSize)
inputs
- an array of InputStreams of length 2.
It will be setup such
that data written to outputs[1] is read on inputs[0] and data written to
outputs[0] is read on inputs[1].outputs
- an array of InputStreams of length 2.
It will be setup such
that data written to outputs[1] is read on inputs[0] and data written to
outputs[0] is read on inputs[1].bufferSize
- an optional maximum buffer size to use. If it is 0 or less then no
maximum is applied.public InputStream getInputStream()