简体   繁体   中英

How to update a byte buffer

I want to know if it's possible to update a byte buffer.

Say I have the below:

ByteBuffer buffer = ByteBuffer.allocate(56);
buffer.putInt(12);
buffer.putLong(34);    
buffer.put(byte('A'));    

Assuming I want to modify the buffer to say the that first int I put in should be 50, how do I do that.

I want something like:

public void updateByteBuffer(ByteBuffer, int position, int newValue){
  // logic to change buffer.putInt(12); to buffer.putInt(50);
  // So after this function, my ByteBuffer should contain(hex) 50,34 and 'A';
}

You could always just write buffer.putInt(0, 50) . That's the overload that accepts an index, as a byte offset, to indicate where to put the argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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