Search in sources :

Example 31 with ByteBuffer

use of java.nio.ByteBuffer in project AndroidAsync by koush.

the class ByteBufferList method get.

public void get(ByteBufferList into, int length) {
    if (remaining() < length)
        throw new IllegalArgumentException("length");
    int offset = 0;
    while (offset < length) {
        ByteBuffer b = mBuffers.remove();
        int remaining = b.remaining();
        if (remaining == 0) {
            reclaim(b);
            continue;
        }
        if (offset + remaining > length) {
            int need = length - offset;
            // this is shared between both
            ByteBuffer subset = obtain(need);
            subset.limit(need);
            b.get(subset.array(), 0, need);
            into.add(subset);
            mBuffers.addFirst(b);
            assert subset.capacity() >= need;
            assert subset.position() == 0;
            break;
        } else {
            // this belongs to the new list
            into.add(b);
        }
        offset += remaining;
    }
    remaining -= length;
}
Also used : ByteBuffer(java.nio.ByteBuffer)

Example 32 with ByteBuffer

use of java.nio.ByteBuffer in project AndroidAsync by koush.

the class ByteBufferList method getAllByteArray.

public byte[] getAllByteArray() {
    // if that's what we're looking for. avoids allocation.
    if (mBuffers.size() == 1) {
        ByteBuffer peek = mBuffers.peek();
        if (peek.capacity() == remaining() && peek.isDirect()) {
            remaining = 0;
            return mBuffers.remove().array();
        }
    }
    byte[] ret = new byte[remaining()];
    get(ret);
    return ret;
}
Also used : ByteBuffer(java.nio.ByteBuffer)

Example 33 with ByteBuffer

use of java.nio.ByteBuffer in project AndroidAsync by koush.

the class ByteBufferList method obtain.

public static ByteBuffer obtain(int size) {
    if (size <= maxItem) {
        PriorityQueue<ByteBuffer> r = getReclaimed();
        if (r != null) {
            synchronized (LOCK) {
                while (r.size() > 0) {
                    ByteBuffer ret = r.remove();
                    if (r.size() == 0)
                        maxItem = 0;
                    currentSize -= ret.capacity();
                    assert r.size() != 0 ^ currentSize == 0;
                    if (ret.capacity() >= size) {
                        //                            System.out.println("using " + ret.capacity());
                        return ret;
                    }
                //                        System.out.println("dumping " + ret.capacity());
                }
            }
        }
    }
    //        System.out.println("alloc for " + size);
    ByteBuffer ret = ByteBuffer.allocate(Math.max(8192, size));
    return ret;
}
Also used : ByteBuffer(java.nio.ByteBuffer)

Example 34 with ByteBuffer

use of java.nio.ByteBuffer in project AndroidAsync by koush.

the class ByteBufferList method reclaim.

public static void reclaim(ByteBuffer b) {
    if (b == null || b.isDirect())
        return;
    if (b.arrayOffset() != 0 || b.array().length != b.capacity())
        return;
    if (b.capacity() < 8192)
        return;
    if (b.capacity() > MAX_ITEM_SIZE)
        return;
    PriorityQueue<ByteBuffer> r = getReclaimed();
    if (r == null)
        return;
    synchronized (LOCK) {
        while (currentSize > MAX_SIZE && r.size() > 0 && r.peek().capacity() < b.capacity()) {
            //                System.out.println("removing for better: " + b.capacity());
            ByteBuffer head = r.remove();
            currentSize -= head.capacity();
        }
        if (currentSize > MAX_SIZE) {
            //                System.out.println("too full: " + b.capacity());
            return;
        }
        assert !reclaimedContains(b);
        b.position(0);
        b.limit(b.capacity());
        currentSize += b.capacity();
        r.add(b);
        assert r.size() != 0 ^ currentSize == 0;
        maxItem = Math.max(maxItem, b.capacity());
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer)

Example 35 with ByteBuffer

use of java.nio.ByteBuffer in project AndroidAsync by koush.

the class LineEmitter method onDataAvailable.

@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
    ByteBuffer buffer = ByteBuffer.allocate(bb.remaining());
    while (bb.remaining() > 0) {
        byte b = bb.get();
        if (b == '\n') {
            assert mLineCallback != null;
            buffer.flip();
            data.add(buffer);
            mLineCallback.onStringAvailable(data.readString(charset));
            data = new ByteBufferList();
            return;
        } else {
            buffer.put(b);
        }
    }
    buffer.flip();
    data.add(buffer);
}
Also used : ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBuffer (java.nio.ByteBuffer)8895 Test (org.junit.Test)1965 IOException (java.io.IOException)1016 ArrayList (java.util.ArrayList)449 File (java.io.File)224 FileChannel (java.nio.channels.FileChannel)214 MappedByteBuffer (java.nio.MappedByteBuffer)195 HashMap (java.util.HashMap)177 CharBuffer (java.nio.CharBuffer)153 ByteArrayOutputStream (java.io.ByteArrayOutputStream)145 InetSocketAddress (java.net.InetSocketAddress)139 Random (java.util.Random)127 InputStream (java.io.InputStream)124 Map (java.util.Map)119 FileInputStream (java.io.FileInputStream)116 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)99 Test (org.testng.annotations.Test)96 IntBuffer (java.nio.IntBuffer)95 SocketChannel (java.nio.channels.SocketChannel)94 FileOutputStream (java.io.FileOutputStream)93