Created
January 22, 2016 23:02
-
-
Save outro56/5e5c1f7c5e9c0460ed0f to your computer and use it in GitHub Desktop.
quickly read bytes from a memory mapped file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copied from | |
// http://stackoverflow.com/questions/7956167/how-can-i-quickly-read-bytes-from-a-memory-mapped-file-in-net | |
public static MemoryMapppedIOExtensions | |
{ | |
public unsafe byte[] ReadBytesFast(this MemoryMappedViewAccessor view, int offset, int num) | |
{ | |
try | |
{ | |
byte[] arr = new byte[num]; | |
byte *ptr = (byte*)0; | |
_view.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr); | |
Marshal.Copy(IntPtr.Add(new IntPtr(ptr), offset), arr, 0, num); | |
} | |
finally | |
{ | |
_view.SafeMemoryMappedViewHandle.ReleasePointer(); | |
} | |
return arr; | |
} | |
public unsafe void WriteBytesFast(this MemoryMappedViewAccessor view, int offset, byte[] data) | |
{ | |
try | |
{ | |
byte* ptr = (byte*)0; | |
_view.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr); | |
Marshal.Copy(data, 0, IntPtr.Add(new IntPtr(ptr), offset), data.Length); | |
} | |
finally | |
{ | |
_view.SafeMemoryMappedViewHandle.ReleasePointer(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment