C# equivalent of Java Memory mapping methods

969 views Asked by At

While translating a Java project to C#, i got stuck with the following piece:

RandomAccessFile raf = new RandomAccessFile(fileName, "r");
FileChannel channel = raf.getChannel();
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, length);

I'm not familiar with the memory mapping conception, I found a MemoryMappedFile class in C#, but don't know how to use it properly like in the Java code above (the MappedByteBuffer is used to obtain a large binary file, about 600-700MB).

Can anyone tell me how to translate the piece above properly?

1

There are 1 answers

1
plinth On BEST ANSWER
MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(fileName, FileMode.Read);
using (MemoryMappedViewStream vs = mmf.CreateViewStream()) {
    // perform stream operations
}

A MemoryMappedViewStream is a thin veneer onto the memory.