-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFastBigEndianReader.cs
More file actions
285 lines (234 loc) · 6.93 KB
/
FastBigEndianReader.cs
File metadata and controls
285 lines (234 loc) · 6.93 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
using System;
using System.IO;
using System.Text;
/// <summary>
/// Much faster reader that only reads memory buffer
/// </summary>
internal unsafe class FastBigEndianReader : IDataReader, IDisposable
{
private long position = 0;
private readonly byte[] buffer;
public byte[] Buffer
{
get { return buffer; }
}
public FastBigEndianReader(byte[] buffer)
{
this.buffer = buffer;
}
public byte ReadByte()
{
fixed (byte* pbyte = &buffer[position++])
{
return *pbyte;
}
}
public sbyte ReadSByte()
{
fixed (byte* pbyte = &buffer[position++])
{
return (sbyte)*pbyte;
}
}
public long Position
{
get { return position; }
}
public long BytesAvailable
{
get { return buffer.Length - position; }
}
public short ReadShort()
{
var position = this.position;
this.position += 2;
fixed (byte* pbyte = &buffer[position])
{
return (short) ((*pbyte << 8) | (*(pbyte + 1)));
}
}
public int ReadInt()
{
var position = this.position;
this.position += 4;
fixed (byte* pbyte = &buffer[position])
{
return ( *pbyte << 24 ) | ( *( pbyte + 1 ) << 16 ) | ( *( pbyte + 2 ) << 8 ) | ( *( pbyte + 3 ) );
}
}
public long ReadLong()
{
var position = this.position;
this.position += 8;
fixed (byte* pbyte = &buffer[position])
{
int i1 = ( *pbyte << 24 ) | ( *( pbyte + 1 ) << 16 ) | ( *( pbyte + 2 ) << 8 ) | ( *( pbyte + 3 ) );
int i2 = ( *( pbyte + 4 ) << 24 ) | ( *( pbyte + 5 ) << 16 ) | ( *( pbyte + 6 ) << 8 ) | ( *( pbyte + 7 ) );
return (uint)i2 | ( (long)i1 << 32 );
}
}
public ushort ReadUShort()
{
return (ushort)ReadShort();
}
public uint ReadUInt()
{
return (uint)ReadInt();
}
public ulong ReadULong()
{
return (ulong)ReadLong();
}
public byte[] ReadBytes(int n)
{
var dst = new byte[n];
fixed (byte* pSrc = &buffer[position], pDst = dst)
{
byte* ps = pSrc;
byte* pd = pDst;
// Loop over the count in blocks of 4 bytes, copying an integer (4 bytes) at a time:
for (int i = 0; i < n / 4; i++)
{
*( (int*)pd ) = *( (int*)ps );
pd += 4;
ps += 4;
}
// Complete the copy by moving any bytes that weren't moved in blocks of 4:
for (int i = 0; i < n % 4; i++)
{
*pd = *ps;
pd++;
ps++;
}
}
position += n;
return dst;
}
public bool ReadBoolean()
{
return ReadByte() != 0;
}
public char ReadChar()
{
return (char)ReadShort();
}
public float ReadFloat()
{
int val = ReadInt();
return *(float*)&val;
}
public double ReadDouble()
{
long val = ReadLong();
return *(double*)&val;
}
public string ReadUTF()
{
ushort length = ReadUShort();
byte[] bytes = ReadBytes(length);
return Encoding.UTF8.GetString(bytes);
}
public string ReadUTFBytes(ushort len)
{
byte[] bytes = ReadBytes(len);
return Encoding.UTF8.GetString(bytes);
}
#region Custom Methods
private const int INT_SIZE = 32;
private const int SHORT_SIZE = 16;
private const int SHORT_MIN_VALUE = -32768;
private const int SHORT_MAX_VALUE = 32767;
private const int UNSIGNED_SHORT_MAX_VALUE = 65536;
private const int CHUNCK_BIT_SIZE = 7;
private const int MASK_1 = 128;
private const int MASK_0 = 127;
public int ReadVarInt()
{
var local_4 = 0;
var local_1 = 0;
var local_2 = 0;
var local_3 = false;
while (local_2 < INT_SIZE)
{
local_4 = ReadByte();
local_3 = (local_4 & MASK_1) == MASK_1;
if (local_2 > 0)
{
local_1 += ((local_4 & MASK_1) << local_2);
}
else
{
local_1 += (local_4 & MASK_0);
}
local_2 += CHUNCK_BIT_SIZE;
if (!local_3)
{
return local_1;
}
}
throw new System.Exception("Too much data");
}
public uint ReadVarUhInt()
{
return (uint)(ReadVarInt());
}
public int ReadVarShort()
{
var local_4 = 0;
var local_1 = 0;
var local_2 = 0;
var local_3 = false;
while (local_2 < SHORT_SIZE)
{
local_4 = ReadByte();
local_3 = (local_4 & MASK_1) == MASK_1;
if (local_2 > 0)
{
local_1 += ((local_4 & MASK_1) << local_2);
}
else
{
local_1 += (local_4 & MASK_0);
}
local_2 += CHUNCK_BIT_SIZE;
if (!local_3)
{
if (local_1 > SHORT_MAX_VALUE)
{
local_1 = local_1 - UNSIGNED_SHORT_MAX_VALUE;
}
return local_1;
}
}
throw new System.Exception("Too much data");
}
public uint ReadVarUhShort()
{
return (uint)(ReadVarShort());
}
public double ReadVarLong()
{
return ReadInt();
}
public double ReadVarUhLong()
{
return ReadUInt();
}
#endregion
public void Seek(int offset, SeekOrigin seekOrigin)
{
if (seekOrigin == SeekOrigin.Begin)
position = offset;
else if (seekOrigin == SeekOrigin.End)
position = buffer.Length + offset;
else if (seekOrigin == SeekOrigin.Current)
position += offset;
}
public void SkipBytes(int n)
{
position += n;
}
public void Dispose()
{
}
}