我需要一个 Java 中的字节缓冲区类以供单线程使用。我应该能够在缓冲区的后面插入数据并在前面读取数据,摊销成本为 O(1)。缓冲区满时应该调整大小,而不是抛出异常或其他东西。
我可以自己编写一个,但如果标准 Java 包中尚不存在此功能,我会感到非常惊讶,如果不存在,我希望它存在于一些经过良好测试的公共(public)库中.
你会推荐什么?
最佳答案
不确定它是否是“最好的”,但你有一个很好的例子 Circular Byte buffer here .
那些Java Utilities - OstermillerUtils类(class)在 GPL license 下.
This Circular Byte Buffer implements the circular buffer producer/consumer model for bytes. Filling and emptying the buffer is done with standard Java InputStreams and OutputStreams.
Using this class is a simpler alternative to using a PipedInputStream and a PipedOutputStream.
PipedInputStreams and PipedOutputStreams don't support the mark operation, don't allow you to control buffer sizes that they use, and have a more complicated API that requires a instantiating two classes and connecting them.
关于java - Java 中最好的可调整大小的循环字节缓冲区是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/325602/