Java nio: How to read characters from memory mapped file with correct charset

孤街醉人 提交于 2020-01-04 01:57:08

问题


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.



来源:https://stackoverflow.com/questions/26740370/java-nio-how-to-read-characters-from-memory-mapped-file-with-correct-charset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!