Skip to content

Commit c4e4d54

Browse files
committed
Added additional constructors
1 parent 188413b commit c4e4d54

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

MLAPI/NetworkingManagerComponents/Binary/BitStream.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,40 @@ namespace MLAPI.NetworkingManagerComponents.Binary
1010
{
1111
public sealed class BitStream : Stream
1212
{
13-
private readonly double growthFactor;
13+
const int initialCapacity = 16;
14+
private readonly float growthFactor;
1415
private readonly byte[] target;
1516

1617
/// <summary>
1718
/// A stream that supports writing data smaller than a single byte. This stream also has a built-in compression algorithm that can (optionally) be used to write compressed data.
1819
/// </summary>
1920
/// <param name="capacity">Initial capacity of buffer in bytes.</param>
2021
/// <param name="growthFactor">Factor by which buffer should grow when necessary.</param>
21-
public BitStream(int capacity = 16, double growthFactor = 2.0)
22+
public BitStream(int capacity = initialCapacity, float growthFactor = 2.0f)
2223
{
2324
target = new byte[capacity];
24-
this.growthFactor = growthFactor <= 1 ? 1.5 : growthFactor;
25+
this.growthFactor = growthFactor <= 1 ? 1.5f : growthFactor;
2526
Resizable = true;
2627
}
2728

29+
/// <summary>
30+
/// A stream that supports writing data smaller than a single byte. This stream also has a built-in compression algorithm that can (optionally) be used to write compressed data.
31+
/// </summary>
32+
/// <param name="growthFactor">Factor by which buffer should grow when necessary.</param>
33+
public BitStream(float growthFactor = 2.0f) : this(initialCapacity, growthFactor) { }
34+
35+
/// <summary>
36+
/// A stream that supports writing data smaller than a single byte. This stream also has a built-in compression algorithm that can (optionally) be used to write compressed data.
37+
/// </summary>
38+
/// <param name="target">The buffer containing initial data</param>
39+
/// <param name="offset">The offset where the data begins</param>
40+
/// <param name="count">The amount of bytes to copy from the initial data buffer</param>
41+
public BitStream(byte[] target, int offset, int count) : this(count)
42+
{
43+
Buffer.BlockCopy(target, offset, this.target, 0, count);
44+
Resizable = false;
45+
}
46+
2847
/// <summary>
2948
/// A stream that supports writing data smaller than a single byte. This stream also has a built-in compression algorithm that can (optionally) be used to write compressed data.
3049
/// NOTE: when using a pre-allocated buffer, the stream will not grow!

0 commit comments

Comments
 (0)