private void flushBuffer(ByteBuffer buffer, boolean force) throws IOException {
if (buffer.position()==0) return;
if (force || buffer.position()==buffer.limit()) {
buffer.limit(buffer.position());
buffer.position(0);
IntSet failedPositions = new TroveIntSet(100);
long time=System.currentTimeMillis();
while (buffer.position() != buffer.limit()) {
final int position = buffer.position();
if (!updateFromBuffer(buffer)) failedPositions.add(position);
}
if (log.isInfoEnabled()) log.info(String.format("flushed buffer %d, kept %d took %d ms.",idx(buffer),failedPositions.size(),System.currentTimeMillis()-time));
stats[idx(buffer)].written(buffer.position()/RECORD_SIZE - failedPositions.size());
buffer.limit(capacity);
int initialPos=failedPositions.isEmpty() ? 0 : copyFailedPositions(buffer, failedPositions);
buffer.position(initialPos);
}
}