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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/325602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:57:44  来源:igfitidea点击:

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

javabufferbyte

提问by Wouter Lievens

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). The buffer should resize when it's full, rather than throw an exception or something.

我需要一个用于单线程使用的 Java 字节缓冲区类。我应该能够在缓冲区的后面插入数据并在前面读取数据,摊销成本为 O(1)。缓冲区应该在它满时调整大小,而不是抛出异常或其他东西。

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?

你会推荐什么?

采纳答案by VonC

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

不确定它是否是“最好的”,但是您在这里有一个很好的Circular Byte 缓冲区示例。

Those Java Utilities - OstermillerUtilsclasses 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.

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 InputStreams 和 OutputStreams 完成的。

使用此类是使用 PipedInputStream 和 PipedOutputStream 的一种更简单的替代方法。
PipedInputStreams 和 PipedOutputStreams 不支持标记操作,不允许您控制它们使用的缓冲区大小,并且具有更复杂的 API,需要实例化两个类并连接它们。

回答by Dean Hiller

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 许可证。

回答by dfa

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 Queueimplementation.

java.util.ArrayDeque<Byte>在具有类似要求的项目中使用了a。请注意,您可以使用 java.util.concurrentQueue实现轻松更改实现。

回答by Christian d'Heureuse

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.

它通过自动测试程序进行了“充分测试”,该程序使用随机数来测试所有可能的情况。

回答by Mohsen

Another solution is to use GrowablePipedOutputStreamand GrowablePipedInputStreamby JBoss.

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