1

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

1 Answer 1

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

A MemoryMappedViewStream is a thin veneer onto the memory.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but how do I write a buffer to a file then (with FileMode.Create, of course)? For example I have a function packPrunTable(table, buffer, FILE_SIZE), and I want the buffer to be mapped to file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.