What is the purpose of ByteBuffer's flip method? (And why is it called “flip”?)

后端 未结 4 535
眼角桃花
眼角桃花 2020-12-07 13:12

Why does ByteBuffer\'s flip() method called \"flip\"? What is \"flipped\" here? According to apidoc, two successive flips won\'t restore original state, and multiple flips w

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-07 13:38

    ByteBuffer is ill designed. There are lots of complaints from decent programmers.

    So don't try to reason about it, just study and use the API carefully.

    Now I cannot badmouth it without presenting an alternative, so here it is:

    A buffer has a fixed capacity; it maintains 2 pointers: start and end. get() returns the byte at the start position and increments start. put() puts the byte at the end position and increments end. No flip()!

提交回复
热议问题