R
- the type of response that this stream handler will deliverpublic interface DataStreamHandler<R>
This interface is used to handle the asynchronous streaming of files.
The callback is invoked on a background thread ant it may be invoked many times while the data is streamed
from the server.
While the content is downloaded the data byte array will never be null and will contain
new data received from the server.
When the streaming of the file ends, the callback will be invoked one more time with
a null data
byte array, that is an indication that the content has been completely
retrieved from the server.
The return value of the last call to #onData(byte[], int, long, String, String)
is what will be presented
to the handler, wrapped in a BaasResult
, if during any invocation an exception is thrown
it will be wrapped as a BaasResult.failure(BaasException)
.
This is an example of streaming a response to a String:
public class StreamToString implements DataStreamHandler<String>{
private ByteArrayOutputStream out = new ByteArrayOutputStream();
@Override
public String onData(byte[] data,int read,long contentLength,String id,String contentType) throws Exception{
if(data!=null){
out.write(data,0,read);
return null;
} else{
return out.toString("UTF-8");
}
}
}
Modifier and Type | Method and Description |
---|---|
R |
endData(java.lang.String id,
long contentLength,
java.lang.String contentType)
Method invoked when the whole data has been streamed
|
void |
finishStream(java.lang.String id) |
void |
onData(byte[] data,
int read)
Method invoked when new data is available.
|
void |
startData(java.lang.String id,
long contentLength,
java.lang.String contentType)
Method invoked right before data starts to stream.
|
void startData(java.lang.String id, long contentLength, java.lang.String contentType) throws java.lang.Exception
id
- the identifier to which this stream is bound tocontentLength
- the length of the bodycontentType
- the contentType of the responsejava.lang.Exception
void onData(byte[] data, int read) throws java.lang.Exception
data
- a byte[] array filled with new available data or null if there is no more available.read
- the number of actual bytes that can be read from data
java.lang.Exception
- any exception thrown will be wrapped in a BaasException
R endData(java.lang.String id, long contentLength, java.lang.String contentType) throws java.lang.Exception
id
- the identifier to which this stream is bound tocontentLength
- the length of the bodycontentType
- the contentType of the responsejava.lang.Exception
void finishStream(java.lang.String id)