简体   繁体   English

Java中可用的最佳可调整大小的循环字节缓冲区是什么?

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

I need a byte buffer class in Java for single-threaded use. 我需要Java中的字节缓冲类来实现单线程使用。 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). 我应该能够在缓冲区的后面插入数据并在前面读取数据,摊销成本为O(1)。 The buffer should resize when it's full, rather than throw an exception or something. 缓冲区应该在它满时调整大小,而不是抛出异常或其他东西。

I could write one myself, but I'd be very surprised if this didn't exist yet in a standard Java package, and if it doesn't, I'd expect it to exist in some well-tested public library. 我自己可以写一个,但如果在标准的Java包中不存在,我会感到非常惊讶,如果没有,我希望它存在于一些经过良好测试的公共库中。

What would you recommend? 你会推荐什么?

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 . 那些Java Utilities - OstermillerUtils类属于GPL许可

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. 使用标准Java InputStreams和OutputStreams完成缓冲区的填充和清空。

Using this class is a simpler alternative to using a PipedInputStream and a PipedOutputStream. 使用此类是使用PipedInputStream和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. PipedInputStreams和PipedOutputStreams不支持标记操作,不允许您控制它们使用的缓冲区大小,并且具有更复杂的API,需要实例化两个类并连接它们。

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 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. 我们可能会尝试这个,因为它是apache许可证。

I'm using a java.util.ArrayDeque<Byte> in a project with similar requirements. 我在具有类似要求的项目中使用java.util.ArrayDeque<Byte> Note that you can easily change implementation using a java.util.concurrent Queue implementation. 请注意,您可以使用java.util.concurrent Queue实现轻松更改实现。

I have written such a class: ByteRingBuffer 我写了这样一个类: ByteRingBuffer

It does not resize automatically, but there is a resize() method. 它不会自动调整大小,但有一个resize()方法。

It's "well-tested" with an automatic test program, that uses random numbers to test all possible situations. 它使用自动测试程序进行了“充分测试”,该程序使用随机数来测试所有可能的情况。

另一个解决方案是使用JBoss的GrowablePipedOutputStreamGrowablePipedInputStream

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

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