|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.io.InputStream
com.gc.iotools.stream.is.InputStreamFromOutputStream<T>
T - Optional result returned by the function
#produce(OutputStream)) after the data has been
written. It can be obtained calling the getResult()public abstract class InputStreamFromOutputStream<T>
This class allow to read the data written to an OutputStream from an InputStream.
To use this class you must subclass it and implement the abstract method
#produce(OutputStream)). The data who is produced inside this
function can be written to the OutputStream passed as a
parameter. Later it can be read back from from the
InputStreamFromOutputStream class (whose ancestor is
java.io.InputStream ).
final String dataId=//id of some data.
final InputStreamFromOutputStream<String> isos
= new InputStreamFromOutputStream<String>() {
@Override
public String produce(final OutputStream dataSink) throws Exception {
//call your application function who produces the data here
//WARNING: we're in another thread here, so this method shouldn't
//write any class field or make assumptions on the state of the class.
return produceMydata(dataId,dataSink)
}
};
try {
//now you can read from the InputStream the data that was written to the
//dataSink OutputStream
byte[] readed=IOUtils.toByteArray(isos);
//Use data here
} catch (final IOException e) {
//Handle exception here
} finally {
isos.close();
}
//You can get the result of produceMyData after the stream has been closed.
String resultOfProduction = isos.getResult();
This class encapsulates a pipe and a Thread, hiding the
complexity of using them.
ExecutionModel| Constructor Summary | |
|---|---|
InputStreamFromOutputStream()
It creates a InputStreamFromOutputStream with a
THREAD_PER_INSTANCE thread strategy. |
|
InputStreamFromOutputStream(ExecutionModel executionModel)
It creates a InputStreamFromOutputStream and let the user
set the ExecutionModel he likes. |
|
InputStreamFromOutputStream(ExecutorService executor)
|
|
| Method Summary | |
|---|---|
void |
close()
|
static String[] |
getActiveThreadNames()
|
T |
getResult()
Returns the object that was |
protected abstract T |
produce(OutputStream dataSink)
This method must be implemented by the user of this class to produce the data that must be read from the external InputStream. |
int |
read()
|
int |
read(byte[] b,
int off,
int len)
|
static void |
setDefaultBufferSize(int defaultPipeSize)
Set the size for the pipe circular buffer for the newly created InputStreamFromOutputStream. |
| Methods inherited from class java.io.InputStream |
|---|
available, mark, markSupported, read, reset, skip |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public InputStreamFromOutputStream()
It creates a InputStreamFromOutputStream with a
THREAD_PER_INSTANCE thread strategy.
ExecutionModel.THREAD_PER_INSTANCEpublic InputStreamFromOutputStream(ExecutionModel executionModel)
It creates a InputStreamFromOutputStream and let the user
set the ExecutionModel he likes.
executionModel - The preferred ExecutionModel.ExecutionModelpublic InputStreamFromOutputStream(ExecutorService executor)
| Method Detail |
|---|
public static final String[] getActiveThreadNames()
public static void setDefaultBufferSize(int defaultPipeSize)
InputStreamFromOutputStream. Default is 1024 bytes.
defaultPipeSize - the default pipe buffer size in bytes.
public final void close()
throws IOException
close in interface Closeableclose in class InputStreamIOException
public T getResult()
throws Exception
Returns the object that was
This method must be called after the method close(),
otherwise an IllegalStateException is thrown. It waits for the method
produce(OutputStream) to terminate and returns the result
produced here.
Exception - If the produce(OutputStream) method threw an
Exception this method will throw again the same exception.
public final int read()
throws IOException
read in class InputStreamIOException
public final int read(byte[] b,
int off,
int len)
throws IOException
read in class InputStreamIOException
protected abstract T produce(OutputStream dataSink)
throws Exception
This method must be implemented by the user of this class to produce the
data that must be read from the external InputStream.
Special care must be paid passing arguments to this method or setting global fields because it is executed in another thread.
The right way to set a field variable is to return a value in the
produceand retrieve it in the getResult().
dataSink - the implementing class should write data to this stream.
Exception - the exception eventually thrown by the implementing class is
returned by the read() methods.getResult()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||