use of com.koushikdutta.async.util.Allocator in project AndroidAsync by koush.
the class AsyncNetworkSocket method attach.
void attach(DatagramChannel channel) throws IOException {
mChannel = new DatagramChannelWrapper(channel);
// keep udp at roughly the mtu, which is 1540 or something
// letting it grow freaks out nio apparently.
allocator = new Allocator(8192);
}
use of com.koushikdutta.async.util.Allocator in project AndroidAsync by koush.
the class AsyncNetworkSocket method attach.
void attach(SocketChannel channel, InetSocketAddress socketAddress) throws IOException {
this.socketAddress = socketAddress;
allocator = new Allocator();
mChannel = new SocketChannelWrapper(channel);
}
use of com.koushikdutta.async.util.Allocator in project AndroidAsync by koush.
the class Util method pump.
public static void pump(final InputStream is, final long max, final DataSink ds, final CompletedCallback callback) {
final CompletedCallback wrapper = new CompletedCallback() {
boolean reported;
@Override
public void onCompleted(Exception ex) {
if (reported)
return;
reported = true;
callback.onCompleted(ex);
}
};
final WritableCallback cb = new WritableCallback() {
int totalRead = 0;
private void cleanup() {
ds.setClosedCallback(null);
ds.setWriteableCallback(null);
pending.recycle();
StreamUtility.closeQuietly(is);
}
ByteBufferList pending = new ByteBufferList();
Allocator allocator = new Allocator().setMinAlloc((int) Math.min(2 << 19, max));
@Override
public void onWriteable() {
try {
do {
if (!pending.hasRemaining()) {
ByteBuffer b = allocator.allocate();
long toRead = Math.min(max - totalRead, b.capacity());
int read = is.read(b.array(), 0, (int) toRead);
if (read == -1 || totalRead == max) {
cleanup();
wrapper.onCompleted(null);
return;
}
allocator.track(read);
totalRead += read;
b.position(0);
b.limit(read);
pending.add(b);
}
ds.write(pending);
} while (!pending.hasRemaining());
} catch (Exception e) {
cleanup();
wrapper.onCompleted(e);
}
}
};
ds.setWriteableCallback(cb);
ds.setClosedCallback(wrapper);
cb.onWriteable();
}
Aggregations