简体   繁体   English

如何更新字节缓冲区

[英]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. 假设我要修改缓冲区以说我输入的第一个int应该是50,那么我该怎么做。

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) . 您总是可以只写buffer.putInt(0, 50) That's the overload that accepts an index, as a byte offset, to indicate where to put the argument. 这就是重载,它接受索引作为字节偏移量,以指示将参数放在何处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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