forked from fast-pack/CSharpFastPFOR
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathByteBuffer.cs
More file actions
78 lines (64 loc) · 1.74 KB
/
ByteBuffer.cs
File metadata and controls
78 lines (64 loc) · 1.74 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
using System.IO;
namespace CSharpFastPFOR.Port
{
public class ByteBuffer
{
public readonly MemoryStream _ms;
private readonly BinaryWriter _bw;
private readonly BinaryReader _br;
private ByteOrder _order;
public ByteBuffer(int length)
{
_ms = new MemoryStream();
_ms.SetLength(length);
_bw = new BinaryWriter(_ms);
_br = new BinaryReader(_ms);
_order = ByteOrder.BIG_ENDIAN; //To simulate Java
}
internal static ByteBuffer allocateDirect(int length)
{
return new ByteBuffer(length);
}
public void order(ByteOrder order)
{
_order = order;
}
public int position()
{
return (int)_ms.Position;
}
public void put(sbyte b)
{
_bw.Write(b);
}
public void flip()
{
_ms.Position = 0;
}
public IntBuffer asIntBuffer()
{
return new IntBuffer(_ms, _order);
}
public void clear()
{
byte[] empty = new byte[_ms.Length];
_ms.Position = 0;
_ms.Write(empty, 0, empty.Length);
_ms.Position = 0;
}
public sbyte get()
{
return _br.ReadSByte();
}
internal void put(int[] src, int offset, int length)
{
//TODO: port this
//checkBounds(offset, length, src.Length);
//if (length > remaining())
// throw new BufferOverflowException();
int end = offset + length;
for (int i = offset; i < end; i++)
_bw.Write(src[i]);
}
}
}