Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
InputBuffer.cs
Go to the documentation of this file.
2
3internal sealed class InputBuffer
4{
6
7 private uint _bitBuffer;
8
9 private int _bitsInBuffer;
10
12
13 public int AvailableBytes => _buffer.Length + _bitsInBuffer / 8;
14
15 public bool EnsureBitsAvailable(int count)
16 {
17 if (_bitsInBuffer < count)
18 {
19 if (NeedsInput())
20 {
21 return false;
22 }
23 _bitBuffer |= (uint)(_buffer.Span[0] << _bitsInBuffer);
25 _bitsInBuffer += 8;
26 if (_bitsInBuffer < count)
27 {
28 if (NeedsInput())
29 {
30 return false;
31 }
32 _bitBuffer |= (uint)(_buffer.Span[0] << _bitsInBuffer);
34 _bitsInBuffer += 8;
35 }
36 }
37 return true;
38 }
39
40 public uint TryLoad16Bits()
41 {
42 if (_bitsInBuffer < 8)
43 {
44 if (_buffer.Length > 1)
45 {
46 Span<byte> span = _buffer.Span;
47 _bitBuffer |= (uint)(span[0] << _bitsInBuffer);
48 _bitBuffer |= (uint)(span[1] << _bitsInBuffer + 8);
50 _bitsInBuffer += 16;
51 }
52 else if (_buffer.Length != 0)
53 {
54 _bitBuffer |= (uint)(_buffer.Span[0] << _bitsInBuffer);
56 _bitsInBuffer += 8;
57 }
58 }
59 else if (_bitsInBuffer < 16 && !_buffer.IsEmpty)
60 {
61 _bitBuffer |= (uint)(_buffer.Span[0] << _bitsInBuffer);
63 _bitsInBuffer += 8;
64 }
65 return _bitBuffer;
66 }
67
68 private uint GetBitMask(int count)
69 {
70 return (uint)((1 << count) - 1);
71 }
72
73 public int GetBits(int count)
74 {
76 {
77 return -1;
78 }
79 int result = (int)(_bitBuffer & GetBitMask(count));
80 _bitBuffer >>= count;
82 return result;
83 }
84
85 public int CopyTo(Memory<byte> output)
86 {
87 int num = 0;
88 while (_bitsInBuffer > 0 && !output.IsEmpty)
89 {
90 output.Span[0] = (byte)_bitBuffer;
91 output = output.Slice(1);
92 _bitBuffer >>= 8;
93 _bitsInBuffer -= 8;
94 num++;
95 }
96 if (output.IsEmpty)
97 {
98 return num;
99 }
100 int num2 = Math.Min(output.Length, _buffer.Length);
101 _buffer.Slice(0, num2).CopyTo(output);
102 _buffer = _buffer.Slice(num2);
103 return num + num2;
104 }
105
106 public int CopyTo(byte[] output, int offset, int length)
107 {
108 return CopyTo(output.AsMemory(offset, length));
109 }
110
111 public bool NeedsInput()
112 {
113 return _buffer.IsEmpty;
114 }
115
117 {
118 if (_buffer.IsEmpty)
119 {
120 _buffer = buffer;
121 }
122 }
123
124 public void SetInput(byte[] buffer, int offset, int length)
125 {
126 SetInput(buffer.AsMemory(offset, length));
127 }
128
129 public void SkipBits(int n)
130 {
131 _bitBuffer >>= n;
132 _bitsInBuffer -= n;
133 }
134
135 public void SkipToByteBoundary()
136 {
139 }
140}
int CopyTo(byte[] output, int offset, int length)
void SetInput(byte[] buffer, int offset, int length)
void SetInput(Memory< byte > buffer)
int CopyTo(Memory< byte > output)
static byte Min(byte val1, byte val2)
Definition Math.cs:912
Memory< T > Slice(int start)
Definition Memory.cs:194
unsafe Span< T > Span
Definition Memory.cs:28
bool IsEmpty
Definition Memory.cs:25