com.koushikdutta.async.parser.ByteBufferListParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of androidasync Show documentation
Show all versions of androidasync Show documentation
Asynchronous socket, http(s) (client+server) and websocket library for android. Based on nio, not threads.
package com.koushikdutta.async.parser;
import com.koushikdutta.async.ByteBufferList;
import com.koushikdutta.async.DataEmitter;
import com.koushikdutta.async.DataSink;
import com.koushikdutta.async.Util;
import com.koushikdutta.async.callback.CompletedCallback;
import com.koushikdutta.async.callback.DataCallback;
import com.koushikdutta.async.future.Future;
import com.koushikdutta.async.future.SimpleFuture;
/**
* Created by koush on 5/27/13.
*/
public class ByteBufferListParser implements AsyncParser {
@Override
public Future parse(final DataEmitter emitter) {
final ByteBufferList bb = new ByteBufferList();
final SimpleFuture ret = new SimpleFuture() {
@Override
protected void cancelCleanup() {
emitter.close();
}
};
emitter.setDataCallback(new DataCallback() {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList data) {
data.get(bb);
}
});
emitter.setEndCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
if (ex != null) {
ret.setComplete(ex);
return;
}
try {
ret.setComplete(bb);
}
catch (Exception e) {
ret.setComplete(e);
}
}
});
return ret;
}
@Override
public void write(DataSink sink, ByteBufferList value, CompletedCallback completed) {
Util.writeAll(sink, value, completed);
}
}