20

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.

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.

What would you recommend?

Wouter Lievens
  • 4,019
  • 5
  • 41
  • 66
  • It has a private resize method that you can easily adapt to your dynamic resize need, or an 'infinite' mode which means it will always grow. – VonC Nov 28 '08 at 12:13

5 Answers5

9

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • That's not bad. It's not resizable, and it's GPL so you have to watch out for licensing issues but it would be a good start. – paxdiablo Nov 28 '08 at 12:10
  • Nice link (I found this too on google) but I can't use it, I absolutely need it to be nonblocking,therefore resizable. +1, but not answered :-) – Wouter Lievens Nov 28 '08 at 12:11
  • It has a private resize method that you can easily adapt to your dynamic resize need. – VonC Nov 28 '08 at 12:12
  • @Wouter, it will always be blocking at some point (if you can't allocate more memory for the bytes), so you'd have to code for that anyway. – paxdiablo Nov 28 '08 at 12:13
  • @Pax: you mean when you're out of memory? That line of thinking would go for any resizable container. Or am I misunderstanding? – Wouter Lievens Nov 28 '08 at 12:15
  • Not misunderstanding. I'm saying, since you have to code for a limit of some sort, it makes little difference between 100, 10,000 or the extent of memory (in terms of coding effort). Although I understand that resizable gives the best tradeoff between initial size and maximum capability. – paxdiablo Nov 28 '08 at 13:43
  • has anyone used the OstermillerUtils for bytebuffer? Please give some feedback. – Jus12 May 19 '11 at 10:26
3

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.

Dean Hiller
  • 19,235
  • 25
  • 129
  • 212
2

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.

Ricket
  • 33,368
  • 30
  • 112
  • 143
dfa
  • 114,442
  • 31
  • 189
  • 228
  • 9
    You might find that using Byte instead of byte is a significant perform hit. ;) – Peter Lawrey Oct 14 '09 at 07:26
  • This is a very inefficient byte buffer, not only because the overhead in not using primitives. Try something backed by a byte[] instead. – Martin May 31 '17 at 11:44
1

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.

Christian d'Heureuse
  • 5,090
  • 1
  • 32
  • 28
0

Another solution is to use GrowablePipedOutputStream and GrowablePipedInputStream by JBoss.

Mohsen
  • 3,512
  • 3
  • 38
  • 66