Java内存映射大文件
Java 将 MappedByteBuffer 限制为 2GIG,这使得用于映射大文件变得很棘手。通常推荐的方法是使用 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[]))。
问题:对于此类场景,您的最佳实践是什么?您知道可以重复使用而无需重新发明轮子的工作解决方案/代码吗?
The Java limitation of MappedByteBuffer to 2GIG make it tricky to use for mapping big files. The usual recommended approach is to use an array of MappedByteBuffer and index it through:
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));
}
this can be a working for single bytes, but requires rewriting a lot of code if you want to handle read/writes that are bigger and require crossing boundaries (getLong() or get(byte[])).
The question: what is your best practice for these kind of scenarios, do you know any working solution/code that can be re-used without re-inventing the wheel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您检查过 dsiutil 的
ByteBufferInputStream
?Javadoc
此类的主要用处是可以创建真正基于
MappedByteBuffer
的输入流。特别是,工厂方法
map(FileChannel, FileChannel.MapMode)
会将整个文件内存映射到ByteBuffer
数组中,并将该数组公开为 ByteBufferInputStream。这使得可以轻松访问大于 2GiB 的映射文件。long length()
longposition()
voidposition(long newPosition)
这是您想到的吗? 也是 LGPL。
Have you checked out dsiutil's
ByteBufferInputStream
?Javadoc
The main usefulness of this class is that of making it possible creating input streams that are really based on a
MappedByteBuffer
.In particular, the factory method
map(FileChannel, FileChannel.MapMode)
will memory-map an entire file into an array ofByteBuffer
and expose the array as a ByteBufferInputStream. This makes it possible to access easily mapped files larger than 2GiB.long length()
long position()
void position(long newPosition)
Is that something you were thinking of? It's LGPL too.