Skip to content

Instantly share code, notes, and snippets.

@bcalmac
Last active April 28, 2016 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcalmac/19c5b6990fa4573fd4264fdc893221c5 to your computer and use it in GitHub Desktop.
Save bcalmac/19c5b6990fa4573fd4264fdc893221c5 to your computer and use it in GitHub Desktop.
Write and then read back a memory mapped file
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import static org.junit.Assert.assertEquals;
public class MemoryMappedFileTest {
@Rule
public TemporaryFolder folder = new TemporaryFolder();
/** Write some values to a memory mapped file and then read them back. */
@Test
public void writeAndReadBack() throws IOException {
try (FileChannel fileChannel = new RandomAccessFile(folder.newFile(), "rw").getChannel()) {
int itemCount = 512;
int itemSize = 8;
MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, itemCount * itemSize);
for (long l = 0; l < itemCount; l++) {
assertEquals((itemCount - l) * itemSize, buffer.remaining());
buffer.putLong(l);
}
assertEquals(0, buffer.remaining());
buffer.rewind();
for (long l = 0; l < itemCount; l++) {
assertEquals((itemCount - l) * itemSize, buffer.remaining());
assertEquals(l, buffer.getLong());
}
assertEquals(0, buffer.remaining());
}
}
/** Prove that the buffer is valid even after closing the channel. */
@Test
public void closeChannelBeforeUsingTheBuffer() throws IOException {
int itemCount = 512;
int itemSize = 8;
File file = folder.newFile();
FileChannel writeChannel = new RandomAccessFile(file, "rw").getChannel();
MappedByteBuffer writeBuffer = writeChannel.map(FileChannel.MapMode.READ_WRITE, 0, itemCount * itemSize);
writeChannel.close();
for (long l = 0; l < itemCount; l++) {
assertEquals((itemCount - l) * itemSize, writeBuffer.remaining());
writeBuffer.putLong(l);
}
assertEquals(0, writeBuffer.remaining());
FileChannel readChannel = new RandomAccessFile(file, "r").getChannel();
assertEquals(itemCount * itemSize, readChannel.size());
MappedByteBuffer readBuffer = readChannel.map(FileChannel.MapMode.READ_ONLY, 0, itemCount * itemSize);
readChannel.close();
for (long l = 0; l < itemCount; l++) {
assertEquals((itemCount - l) * itemSize, readBuffer.remaining());
assertEquals(l, readBuffer.getLong());
}
assertEquals(0, readBuffer.remaining());
}
/** Prove that the buffer is initialized with zeros */
@Test
public void readUninitializedData() throws IOException {
try (FileChannel fileChannel = new RandomAccessFile(folder.newFile(), "rw").getChannel()) {
MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 8);
assertEquals(0, buffer.getLong());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment