Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ByteBuffer, what is a clean way to detect if it needs to be flipped

Is there a clean sure fire way to detect if a ByteBuffer needs flipping?

I have a ByteBuffer that is used for packing and unpacking a data stucture and also for storage of bytes to be packed / unpacked.

However, if a write operation has just been performed or a read operation has been performed or if a ByteBuffer was passed to my code I cannot guarantee that the buffer is in write mode or read mode with out manually manipulating position and limit.

Partial reads and writes also present add to this problem. I have been adopting the convention that the buffer is left in write mode and a duplicate is created and flip'ed for read purposes. When a buffer is passed in to my code is is manually inspected and position and limit are set as they would be in write mode. Surely there is a better way.

like image 931
JeffV Avatar asked Nov 26 '09 01:11

JeffV


People also ask

What is flip ByteBuffer?

flip. public ByteBuffer flip​() Flips this buffer. The limit is set to the current position and then the position is set to zero. If the mark is defined then it is discarded.

What does ByteBuffer wrap do?

wrap. Wraps a byte array into a buffer. The new buffer will be backed by the given byte array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity will be array.

What is ByteBuffer limit?

ByteBuffer limit() methods in Java with Examples nio. ByteBuffer Class is used to set this buffer's limit. If the position is larger than the new limit then it is set to the new limit. If the mark is defined and larger than the new limit then it is discarded.

How do I find my ByteBuffer size?

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.


2 Answers

Encapsulate it in an object - something like the following:

class FlippingByteBuffer {
  ByteBuffer buffer;
  boolean isInUse;
  boolean/Enum mode; //Is it read or write?
}

Then you can use it in your code:

//I'm about to read
FlippingByteBuffer fbb =...;
fbb.beginReading();
doSomething(fbb.getByteBuffer());
fbb.finishReading();

This way you know when something is happening. You could even start using Locks to ensure that the reading and writing block each others etc.

If there's something in the middle, (you're calling something and it's calling back to you), you could put your FlippingByteBuffer into a map and/or put it in a ThreadLocal (ewww!), or make FlippingByteBuffer implement ByteBuffer. That might be able to automatically switch between modes (using the read and write methods) depending on what you're doing.

like image 163
Stephen Avatar answered Oct 04 '22 15:10

Stephen


Just as an idea, how about wrapping the ByteBuffer in another class? That class could update and maintain the state you want whenever you perform read or write operations. You may also want to implement locking or some other mechanism to make it thread-safe.

like image 38
Andy West Avatar answered Oct 04 '22 16:10

Andy West