簡體   English   中英

如何更新字節緩沖區

[英]How to update a byte buffer

我想知道是否可以更新字節緩沖區。

說我有以下內容:

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

假設我要修改緩沖區以說我輸入的第一個int應該是50,那么我該怎么做。

我想要類似的東西:

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';
}

您總是可以只寫buffer.putInt(0, 50) 這就是重載,它接受索引作為字節偏移量,以指示將參數放在何處。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM