5
public class ByteBuffer
8
private DataConverter c;
14
private ByteOrder order;
18
this.c = DataConverter.BigEndian;
21
private ByteBuffer (byte[] buf, int start, int len)
25
this.limit = start + len;
28
this.capacity = buf.Length;
29
this.c = DataConverter.BigEndian;
32
public static ByteBuffer Allocate (int size)
34
return new ByteBuffer (new byte[size], 0, size);
37
public static ByteBuffer AllocateDirect (int size)
39
return Allocate (size);
42
public byte[] Array ()
47
public int ArrayOffset ()
52
public int Capacity ()
57
private void CheckGetLimit (int inc)
59
if ((index + inc) > limit) {
60
throw new BufferUnderflowException ();
64
private void CheckPutLimit (int inc)
66
if ((index + inc) > limit) {
67
throw new BufferUnderflowException ();
74
limit = offset + capacity;
86
return buffer[index++];
89
public void Get (byte[] data)
91
Get (data, 0, data.Length);
94
public void Get (byte[] data, int start, int len)
97
for (int i = 0; i < len; i++) {
98
data[i + start] = buffer[index++];
105
int num = c.GetInt32 (buffer, index);
110
public short GetShort ()
113
short num = c.GetInt16 (buffer, index);
118
public bool HasArray ()
125
return (limit - offset);
128
public void Limit (int newLimit)
138
public void Order (ByteOrder order)
141
if (order == ByteOrder.BIG_ENDIAN) {
142
c = DataConverter.BigEndian;
144
c = DataConverter.LittleEndian;
148
public int Position ()
150
return (index - offset);
153
public void Position (int pos)
155
if ((pos < offset) || (pos > limit)) {
156
throw new BufferUnderflowException ();
158
index = pos + offset;
161
public void Put (byte[] data)
163
Put (data, 0, data.Length);
166
public void Put (byte data)
169
buffer[index++] = data;
172
public void Put (byte[] data, int start, int len)
175
for (int i = 0; i < len; i++) {
176
buffer[index++] = data[i + start];
180
public void PutInt (int i)
182
Put (c.GetBytes (i));
185
public void PutShort (short i)
187
Put (c.GetBytes (i));
190
public int Remaining ()
192
return (limit - index);
200
public ByteBuffer Slice ()
202
ByteBuffer b = Wrap (buffer, index, buffer.Length - index);
207
b.capacity = limit - index;
211
public static ByteBuffer Wrap (byte[] buf)
213
return new ByteBuffer (buf, 0, buf.Length);
216
public static ByteBuffer Wrap (byte[] buf, int start, int len)
218
return new ByteBuffer (buf, start, len);