-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryReaderMC.cs
66 lines (52 loc) · 1.52 KB
/
BinaryReaderMC.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace MCLib
{
/// <summary>
/// BinaryReader that automatically converts to little endian if necessary
/// </summary>
public class BinaryReaderMC : BinaryReader
{
#region Constructor
public BinaryReaderMC(Stream input)
: base(input, Encoding.UTF8)
{
}
#endregion
#region Overrides
public override short ReadInt16()
{
return BitConverter.ToInt16(ToLittleEndian(ReadBytes(2)), 0);
}
public override int ReadInt32()
{
return BitConverter.ToInt32(ToLittleEndian(ReadBytes(4)), 0);
}
public override long ReadInt64()
{
return BitConverter.ToInt64(ToLittleEndian(ReadBytes(8)), 0);
}
public override float ReadSingle()
{
return BitConverter.ToSingle(ToLittleEndian(ReadBytes(4)), 0);
}
public override double ReadDouble()
{
return BitConverter.ToDouble(ToLittleEndian(ReadBytes(8)), 0);
}
public override string ReadString()
{
return Encoding.UTF8.GetString(ReadBytes(ReadInt16()));
}
#endregion
#region Helpers
private static byte[] ToLittleEndian(IEnumerable<byte> bytes)
{
return BitConverter.IsLittleEndian ? bytes.Reverse().ToArray() : bytes.ToArray();
}
#endregion
}
}