-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reader.cs
98 lines (86 loc) · 2.42 KB
/
Reader.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System.Text;
namespace MyApp
{
class Reader
{
byte[] data;
int offset;
public Reader(byte[] data)
{
this.data = data;
this.offset = 0;
}
public int Remaining()
{
return data.Length - offset;
}
public void Skip(int bytes)
{
offset += bytes;
}
public byte ReadByte()
{
return data[offset++];
}
public byte[] ReadBytes(int count)
{
var dest = new byte[count];
Array.Copy(data, offset, dest, 0, count);
offset += count;
return dest;
}
public string ReadString()
{
int len = 0;
for (; len < data.Length; len++)
{
if (data[offset + len] == 0) break;
}
string str = Encoding.ASCII.GetString(data, offset, len);
offset += len + 1;
return str;
}
public string ReadUTF8String()
{
int len = 0;
for (; len < data.Length; len++)
{
if (data[offset + len] == 0) break;
}
string str = Encoding.UTF8.GetString(data, offset, len);
offset += len + 1;
return str;
}
public short ReadShort()
{
short value = (short)(data[offset] + (data[offset + 1] << 8));
offset += 2;
return value;
}
public ushort ReadUShortBE()
{
ushort value = (ushort)(data[offset + 1] + (data[offset] << 8));
offset += 2;
return value;
}
public long ReadLong()
{
long value = (long)(data[offset] + (data[offset + 1] << 8) + (data[offset + 2] << 16) + (data[offset + 3] << 24)
+ (data[offset + 4] << 32) + (data[offset + 5] << 40) + (data[offset + 6] << 48) + (data[offset + 7] << 56));
offset += 8;
return value;
}
public int ReadInt()
{
int value = (int)(data[offset] + (data[offset + 1] << 8) + (data[offset + 2] << 16) + (data[offset + 3] << 24));
offset += 4;
return value;
}
internal float readFloat()
{
float value = BitConverter.ToSingle(data, offset);
offset += 4;
return value;
}
}
}