Java nio: How to read characters from memory mappe

2019-07-06 05:15发布

问题:

for a new project, I have to read the characters of a file (with configurable encoding) to handle the input. As some of these files can be quite large (> 100MB), I wanted to check out the Java nio's ability to memory map files for faster access.

However, I was not able to figure out, how I am able to create something "Reader"-like to read from the MappedByteBuffer with the correct charset decoding.

To create the MappedByteBuffer, I currently use:

    RandomAccessFile raFile = new RandomAccessFile("myFile.bla", "r");
    FileChannel channel = raFile.getChannel();
    MappedByteBuffer mappedByteBuffer = channel.map(MapMode.READ_ONLY, 0, channel.size());

I know, that I can use getChar() to get a character from the MappedByteBuffer, but how is it possible to specify the encoding? In the javadoc it states, that always two bytes are read and combined to one char, but what is with ASCII encoded files?

I also found the Channels.newReader(...) methods, which however can only handle the channel, not the memory mapped file. Is there something similar for the MappedByteBuffer?

Just to make sure: I know that memory mapping is a somewhat expensive operation and therefore only useful for larger files. I made no decision (yet), whether to use it or not, but want to evaluate it for my special use case.

Many thanks in advance + best regards, Andreas

回答1:

You can use a CharsetDecoder retrieved from your favorite Charset with Charset#newDecoder().

StandardCharsets.UTF_8.newDecoder().decode(mappedByteBuffer)

This returns a CharBuffer from which you can get char values.

Note that this does consume the full MappedByteBuffer. If you only want a few bytes, construct a new ByteBuffer from the few bytes of the original MappedByteBuffer and decode that.