use of com.koushikdutta.async.callback.WritableCallback in project AndroidAsync by koush.
the class BufferedDataSink method setDataSink.
public void setDataSink(DataSink datasink) {
mDataSink = datasink;
mDataSink.setWriteableCallback(new WritableCallback() {
@Override
public void onWriteable() {
writePending();
}
});
}
use of com.koushikdutta.async.callback.WritableCallback in project AndroidAsync by koush.
the class Util method writeAll.
public static void writeAll(final DataSink sink, final ByteBufferList bb, final CompletedCallback callback) {
WritableCallback wc;
sink.setWriteableCallback(wc = new WritableCallback() {
@Override
public void onWriteable() {
sink.write(bb);
if (bb.remaining() == 0 && callback != null) {
sink.setWriteableCallback(null);
callback.onCompleted(null);
}
}
});
wc.onWriteable();
}
use of com.koushikdutta.async.callback.WritableCallback in project AndroidAsync by koush.
the class Util method pump.
public static void pump(final DataEmitter emitter, final DataSink sink, final CompletedCallback callback) {
final DataCallback dataCallback = new DataCallback() {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
sink.write(bb);
if (bb.remaining() > 0)
emitter.pause();
}
};
emitter.setDataCallback(dataCallback);
sink.setWriteableCallback(new WritableCallback() {
@Override
public void onWriteable() {
emitter.resume();
}
});
final CompletedCallback wrapper = new CompletedCallback() {
boolean reported;
@Override
public void onCompleted(Exception ex) {
if (reported)
return;
reported = true;
emitter.setDataCallback(null);
emitter.setEndCallback(null);
sink.setClosedCallback(null);
sink.setWriteableCallback(null);
callback.onCompleted(ex);
}
};
emitter.setEndCallback(wrapper);
sink.setClosedCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
if (ex == null)
ex = new IOException("sink was closed before emitter ended");
wrapper.onCompleted(ex);
}
});
}
use of com.koushikdutta.async.callback.WritableCallback in project AndroidAsync by koush.
the class AsyncHttpServerResponseImpl method initFirstWrite.
void initFirstWrite() {
if (headWritten)
return;
headWritten = true;
final boolean isChunked;
String currentEncoding = mRawHeaders.get("Transfer-Encoding");
if ("".equals(currentEncoding))
mRawHeaders.removeAll("Transfer-Encoding");
boolean canUseChunked = ("Chunked".equalsIgnoreCase(currentEncoding) || currentEncoding == null) && !"close".equalsIgnoreCase(mRawHeaders.get("Connection"));
if (mContentLength < 0) {
String contentLength = mRawHeaders.get("Content-Length");
if (!TextUtils.isEmpty(contentLength))
mContentLength = Long.valueOf(contentLength);
}
if (mContentLength < 0 && canUseChunked) {
mRawHeaders.set("Transfer-Encoding", "Chunked");
isChunked = true;
} else {
isChunked = false;
}
String statusLine = String.format(Locale.ENGLISH, "%s %s %s", httpVersion, code, AsyncHttpServer.getResponseCodeDescription(code));
String rh = mRawHeaders.toPrefixString(statusLine);
Util.writeAll(mSocket, rh.getBytes(), ex -> {
if (ex != null) {
report(ex);
return;
}
if (isChunked) {
ChunkedOutputFilter chunked = new ChunkedOutputFilter(mSocket);
chunked.setMaxBuffer(0);
mSink = chunked;
} else {
mSink = mSocket;
}
mSink.setClosedCallback(closedCallback);
closedCallback = null;
mSink.setWriteableCallback(writable);
writable = null;
if (ended) {
// the response ended while headers were written
end();
return;
}
getServer().post(() -> {
WritableCallback wb = getWriteableCallback();
if (wb != null)
wb.onWriteable();
});
});
}
use of com.koushikdutta.async.callback.WritableCallback in project ion by koush.
the class RequestBodyUploadObserver method write.
@Override
public void write(AsyncHttpRequest request, final DataSink sink, final CompletedCallback completed) {
final int length = body.length();
body.write(request, new DataSink() {
int totalWritten;
@Override
public void write(ByteBufferList bb) {
int start = bb.remaining();
sink.write(bb);
int wrote = start - bb.remaining();
totalWritten += wrote;
callback.onProgress(totalWritten, length);
}
@Override
public void setWriteableCallback(WritableCallback handler) {
sink.setWriteableCallback(handler);
}
@Override
public WritableCallback getWriteableCallback() {
return sink.getWriteableCallback();
}
@Override
public boolean isOpen() {
return sink.isOpen();
}
@Override
public void end() {
sink.end();
}
@Override
public void setClosedCallback(CompletedCallback handler) {
sink.setClosedCallback(handler);
}
@Override
public CompletedCallback getClosedCallback() {
return sink.getClosedCallback();
}
@Override
public AsyncServer getServer() {
return sink.getServer();
}
}, completed);
}
Aggregations