Skip to content

Instantly share code, notes, and snippets.

@ScottKaye
Last active March 15, 2017 01:54
Show Gist options
  • Save ScottKaye/662cbd2a6ec16f298ecd624bb09de60a to your computer and use it in GitHub Desktop.
Save ScottKaye/662cbd2a6ec16f298ecd624bb09de60a to your computer and use it in GitHub Desktop.
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