|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectewe.util.Errorable
ewe.io.BasicStreamObject
ewe.io.StreamObject
ewe.io.MemoryStream
A MemoryStream is a Stream that stores all data written to it using standard Stream write() calls, in an array of bytes and then allows that data to be read out again, usually through standard Stream read() calls. Essentially a memory pipe.
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.
| Field Summary | |
protected Lock |
backgroundLock
|
protected boolean |
readOnly
Set this true to allow for read() calls only. |
protected boolean |
useBackgroundThread
Set this true to allow the getMoreData() method to start and control a background thread for retrieving data for reading. |
protected boolean |
writeOnly
Set this true to allow for write() calls only. |
| Fields inherited from class ewe.io.StreamObject |
napIterations, napTime, READWRITE_CLOSED, READWRITE_ERROR, READWRITE_WOULDBLOCK |
| Fields inherited from class ewe.io.BasicStreamObject |
closed |
| Fields inherited from class ewe.util.Errorable |
error |
| Constructor Summary | |
MemoryStream()
|
|
MemoryStream(boolean useBackgroundThread)
Create a read-only MemoryStream that may or may not use a background thread for retrieving data for reading. |
|
MemoryStream(boolean useBackgroundThread,
boolean forReading)
Create a uni-directional MemoryStream that may or may not use a background thread for retrieving data for reading/writing. |
|
MemoryStream(int maxCapacity)
|
|
| Method Summary | |
boolean |
closeStream()
This is non-blocking. |
boolean |
flushStream()
This is non-blocking. |
int |
generateError(String message)
Cause an error to be generated on the Stream. |
protected byte[] |
getFromBuffer()
Get a all the bytes from the internal data buffer. |
protected int |
getFromBuffer(byte[] buff,
int start,
int count)
Get a number of bytes from the internal data buffer. |
protected void |
getMoreData()
Override this method if you are implementing a read-only Stream, but are not using a background thread to do so. |
protected boolean |
loadAndPutDataBlock()
Override this method if you are using a background thread to get data from a source to place in the stream's buffer for reading. |
protected void |
noMoreDataToTake()
If this is a write-only Stream, this method is called when the Stream has been closed and there will be no more data to take. |
int |
nonBlockingRead(byte[] buff,
int start,
int count)
This is the non-blocking read operation. |
int |
nonBlockingWrite(byte[] buff,
int start,
int count)
This is the non-blocking write operation. |
static Object[] |
pipe()
Create a MemoryStream and return an InputStream and an OutputStream for it. |
static Stream[] |
pipe2()
Create two Streams that represent either end of a memory based communication stream. |
protected void |
putInBuffer(byte[] buff,
int start,
int count)
Use this to put data into the buffer, for later reading out. |
void |
run()
|
protected int |
sizeOfBuffer()
|
protected boolean |
takeAndUseDataBlock()
Override this method if you are using a background thread to use data written into the buffer place in the stream's buffer for reading. |
protected void |
useMoreData()
Override this method if you are implementing a wite-only Stream, but are not using a background thread to do so. |
| Methods inherited from class ewe.io.StreamObject |
close, doRead, doWrite, flush, isOpen, nap, nap, read, readAByte, readBytes, readBytes, write, writeAByte, writeBytes, writeBytes |
| Methods inherited from class ewe.io.BasicStreamObject |
getException, getName, read, read, throwIOException, toInputStream, toOutputStream, toStream, write, write |
| Methods inherited from class ewe.util.Errorable |
returnError, returnError, returnError |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, toString |
| Field Detail |
protected boolean readOnly
protected boolean writeOnly
protected boolean useBackgroundThread
protected Lock backgroundLock
| Constructor Detail |
public MemoryStream()
public MemoryStream(int maxCapacity)
public MemoryStream(boolean useBackgroundThread)
useBackgroundThread - if this is true then a background thread will be used
to load data for reading. In that case the loadAndPutDataBlock() method must be overridden.
public MemoryStream(boolean useBackgroundThread,
boolean forReading)
useBackgroundThread - if this is true then a background thread will be used
to load data for reading or to accept data that's been written.
In that case the loadAndPutDataBlock() method OR takeAndUseDataBlock() method must be overridden.forReading - true for read-only stream, false for a write-only stream.| Method Detail |
public boolean flushStream()
throws IOException
BasicStream
flushStream in interface BasicStreamIOExceptionpublic int generateError(String message)
protected boolean loadAndPutDataBlock()
throws IOException
IOException - if an error occured retrieving the data.
protected boolean takeAndUseDataBlock()
throws IOException
IOException - if an error occured retrieving the data.public void run()
run in interface Runnableprotected void getMoreData()
protected void useMoreData()
This method should be non-blocking. If necessary start a new thread to take the data out of the buffer so that this thread can return. The default implmentation of this will generate an error ifthis Stream is marked as writeOnly.
protected void noMoreDataToTake()
protected int sizeOfBuffer()
protected byte[] getFromBuffer()
throws IOException
IOException - if the Stream has been flagged with an error.
protected int getFromBuffer(byte[] buff,
int start,
int count)
throws IOException
IOException - if the Stream has been flagged with an error.
public int nonBlockingRead(byte[] buff,
int start,
int count)
StreamObjectThe default StreamObject implementation of this uses the single byte read() method.
nonBlockingRead in interface BasicStreamnonBlockingRead in class StreamObjectbuff - Destination byte array to hold incoming data.start - Starting index in buff for incoming data.count - Maximum number of bytes to read - should never be zero.
protected void putInBuffer(byte[] buff,
int start,
int count)
throws IOException
IOException
public int nonBlockingWrite(byte[] buff,
int start,
int count)
StreamObjectThe default StreamObject implementation of this uses the single byte write() method.
nonBlockingWrite in interface BasicStreamnonBlockingWrite in class StreamObjectbuff - Source byte array holding data to be written.start - Starting index in buff for data to be written.count - Number of bytes to write - should never be zero.
public boolean closeStream()
throws IOException
BasicStream
closeStream in interface BasicStreamcloseStream in class StreamObjectIOExceptionpublic static Object[] pipe()
public static Stream[] pipe2()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||