Sunday, 16 June 2019

NIO: MappedByteBuffer

MappeByterBuffer used to map a file to memory.

How to create MappedByteBuffer?
Using 'map' method of FileChannel class, you can create MappedByteBuffer.
FileChannel channel = FileChannel.open(Paths.get(FILE_PATH), READ);
MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

When can I use MappedByteBuffer?
a.   In your application, if you are trying to read contents of file multiple time, then instead of reading every time, load the contents of the file to memory via MappedByteBuffer.

If you are working with large files, then MappedByteBuffer may not be the right solution (Since file content may not fit into available memory).

b.   With memory-mapped file, we can act like that the entire file is in memory and can treat like one large array.

Reading contents of file using MappedByteBuffer?
Step 1: Create FileChannel in READ mode.
FileChannel channel = FileChannel.open(Paths.get(FILE_PATH), READ);

Step 2: Create 'MappedByteBuffer' instance by calling map() method of FileChannel object.
MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

Step 3: Decode the bytes in MappedByteBuffer to CharBuffer.
CharBuffer charBuffer = StandardCharsets.UTF_8.decode(mappedByteBuffer);

Step 4: Close the channel.
channel.close();

Test.java
package com.sample.app;

import static java.nio.file.StandardOpenOption.READ;

import java.io.IOException;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;

public class Test {

 private static final String FILE_PATH = "/Users/krishna/Documents/demo.txt";

 public static void main(String... args) throws IOException {

  FileChannel channel = FileChannel.open(Paths.get(FILE_PATH), READ);

  MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

  CharBuffer charBuffer = StandardCharsets.UTF_8.decode(mappedByteBuffer);

  System.out.println(charBuffer);
  channel.close();

 }
}


Writing to the file using MappedByteBuffer
demo.txt
Hello Krishna,
How are you?
I am fine, What about you
Thank you so much

Step 1: Create FileChannel object using READ and WRITE modes.
FileChannel channel = FileChannel.open(Paths.get(FILE_PATH), READ, WRITE);

Step 2: Create MappedByteBuffer using READ_WRITE map mode.
MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, channel.size());

Step 3: Create CharBuffer and write the content of charBuffer to mappedByteBuffer.
CharBuffer charBuffer = CharBuffer.wrap("Write this content to the file");

if (mappedByteBuffer != null) {
         mappedByteBuffer.put(Charset.forName("utf-8").encode(charBuffer));
}

Step 4: Close the channel.
channel.close();

Test.java
package com.sample.app;

import static java.nio.file.StandardOpenOption.READ;
import static java.nio.file.StandardOpenOption.WRITE;

import java.io.IOException;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.file.Paths;

public class Test {

 private static final String FILE_PATH = "/Users/krishna/Documents/demo.txt";

 public static void main(String... args) throws IOException {

  FileChannel channel = FileChannel.open(Paths.get(FILE_PATH), READ, WRITE);

  MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, channel.size());

  CharBuffer charBuffer = CharBuffer.wrap("Write this content to the file");

  if (mappedByteBuffer != null) {
   mappedByteBuffer.put(Charset.forName("utf-8").encode(charBuffer));
  }

  channel.close();

 }
}


Run Test.java and open demo.txt, you can see below content


demo.txt
Write this content to the fileam fine, What about you
Thank you so much

MemoryMappedBuffer modes
There are three modes supported by MappedByteBuffer
a. READ_ONLY : Mode for a read-only mapping.
b. READ_WRITE : Mode for a read/write mapping. When you modify MappedByteBuffer, same is reflected in underlying file.
c. PRIVATE : Mode for a private (copy-on-write) mapping. Any changes that you made to MappedByteBuffer as not reflected in underlying file.

Previous                                                 Next                                                 Home

No comments:

Post a Comment