This repository has been archived by the owner on Dec 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 153
/
ByteBuffer.cs
executable file
·221 lines (187 loc) · 3.46 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
namespace Sharpen
{
using System;
public class ByteBuffer
{
private byte[] buffer;
private DataConverter c;
private int capacity;
private int index;
private int limit;
private int mark;
private int offset;
private ByteOrder order;
public ByteBuffer ()
{
this.c = DataConverter.BigEndian;
}
private ByteBuffer (byte[] buf, int start, int len)
{
this.buffer = buf;
this.offset = 0;
this.limit = start + len;
this.index = start;
this.mark = start;
this.capacity = buf.Length;
this.c = DataConverter.BigEndian;
}
public static ByteBuffer Allocate (int size)
{
return new ByteBuffer (new byte[size], 0, size);
}
public static ByteBuffer AllocateDirect (int size)
{
return Allocate (size);
}
public byte[] Array ()
{
return buffer;
}
public int ArrayOffset ()
{
return offset;
}
public int Capacity ()
{
return capacity;
}
private void CheckGetLimit (int inc)
{
if ((index + inc) > limit) {
throw new BufferUnderflowException ();
}
}
private void CheckPutLimit (int inc)
{
if ((index + inc) > limit) {
throw new BufferUnderflowException ();
}
}
public void Clear ()
{
index = offset;
limit = offset + capacity;
}
public void Flip ()
{
limit = index;
index = offset;
}
public byte Get ()
{
CheckGetLimit (1);
return buffer[index++];
}
public void Get (byte[] data)
{
Get (data, 0, data.Length);
}
public void Get (byte[] data, int start, int len)
{
CheckGetLimit (len);
for (int i = 0; i < len; i++) {
data[i + start] = buffer[index++];
}
}
public int GetInt ()
{
CheckGetLimit (4);
int num = c.GetInt32 (buffer, index);
index += 4;
return num;
}
public short GetShort ()
{
CheckGetLimit (2);
short num = c.GetInt16 (buffer, index);
index += 2;
return num;
}
public bool HasArray ()
{
return true;
}
public int Limit ()
{
return (limit - offset);
}
public void Limit (int newLimit)
{
limit = newLimit;
}
public void Mark ()
{
mark = index;
}
public void Order (ByteOrder order)
{
this.order = order;
if (order == ByteOrder.BIG_ENDIAN) {
c = DataConverter.BigEndian;
} else {
c = DataConverter.LittleEndian;
}
}
public int Position ()
{
return (index - offset);
}
public void Position (int pos)
{
if ((pos < offset) || (pos > limit)) {
throw new BufferUnderflowException ();
}
index = pos + offset;
}
public void Put (byte[] data)
{
Put (data, 0, data.Length);
}
public void Put (byte data)
{
CheckPutLimit (1);
buffer[index++] = data;
}
public void Put (byte[] data, int start, int len)
{
CheckPutLimit (len);
for (int i = 0; i < len; i++) {
buffer[index++] = data[i + start];
}
}
public void PutInt (int i)
{
Put (c.GetBytes (i));
}
public void PutShort (short i)
{
Put (c.GetBytes (i));
}
public int Remaining ()
{
return (limit - index);
}
public void Reset ()
{
index = mark;
}
public ByteBuffer Slice ()
{
ByteBuffer b = Wrap (buffer, index, buffer.Length - index);
b.offset = index;
b.limit = limit;
b.order = order;
b.c = c;
b.capacity = limit - index;
return b;
}
public static ByteBuffer Wrap (byte[] buf)
{
return new ByteBuffer (buf, 0, buf.Length);
}
public static ByteBuffer Wrap (byte[] buf, int start, int len)
{
return new ByteBuffer (buf, start, len);
}
}
}