T
- wrapped output stream.public abstract class StreamHandler<T extends OutputStream> extends Object
On server side upload just declare the method using desired input stream as parameters; additional parameters are
allowed. For download need to wrap returned stream into this stream handler - see downloadStream()
from
code sample.
@Remote interface Controller { void uploadStream(InputStream inputStream) throws IOException; StreamHandler<OutputStream> downloadStream(); }
class ControllerImpl implements Controller { public void uploadStream(InputStream inputStream) throws IOException { StringWriter writer = new StringWriter(); Files.copy(new InputStreamReader(inputStream, "UTF-8"), writer); } public StreamHandler<OutputStream> downloadStream() { return new StreamHandler<OutputStream>(OutputStream.class) { @Override public void handle(OutputStream outputStream) throws IOException { outputStream.write("output stream".getBytes("UTF-8")); } }; } }
Client side logic uses service provider interface, see Controller
interface from below code snippet.
Client interfaces should reverse streams declared by server interface used for implementation. In our
example, server uploadStream
method has input stream whereas client interface has output
stream, wrapped in stream handler instance. For downloadStream
, client replaces server output
stream with input and because is an input stream there is no need to use stream handler.
// service provider interface reverse streams direction interface Controller { void uploadStream(StreamHandler<FileOutputStream> outputStream) throws IOException; InputStream downloadStream(); }
Controller controller = Factory.getInstance(controllerURL, Controller.class); // wrap output stream into StreamHandler instance controller.uploadStream(new StreamHandler<FileOutputStream>(FileOutputStream.class) { @Override protected void handle(FileOutputStream outputStream) throws IOException { outputStream.write("output stream".getBytes("UTF-8")); } }); InputStream inputStream = controller.downloadStream(); Files.read(inputStream, outputStream); // just uses input stream the usual way
Modifier and Type | Field and Description |
---|---|
private Class<T> |
streamClass
Wrapped output stream class.
|
Constructor and Description |
---|
StreamHandler(Class<T> streamClass)
Construct concrete stream handler instance wrapping given output stream.
|
Modifier and Type | Method and Description |
---|---|
Class<T> |
getStreamClass()
Get wrapped output stream class.
|
protected abstract void |
handle(T outputStream)
Concrete stream handler should implement this method, usually to writing to given output stream.
|
void |
invokeHandler(OutputStream outputStream)
Helper method used to invoke concrete output stream handler method.
|
private Class<T extends OutputStream> streamClass
protected abstract void handle(T outputStream) throws IOException
outputStream
- output stream instance.IOException
- if output stream writing fails.public Class<T> getStreamClass()
public void invokeHandler(OutputStream outputStream) throws IOException
outputStream
- wrapped output stream instance.IOException
- any output stream exception is bubbled up.Copyright © 2018. All rights reserved.