Java内存映射大文件

发布于 2024-11-01 17:38:52 字数 566 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放赐 2024-11-08 17:38:52

您检查过 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 of ByteBuffer 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文