简体   繁体   中英

Who should call ByteBuffer.flip()

I'm very well aware of when a ByteBuffer needs to be flipped. My question is pointed towards setting up an implicit contract between the put -er and the get -er to reduce bugs.

Does the putter flip before giving the buffer to the getter? Or does the getter flip upon receiving the buffer?

I lean towards the putter flipping the buffer because it makes sense that the getter always receives a buffer that is ready to get .

Is there any precedent? What do you do?

The best practice as far as I can see is to leave the buffer in readable/puttable state, and to only flip() immediately before a write or get, and compact() immediately afterwards. The writer/getter should do that itself, and not rely on anyone else doing it for him. Note that compact() should be called in a finally block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM