Search in sources :

Example 1 with Allocator

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);
}
Also used : Allocator(com.koushikdutta.async.util.Allocator)

Example 2 with Allocator

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);
}
Also used : Allocator(com.koushikdutta.async.util.Allocator)

Example 3 with Allocator

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();
}
Also used : Allocator(com.koushikdutta.async.util.Allocator) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) WritableCallback(com.koushikdutta.async.callback.WritableCallback) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException)

Aggregations

Allocator (com.koushikdutta.async.util.Allocator)3 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)1 WritableCallback (com.koushikdutta.async.callback.WritableCallback)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1