简体   繁体   English

在 Java 中使用 ByteBuffer 处理交错读/写的最佳方法是什么?

[英]What is the best way of using ByteBuffer to handle interleaved read/write in Java?

Edited: to make the question less 'opinion-based'编辑:使问题不那么“基于意见”

I want to ask for the suggestible way of using bytebuffer in the case of interleaved read/write.我想询问在交错读/写的情况下使用字节缓冲区的建议方式。

My program processes a random sequence of NIO that performs read() and write() operation to a ByteBuffer .我的程序处理一个随机的 NIO 序列,对ByteBuffer执行read()write()操作。 Currently I use a variable to record whether the last operation was read() or write() , and call flip() or compact() accordingly.目前我使用一个变量来记录最后一个操作是read()还是write() ,并相应地调用flip()compact()

Is this the best approach or ByteBuffer is not meant to handle this task?这是最好的方法还是 ByteBuffer 不是用来处理这个任务的?


Original question title: Is Java ByteBuffer poorly designed?原问题标题:Java ByteBuffer 设计不好?

I just started with Java NIO and I think the Bytebuffer is designed in a way that makes interleaved read/write operations difficult to achieve.我刚从 Java NIO 开始,我认为 Bytebuffer 的设计方式使交错的读/写操作难以实现。 Where such operation pattern is exactly the reason why we have a "buffer" in NIO.这样的操作模式正是我们在 NIO 中有一个“缓冲区”的原因。

Specifically, the flip() and compact() is unnecessary details for programming.具体来说, flip()compact()是不必要的编程细节。 If I have a random sequence of read() and write() to a bytebuffer, then I have not choice but to keep a separate variable to track whether the buffer has been 'flipped'.如果我有一个随机序列的read()write()到字节缓冲区,那么我别无选择,只能保留一个单独的变量来跟踪缓冲区是否已“翻转”。 For example, for a flipped buffer I need to compact() it before write again, but I cannot do so for an unflipped buffer.例如,对于翻转的缓冲区,我需要在再次write之前对其进行compact() ,但对于未翻转的缓冲区,我不能这样做。

IMO, there is almost no case that a write() following read() do no need to call compact() or clear() first. IMO,几乎没有情况下read() ) 之后的write()不需要先调用compact()clear() Also, no read() after write() is without flip() , so I think either should be done:此外, write() read()没有flip() ,所以我认为应该这样做:

  • make 'read mode' and 'write mode' explicit and throw warning if write in read mode or vice versa.明确“读取模式”和“写入模式”并在读取模式下写入时抛出警告,反之亦然。 Use a explicit readMode() or writeMode() command to switch.使用显式readMode()writeMode()命令进行切换。
  • remove the need to flip/compact before read/write by either adopt a different design (eg circular buffer) or by making it implicit in read/write.通过采用不同的设计(例如循环缓冲区)或通过使其隐含在读/写中来消除读/写之前翻转/压缩的需要。

As I am still quite new to Java, I want to know if my complaint is legit or there is some careful design choice going on in implementing Bytebuffer?由于我对 Java 还是很陌生,我想知道我的投诉是否合法,或者在实施 Bytebuffer 时是否有一些谨慎的设计选择?

Many thanks to user207421 for answering.非常感谢 user207421 的回答。 He proposed a wonderful solution which I am adopting in my design now.他提出了一个很好的解决方案,我现在在我的设计中采用了这个解决方案。

Basically, you always keep the ByteBuffer in the same state after each operation.基本上,每次操作后,您始终将ByteBuffer保持在相同的 state 中。

For example, to keep the ByteBuffer in unflipped state, then every write() operation should be wrapped with flip() and compact() before and after.例如,要将ByteBuffer保持在未翻转的 state 中,则每个write()操作都应在前后用flip()compact()包装。 Also, Add synchronization to deal with threads.另外,添加同步以处理线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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