}
public Buffer compact() {
checkReadonly();
Buffer buffer = BufferFactory.allocate(remaining());
batch(true, getIndex(0), buffer, buffer.capacity());
buffer.position(0);
int index = getIndex(0, 0);
Entry entry = getEntry(index);
int offset = index - entry.position;
do {
Buffer content = entry.buffer;
buffer.limit(Math.min(buffer.capacity(), buffer.position()
+ content.remaining() - offset));
content.put(content.position() + offset, buffer);
if (buffer.position() == buffer.capacity())
break;
offset = 0;
} while ((entry = entry.next) != header);