Search code examples
javanio

How to flip a ByteBuffer relative to the mark?


Is there a built-in way, or alternatively, what is the cleanest way, to flip a java NIO ByteBuffer relative to the mark?

That is, flip() sets limit := position and then position := 0. I would like a flipToMark() routine that sets limit := position and then position := mark.


Solution

  • Flipping clears the mark, so unfortunately buffer.flip().reset() won't work.

    How about buffer.limit(buffer.position()).reset()?