Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Growing ByteBuffer

Has anyone has ever seen an implementation of java.nio.ByteBuffer that will grow dynamically if a putX() call overruns the capacity?

The reason I want to do it this way is twofold:

  1. I don't know how much space I need ahead of time.
  2. I'd rather not do a new ByteBuffer.allocate() then a bulk put() every time I run out of space.
like image 571
Seth Avatar asked Nov 21 '09 05:11

Seth


People also ask

How do you make a ByteBuffer?

A ByteBuffer is created via the the two static factory methods: allocate(int) this will allocate a HeapByteBuffer with the capacity specified by the int argument. allocateDirect(int) this will allocate a DirectByteBuffer with the capacity specified by the int argument.

How do I get ByteBuffer length?

After you've written to the ByteBuffer, the number of bytes you've written can be found with the position() method. If you then flip() the buffer, the number of bytes in the buffer can be found with the limit() or remaining() methods.

How do I allocate ByteBuffer?

A new ByteBuffer can be allocated using the method allocate() in the class java. nio. ByteBuffer. This method requires a single parameter i.e. the capacity of the buffer.


2 Answers

In order for asynchronous I/O to work, you must have continuous memory. In C you can attempt to re-alloc an array, but in Java you must allocate new memory. You could write to a ByteArrayOutputStream, and then convert it to a ByteBuffer at the time you are ready to send it. The downside is you are copying memory, and one of the keys to efficient IO is reducing the number of times memory is copied.

like image 148
brianegge Avatar answered Oct 14 '22 16:10

brianegge


A ByteBuffer cannot really work this way, as its design concept is to be just a view of a specific array, which you may also have a direct reference to. It could not try to swap that array for a larger array without weirdness happening.

What you want to use is a DataOutput. The most convenient way is to use the (pre-release) Guava library:

ByteArrayDataOutput out = ByteStreams.newDataOutput(); out.write(someBytes); out.writeInt(someInt); // ... return out.toByteArray(); 

But you could also create a DataOutputStream from a ByteArrayOutputStream manually, and just deal with the spurious IOExceptions by chaining them into AssertionErrors.

like image 22
Kevin Bourrillion Avatar answered Oct 14 '22 15:10

Kevin Bourrillion