Terraria
v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
InputBuffer.cs
Go to the documentation of this file.
1
namespace
System.IO.Compression
;
2
3
internal
sealed
class
InputBuffer
4
{
5
private
Memory<byte>
_buffer
;
6
7
private
uint
_bitBuffer
;
8
9
private
int
_bitsInBuffer
;
10
11
public
int
AvailableBits
=>
_bitsInBuffer
;
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
);
24
_buffer
=
_buffer
.
Slice
(1);
25
_bitsInBuffer
+= 8;
26
if
(
_bitsInBuffer
<
count
)
27
{
28
if
(
NeedsInput
())
29
{
30
return
false
;
31
}
32
_bitBuffer
|= (uint)(
_buffer
.
Span
[0] <<
_bitsInBuffer
);
33
_buffer
=
_buffer
.
Slice
(1);
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);
49
_buffer
=
_buffer
.
Slice
(2);
50
_bitsInBuffer
+= 16;
51
}
52
else
if
(
_buffer
.
Length
!= 0)
53
{
54
_bitBuffer
|= (uint)(
_buffer
.
Span
[0] <<
_bitsInBuffer
);
55
_buffer
=
_buffer
.
Slice
(1);
56
_bitsInBuffer
+= 8;
57
}
58
}
59
else
if
(
_bitsInBuffer
< 16 && !
_buffer
.
IsEmpty
)
60
{
61
_bitBuffer
|= (uint)(
_buffer
.
Span
[0] <<
_bitsInBuffer
);
62
_buffer
=
_buffer
.
Slice
(1);
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
{
75
if
(!
EnsureBitsAvailable
(
count
))
76
{
77
return
-1;
78
}
79
int
result = (int)(
_bitBuffer
&
GetBitMask
(
count
));
80
_bitBuffer
>>=
count
;
81
_bitsInBuffer
-=
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
116
public
void
SetInput
(
Memory<byte>
buffer
)
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
{
137
_bitBuffer
>>=
_bitsInBuffer
% 8;
138
_bitsInBuffer
-=
_bitsInBuffer
% 8;
139
}
140
}
System.IO.Compression.InputBuffer.TryLoad16Bits
uint TryLoad16Bits()
Definition
InputBuffer.cs:40
System.IO.Compression.InputBuffer.AvailableBytes
int AvailableBytes
Definition
InputBuffer.cs:13
System.IO.Compression.InputBuffer.CopyTo
int CopyTo(byte[] output, int offset, int length)
Definition
InputBuffer.cs:106
System.IO.Compression.InputBuffer.GetBits
int GetBits(int count)
Definition
InputBuffer.cs:73
System.IO.Compression.InputBuffer.GetBitMask
uint GetBitMask(int count)
Definition
InputBuffer.cs:68
System.IO.Compression.InputBuffer.AvailableBits
int AvailableBits
Definition
InputBuffer.cs:11
System.IO.Compression.InputBuffer.SkipBits
void SkipBits(int n)
Definition
InputBuffer.cs:129
System.IO.Compression.InputBuffer.NeedsInput
bool NeedsInput()
Definition
InputBuffer.cs:111
System.IO.Compression.InputBuffer.SkipToByteBoundary
void SkipToByteBoundary()
Definition
InputBuffer.cs:135
System.IO.Compression.InputBuffer._bitsInBuffer
int _bitsInBuffer
Definition
InputBuffer.cs:9
System.IO.Compression.InputBuffer._buffer
Memory< byte > _buffer
Definition
InputBuffer.cs:5
System.IO.Compression.InputBuffer.SetInput
void SetInput(byte[] buffer, int offset, int length)
Definition
InputBuffer.cs:124
System.IO.Compression.InputBuffer.EnsureBitsAvailable
bool EnsureBitsAvailable(int count)
Definition
InputBuffer.cs:15
System.IO.Compression.InputBuffer.SetInput
void SetInput(Memory< byte > buffer)
Definition
InputBuffer.cs:116
System.IO.Compression.InputBuffer.CopyTo
int CopyTo(Memory< byte > output)
Definition
InputBuffer.cs:85
System.IO.Compression.InputBuffer._bitBuffer
uint _bitBuffer
Definition
InputBuffer.cs:7
System.IO.Compression.InputBuffer
Definition
InputBuffer.cs:4
System.Math.Min
static byte Min(byte val1, byte val2)
Definition
Math.cs:912
System.Math
Definition
Math.cs:13
System.IO.Compression
Definition
BrotliDecoder.cs:5
System.ExceptionArgument.length
@ length
System.ExceptionArgument.offset
@ offset
System.ExceptionArgument.buffer
@ buffer
System.ExceptionArgument.count
@ count
System.Memory.Length
int Length
Definition
Memory.cs:23
System.Memory.Slice
Memory< T > Slice(int start)
Definition
Memory.cs:194
System.Memory.Span
unsafe Span< T > Span
Definition
Memory.cs:28
System.Memory.IsEmpty
bool IsEmpty
Definition
Memory.cs:25
System.Memory
Definition
Memory.cs:14
System.Span
Definition
Span.cs:14
source
System.IO.Compression
System.IO.Compression
InputBuffer.cs
Generated by
1.10.0