int doDrain(Consumer consumer) { int drainedCount = 0; int drainedSizeInBytes = 0; while (drainedCount < count) { byte[] next = elements[readPos]; if (next == null) break; if (consumer.accept(next)) { drainedCount++; drainedSizeInBytes += next.length; elements[readPos] = null; if (++readPos == elements.length) readPos = 0; // circle back to the front of the array } else { break; } } count -= drainedCount; sizeInBytes -= drainedSizeInBytes; return drainedCount; } }