Can you tell me where the situation(position >= buffer.size()) occurs? It seems that *position* is usually incremented after invoking ensure().