[英]Who should call ByteBuffer.flip()
I'm very well aware of when a ByteBuffer
needs to be flipped. 我非常了解ByteBuffer
何时需要翻转。 My question is pointed towards setting up an implicit contract between the put
-er and the get
-er to reduce bugs. 我的问题是指向在put
-er和get
-er之间建立隐式契约以减少错误。
Does the putter flip
before giving the buffer to the getter? 推杆在将缓冲区提供给吸气剂之前会flip
吗? Or does the getter flip
upon receiving the buffer? 还是在接收缓冲区时吸气剂flip
?
I lean towards the putter flipping the buffer because it makes sense that the getter always receives a buffer that is ready to get . 我倾向于推杆翻转缓冲区,因为有意义的是,getter始终会接收准备好获取的缓冲区。
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. 据我所知,最佳实践是将缓冲区保持在可读/可放置状态,仅在写入或获取之前立即执行flip()
,之后立即执行compact()
。 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. 注意, compact()
应该在finally
块中调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.