Last active
March 15, 2017 01:54
-
-
Save ScottKaye/662cbd2a6ec16f298ecd624bb09de60a to your computer and use it in GitHub Desktop.
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
void Main() | |
{ | |
var bytes = new byte[] { 0, 0, 1, 0, 255, 64 }; | |
var reader = new ByteReader(bytes); | |
reader.Read<int>().Dump(); | |
reader.Read<byte>().Dump(); | |
reader.Read<sbyte>().Dump(); | |
reader.Read<byte>(4).Dump(); | |
} | |
public unsafe class ByteReader | |
{ | |
private readonly byte[] _bytes; | |
public uint Position { get; private set; } = 0; | |
public ByteReader(byte[] bytes) | |
{ | |
if (bytes == null) | |
{ | |
throw new ArgumentNullException(nameof(bytes)); | |
} | |
_bytes = bytes; | |
} | |
// Move position by size of T | |
public void Move<T>() | |
where T : struct | |
=> Position += ((uint)Unsafe.SizeOf<T>()); | |
// Read sequentially | |
public T Read<T>() | |
where T : struct | |
{ | |
var value = Read<T>(Position); | |
Move<T>(); | |
return value; | |
} | |
// Read bytes from the start position + offset | |
public T Read<T>(uint offset) | |
where T : struct | |
{ | |
fixed (byte* b = &_bytes[0]) | |
{ | |
return Unsafe.Read<T>(b + offset); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment