+ m_totalAvailable + " bytes; call tryRead() first");
}
int bytesCopied = 0;
while (bytesCopied < output.length) {
BBContainer first = m_readBuffers.peekFirst();
if (first == null) {
// Steal the write buffer
m_writeBuffer.b.flip();
m_readBuffers.add(m_writeBuffer);
first = m_writeBuffer;
m_writeBuffer = null;
}
assert first.b.remaining() > 0;
// Copy bytes from first into output
int bytesRemaining = first.b.remaining();
int bytesToCopy = output.length - bytesCopied;
if (bytesToCopy > bytesRemaining) bytesToCopy = bytesRemaining;
first.b.get(output, bytesCopied, bytesToCopy);
bytesCopied += bytesToCopy;
m_totalAvailable -= bytesToCopy;
if (first.b.remaining() == 0) {
// read an entire block: move it to the empty buffers list
m_readBuffers.poll();
first.discard();
}
}
if (bytesCopied > 0) {
m_globalAvailable.addAndGet(0 - bytesCopied);