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;
}
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;
}
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;
}
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());
}
}
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);
}
Aggregations