-
Notifications
You must be signed in to change notification settings - Fork 0
/
ByteBuffer.cs
312 lines (286 loc) · 5.93 KB
/
ByteBuffer.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using System.IO;
public class ByteBuffer
{
//'Mode' is only used to determine whether to return data length or capacity from the 'limit' method:
private enum Mode
{
Read,
Write
}
private Mode mode;
private MemoryStream stream;
private BinaryReader reader;
private BinaryWriter writer;
private ByteBuffer()
{
stream = new MemoryStream();
reader = new BinaryReader(stream);
writer = new BinaryWriter(stream);
}
~ByteBuffer()
{
reader.Close();
writer.Close();
stream.Close();
stream.Dispose();
}
public static ByteBuffer allocate(int capacity)
{
ByteBuffer buffer = new ByteBuffer();
buffer.stream.Capacity = capacity;
buffer.mode = Mode.Write;
return buffer;
}
public static ByteBuffer allocateDirect(int capacity)
{
//this wrapper class makes no distinction between 'allocate' & 'allocateDirect'
return allocate(capacity);
}
public int capacity()
{
return stream.Capacity;
}
public ByteBuffer flip()
{
mode = Mode.Read;
stream.SetLength(stream.Position);
stream.Position = 0;
return this;
}
public ByteBuffer clear()
{
mode = Mode.Write;
stream.Position = 0;
return this;
}
public ByteBuffer compact()
{
mode = Mode.Write;
MemoryStream newStream = new MemoryStream(stream.Capacity);
stream.CopyTo(newStream);
stream = newStream;
return this;
}
public ByteBuffer rewind()
{
stream.Position = 0;
return this;
}
public long limit()
{
if (mode == Mode.Write)
return stream.Capacity;
else
return stream.Length;
}
public long position()
{
return stream.Position;
}
public ByteBuffer position(long newPosition)
{
stream.Position = newPosition;
return this;
}
public long remaining()
{
return this.limit() - this.position();
}
public bool hasRemaining()
{
return this.remaining() > 0;
}
public int get()
{
return stream.ReadByte();
}
public ByteBuffer get(byte[] dst, int offset, int length)
{
stream.Read(dst, offset, length);
return this;
}
public ByteBuffer put(byte b)
{
stream.WriteByte(b);
return this;
}
public ByteBuffer put(byte[] src, int offset, int length)
{
stream.Write(src, offset, length);
return this;
}
public bool Equals(ByteBuffer other)
{
if (other != null && this.remaining() == other.remaining())
{
long thisOriginalPosition = this.position();
long otherOriginalPosition = other.position();
bool differenceFound = false;
while (stream.Position < stream.Length)
{
if (this.get() != other.get())
{
differenceFound = true;
break;
}
}
this.position(thisOriginalPosition);
other.position(otherOriginalPosition);
return ! differenceFound;
}
else
return false;
}
//methods using the internal BinaryReader:
public char getChar()
{
return reader.ReadChar();
}
public char getChar(int index)
{
long originalPosition = stream.Position;
stream.Position = index;
char value = reader.ReadChar();
stream.Position = originalPosition;
return value;
}
public double getDouble()
{
return reader.ReadDouble();
}
public double getDouble(int index)
{
long originalPosition = stream.Position;
stream.Position = index;
double value = reader.ReadDouble();
stream.Position = originalPosition;
return value;
}
public float getFloat()
{
return reader.ReadSingle();
}
public float getFloat(int index)
{
long originalPosition = stream.Position;
stream.Position = index;
float value = reader.ReadSingle();
stream.Position = originalPosition;
return value;
}
public int getInt()
{
return reader.ReadInt32();
}
public int getInt(int index)
{
long originalPosition = stream.Position;
stream.Position = index;
int value = reader.ReadInt32();
stream.Position = originalPosition;
return value;
}
public long getLong()
{
return reader.ReadInt64();
}
public long getLong(int index)
{
long originalPosition = stream.Position;
stream.Position = index;
long value = reader.ReadInt64();
stream.Position = originalPosition;
return value;
}
public short getShort()
{
return reader.ReadInt16();
}
public short getShort(int index)
{
long originalPosition = stream.Position;
stream.Position = index;
short value = reader.ReadInt16();
stream.Position = originalPosition;
return value;
}
//methods using the internal BinaryWriter:
public ByteBuffer putChar(char value)
{
writer.Write(value);
return this;
}
public ByteBuffer putChar(int index, char value)
{
long originalPosition = stream.Position;
stream.Position = index;
writer.Write(value);
stream.Position = originalPosition;
return this;
}
public ByteBuffer putDouble(double value)
{
writer.Write(value);
return this;
}
public ByteBuffer putDouble(int index, double value)
{
long originalPosition = stream.Position;
stream.Position = index;
writer.Write(value);
stream.Position = originalPosition;
return this;
}
public ByteBuffer putFloat(float value)
{
writer.Write(value);
return this;
}
public ByteBuffer putFloat(int index, float value)
{
long originalPosition = stream.Position;
stream.Position = index;
writer.Write(value);
stream.Position = originalPosition;
return this;
}
public ByteBuffer putInt(int value)
{
writer.Write(value);
return this;
}
public ByteBuffer putInt(int index, int value)
{
long originalPosition = stream.Position;
stream.Position = index;
writer.Write(value);
stream.Position = originalPosition;
return this;
}
public ByteBuffer putLong(long value)
{
writer.Write(value);
return this;
}
public ByteBuffer putLong(int index, long value)
{
long originalPosition = stream.Position;
stream.Position = index;
writer.Write(value);
stream.Position = originalPosition;
return this;
}
public ByteBuffer putShort(short value)
{
writer.Write(value);
return this;
}
public ByteBuffer putShort(int index, short value)
{
long originalPosition = stream.Position;
stream.Position = index;
writer.Write(value);
stream.Position = originalPosition;
return this;
}
}