簡體   English   中英

擴展Java內存映射字節緩沖區

[英]Expanding Java Memory-Mapped Byte Buffer

有沒有辦法擴展Java內存映射字節緩沖區,以便新的大小反映回磁盤上的映射文件?

不,您需要調整底層文件的大小並重新創建Memory Mapped Byte Buffer。

RandomAccessFile file = new RandomAccessFile(/* some file */);
MappedByteBuffer buffer = file.getChannel().map(MapMode.READ_WRITE, 0, file.length());

// Some stuff happens...

// adjust the size
file.setLength(newLength);

// recreate the memory mapped buffer
buffer = file.getChannel().map(MapMode.READ_WRITE, 0, file.length());

注意:設置文件長度有一些奇怪的行為。 如果您通過地圖在超出文件末尾的特定位置(使用map.position()或map.putX(position,...))寫入文件,則值將附加到結尾該文件並沒有寫在您期望的位置(至少在linux上)。 如果這是不希望的行為,您需要將數據附加到文件以真正增長文件。

暫無
暫無

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

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