Skip to content

Commit 033b4bb

Browse files
Myrrhdge commits
2 parents 49c7902 + 4a2479f commit 033b4bb

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

MLAPI.Tests/NetworkingManagerComponents/Binary/BitStreamTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public void TestEmptyStream()
2020
[Test]
2121
public void TestBool()
2222
{
23-
2423
BitStream bitStream = new BitStream(new byte[100]);
2524
bitStream.WriteBit(true);
2625

MLAPI/NetworkingManagerComponents/Binary/BitStream.cs

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

1616
/// <summary>
1717
/// 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.
1818
/// </summary>
1919
/// <param name="capacity">Initial capacity of buffer in bytes.</param>
2020
/// <param name="growthFactor">Factor by which buffer should grow when necessary.</param>
21-
public BitStream(int capacity = 16, double growthFactor = 2.0)
21+
public BitStream(int capacity = initialCapacity, float growthFactor = 2.0f)
2222
{
2323
target = new byte[capacity];
24-
this.growthFactor = growthFactor <= 1 ? 1.5 : growthFactor;
24+
GrowthFactor = growthFactor;
2525
Resizable = true;
2626
}
2727

28+
/// <summary>
29+
/// 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.
30+
/// </summary>
31+
/// <param name="growthFactor">Factor by which buffer should grow when necessary.</param>
32+
public BitStream(float growthFactor = 2.0f) : this(initialCapacity, growthFactor) { }
33+
34+
/// <summary>
35+
/// 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.
36+
/// </summary>
37+
/// <param name="target">The buffer containing initial data</param>
38+
/// <param name="offset">The offset where the data begins</param>
39+
/// <param name="count">The amount of bytes to copy from the initial data buffer</param>
40+
public BitStream(byte[] target, int offset, int count) : this(count)
41+
{
42+
Buffer.BlockCopy(target, offset, this.target, 0, count);
43+
Resizable = false;
44+
}
45+
2846
/// <summary>
2947
/// 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.
3048
/// NOTE: when using a pre-allocated buffer, the stream will not grow!
@@ -42,6 +60,12 @@ public BitStream(byte[] target)
4260
/// </summary>
4361
public bool Resizable { get; }
4462

63+
private float _growthFactor;
64+
/// <summary>
65+
/// Factor by which buffer should grow when necessary.
66+
/// </summary>
67+
public float GrowthFactor { set { _growthFactor = value <= 1 ? 1.5f : value; } get { return _growthFactor; } }
68+
4569
/// <summary>
4670
/// Whether or not data can be read from the stream.
4771
/// </summary>
@@ -190,7 +214,7 @@ public override void SetLength(long value)
190214
}
191215

192216
/// <summary>
193-
/// Write data from the given buffer to the internal stream buffer,
217+
/// Write data from the given buffer to the internal stream buffer.
194218
/// </summary>
195219
/// <param name="buffer">Buffer to write from.</param>
196220
/// <param name="offset">Offset in given buffer to start reading from.</param>
@@ -212,11 +236,17 @@ public override void Write(byte[] buffer, int offset, int count)
212236
if (BitPosition > BitLength) BitLength = BitPosition;
213237
}
214238

239+
/// <summary>
240+
/// Write data from the given buffer to the internal stream buffer.
241+
/// </summary>
242+
/// <param name="buffer">Buffer to write from.</param>
243+
public void Write(byte[] buffer) => Write(buffer, 0, buffer.Length);
244+
215245
/// <summary>
216246
/// Grow buffer if possible. According to Max(bufferLength, 1) * growthFactor^Ceil(newContent/Max(bufferLength, 1))
217247
/// </summary>
218248
/// <param name="newContent">How many new values need to be accomodated (at least).</param>
219-
private void Grow(long newContent) => SetCapacity(Math.Max(target.LongLength, 1) * (long)Math.Pow(growthFactor, CeilingExact(newContent, Math.Max(target.LongLength, 1))));
249+
private void Grow(long newContent) => SetCapacity(Math.Max(target.LongLength, 1) * (long)Math.Pow(GrowthFactor, CeilingExact(newContent, Math.Max(target.LongLength, 1))));
220250

221251
/// <summary>
222252
/// Write a single bit to the stream
@@ -609,6 +639,23 @@ private void UpdateLength()
609639
/// <returns></returns>
610640
public byte[] GetBuffer() => target;
611641

642+
/// <summary>
643+
/// Creates a copy of the internal buffer. This only contains the used bytes
644+
/// </summary>
645+
/// <returns>A copy of used bytes in the internal buffer</returns>
646+
public byte[] ToArray()
647+
{
648+
byte[] copy = new byte[Length];
649+
Buffer.BlockCopy(target, 0, copy, 0, (int)Length);
650+
return copy;
651+
}
652+
653+
/// <summary>
654+
/// Returns hex encoded version of the buffer
655+
/// </summary>
656+
/// <returns>Hex encoded version of the buffer</returns>
657+
public override string ToString() => BitConverter.ToString(target, 0, (int)Length);
658+
612659
/// <summary>
613660
/// An exception representing cases when buffer-capacity related errors occur.
614661
/// </summary>

0 commit comments

Comments
 (0)