Search code examples
javanio

Querying java.nio.Buffer read/write (flip) state


It seems that there is no method to query a buffer whether it is in read or write state.

Getting the state would be useful in unit tests at least.

What is the best way to get the state?


Solution

  • There is no "read-state" or "write-state" of the Buffer. If the buffer supports it, it can be read and write at the same time.

    The flip() method simply: "The limit is set to the current position and then the position is set to zero. If the mark is defined then it is discarded."

    There is no internal flip state to be changed here. If you have a prepared buffer holding a thousand bytes, you can read a byte, then write a byte which will overwrite the 2nd...