public synchronized boolean enqueue(ByteBuffer byteBuffer) { if (byteBuffer.remaining() == 0) { return false; } if (queue.size() > 0) { ByteBuffer tail = queue.getLast(); if (tail.hasRemaining()) { topUpBuffer(tail, byteBuffer); } } while (byteBuffer.hasRemaining()) { ByteBuffer newBuf = bufferFactory.newBuffer(); topUpBuffer(newBuf, byteBuffer); queue.addLast(newBuf); } facade.modifyInterestOps(SelectionKey.OP_WRITE, 0); return true; }
public synchronized int drainTo(ByteChannel channel) throws IOException { int bytesWritten = 0; while (true) { if (active == null) { if (queue.size() == 0) break; active = queue.removeFirst(); active.flip(); } int rc = channel.write(active); bytesWritten += rc; if (!active.hasRemaining()) { bufferFactory.returnBuffer(active); active = null; } if (rc == 0) break; } return bytesWritten; }