[英]Java MemoryMapping big files
MappedByteBuffer對2GIG的Java限制使得用於映射大文件變得棘手。 通常推薦的方法是使用MappedByteBuffer數組並通過以下方式對其進行索引:
long PAGE_SIZE = Integer.MAX_VALUE;
MappedByteBuffer[] buffers;
private int getPage(long offset) {
return (int) (offset / PAGE_SIZE)
}
private int getIndex(long offset) {
return (int) (offset % PAGE_SIZE);
}
public byte get(long offset) {
return buffers[getPage(offset)].get(getIndex(offset));
}
這可能適用於單個字節,但如果要處理更大且需要跨越邊界的讀/寫(getLong()或get(byte [])),則需要重寫大量代碼。
問題是:對於這些場景,你最好的做法是什么,你知道任何可以在不重新發明輪子的情況下重復使用的工作解決方案/代碼嗎?
你檢查過dsiutil的ByteBufferInputStream
嗎?
這個類的主要用途是可以創建真正基於MappedByteBuffer
輸入流。
特別是,工廠方法map(FileChannel, FileChannel.MapMode)
將整個文件內存映射到ByteBuffer
數組中,並將該數組公開為ByteBufferInputStream。 這使得可以訪問大於2GiB的容易映射的文件。
long length()
long position()
void position(long newPosition)
那是你想到的嗎? 這也是LGPL 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.