What is the best resizable circular byte buffer available in Java?

后端 未结 5 2003
一向
一向 2021-02-01 19:42

I need a byte buffer class in Java for single-threaded use. I should be able to insert data at the back of the buffer and read data at the front, with an amortized cost of O(1).

相关标签:
5条回答
  • 2021-02-01 20:00

    I have written such a class: ByteRingBuffer

    It does not resize automatically, but there is a resize() method.

    It's "well-tested" with an automatic test program, that uses random numbers to test all possible situations.

    0 讨论(0)
  • 2021-02-01 20:07

    Not sure if it is "the best", but you have a nice example of Circular Byte buffer here.

    Those Java Utilities - OstermillerUtils classes are under 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.

    0 讨论(0)
  • 2021-02-01 20:09

    I'm using a java.util.ArrayDeque<Byte> in a project with similar requirements. Note that you can easily change implementation using a java.util.concurrent Queue implementation.

    0 讨论(0)
  • 2021-02-01 20:10

    Another solution is to use GrowablePipedOutputStream and GrowablePipedInputStream by JBoss.

    0 讨论(0)
  • 2021-02-01 20:19

    I wonder if this one works well

    https://svn.apache.org/repos/asf/etch/releases/release-1.0.0/util/src/main/java/etch/util/CircularByteBuffer.java

    We will probably try this one since it is apache license.

    0 讨论(0)
提交回复
热议问题