简体   繁体   English

内存映射文件java NIO

[英]Memory mapped file java NIO

I understand how to create a memory mapped file, but my question is let's say that in the following line: 我理解如何创建一个内存映射文件,但我的问题是让我们说在以下行:

FileChannel roChannel = new RandomAccessFile(file, "r").getChannel();
ByteBuffer roBuf = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, SIZE);

Where i set SIZE to be 2MB for example, does this means that it will only load 2MB of the file or will it read further in the file and update the buffer as i consume bytes from it? 例如,我将SIZE设置为2MB,这是否意味着它只会加载2MB的文件,还是会在文件中进一步读取并更新缓冲区,因为我从中消耗了字节数?

Where i set SIZE to be 2MB for example, does this means that it will only load 2MB of the file or will it read further in the file and update the buffer as i consume bytes from it? 例如,我将SIZE设置为2MB,这是否意味着它只会加载2MB的文件,还是会在文件中进一步读取并更新缓冲区,因为我从中消耗了字节数?

It will only load the portion of the file specified in your buffer initialization. 它只会加载缓冲区初始化中指定的文件部分。 If you want it to read further you'll need to have some sort of read loop. 如果你想进一步阅读,你需要有一些读取循环。 While I would not go as far as saying this is tricky, if one isn't 100% familiar with the java.io and java.nio APIs involved then the chances of stuffing it up are high. 虽然我不会说这很棘手,如果一个人不熟悉所涉及的java.io和java.nio API,那么填充它的可能性很高。 (Eg: not flipping the buffer; buffer/file edge case mistakes). (例如:不翻转缓冲区;缓冲区/文件边缘错误)。

If you are looking for an easy approach to accessing this file in a ByteBuffer, consider using a MappedByteBuffer . 如果您正在寻找一种在ByteBuffer中访问此文件的简单方法,请考虑使用MappedByteBuffer

RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel fc = raf.getChannel();
MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

The nice thing about a using an MBB in this context is that it won't necessarily actually load the entire buffer into memory, but rather only the parts you are accessing. 在这种情况下使用MBB的好处是,它不一定实际将整个缓冲区加载到内存中,而只是加载您正在访问的部分。

The size of the buffer is the size you pass in. It will not grow or shrink. 缓冲区的大小是您传入的大小。它不会增长或缩小。

The javadoc says: javadoc说:

Maps a region of this channel's file directly into memory. 将此频道文件的某个区域直接映射到内存中。

... ...

size - The size of the region to be mapped; size - 要映射的区域的大小; must be non-negative and no greater than Integer.MAX_VALUE 必须是非负数且不大于Integer.MAX_VALUE

EDIT: 编辑:

Depending on what you mean by "updated with new data", the answer is yes. 根据“更新新数据”的含义,答案是肯定的。

The view of a file provided by an instance of this class is guaranteed to be consistent with other views of the same file provided by other instances in the same program. 由此类的实例提供的文件视图保证与同一程序中其他实例提供的同一文件的其他视图一致 The view provided by an instance of this class may or may not, however, be consistent with the views seen by other concurrently-running programs due to caching performed by the underlying operating system and delays induced by network-filesystem protocols. 然而,由于底层操作系统执行的高速缓存和网络文件系统协议引起的延迟,该类实例提供的视图可能会也可能不会与其他同时运行的程序所看到的视图一致。 This is true regardless of the language in which these other programs are written, and whether they are running on the same machine or on some other machine. 无论这些其他程序的编写语言是什么,以及它们是在同一台机器上运行还是在其他机器上运行,都是如此。 The exact nature of any such inconsistencies are system-dependent and are therefore unspecified. 任何此类不一致的确切性质都取决于系统,因此未指定。

So, other systems may do caching, but when those caches are flushed or otherwise up-to-date, they will agree with the view presented by the FileChannel . 因此,其他系统可能会进行缓存,但是当这些缓存被刷新或以其他方式更新时,它们将同意FileChannel提供的视图。

You can also use explicit calls to the position method and other methods to change what is presented by the view. 您还可以使用显式调用position方法和其他方法来更改视图显示的内容。

Changing the channel's position, whether explicitly or by reading or writing bytes, will change the file position of the originating object, and vice versa. 无论是显式地还是通过读取或写入字节来改变通道的位置,都将改变原始对象的文件位置,反之亦然。 Changing the file's length via the file channel will change the length seen via the originating object, and vice versa. 通过文件通道更改文件长度将改变通过原始对象看到的长度,反之亦然。 Changing the file's content by writing bytes will change the content seen by the originating object, and vice versa. 通过写入字节来更改文件的内容将更改原始对象看到的内容,反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM