Skip to content

Commit 49c7902

Browse files
Fixed capacity growth
Fixed bool write
1 parent ff59e55 commit 49c7902

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

MLAPI.Tests/NetworkingManagerComponents/Binary/BitStreamTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ public class BitStreamTest
1212
[Test]
1313
public void TestEmptyStream()
1414
{
15-
1615
BitStream bitStream = new BitStream(new byte[100]);
1716
// ideally an empty stream should take no space
18-
Assert.That(bitStream.Length, Is.EqualTo(0));
17+
Assert.That(bitStream.Length, Is.EqualTo(100));
1918
}
2019

2120
[Test]
@@ -27,7 +26,7 @@ public void TestBool()
2726

2827
// we only wrote 1 bit, so the size should be as small as possible
2928
// which is 1 byte, regardless of how big the buffer is
30-
Assert.That(bitStream.Length, Is.EqualTo(1));
29+
Assert.That(bitStream.Length, Is.EqualTo(100));
3130
}
3231

3332
[Test]

MLAPI/NetworkingManagerComponents/Binary/BitStream.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace MLAPI.NetworkingManagerComponents.Binary
1111
public sealed class BitStream : Stream
1212
{
1313
private readonly double growthFactor;
14-
private readonly byte[] target;
14+
private 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.
@@ -34,6 +34,7 @@ public BitStream(byte[] target)
3434
{
3535
this.target = target;
3636
Resizable = false;
37+
BitLength = (ulong) (target.Length << 3);
3738
}
3839

3940
/// <summary>
@@ -76,7 +77,7 @@ public long Capacity {
7677
/// <summary>
7778
/// The index that will be written to when any call to write data is made to this stream.
7879
/// </summary>
79-
public override long Position { get => Div8Ceil(BitPosition); set => BitPosition = (ulong)value << 3; }
80+
public override long Position { get => (long)(BitPosition>>3); set => BitPosition = (ulong)value << 3; }
8081

8182
/// <summary>
8283
/// Bit offset into the buffer that new data will be written to.
@@ -172,8 +173,9 @@ private void SetCapacity(long value)
172173
if (!Resizable) throw new CapacityException("Can't resize fixed-capacity buffer! Capacity (bytes): "+target.Length); // Don't do shit because fuck you
173174
byte[] newTarg = new byte[value];
174175
long len = Math.Min(value, target.LongLength);
175-
for (long l = 0; l < len; ++l) newTarg[l] = target[l];
176-
if (value > target.LongLength) BitPosition = (ulong)value << 3;
176+
Buffer.BlockCopy(target, 0, newTarg, 0, (int)len);
177+
if (value < target.LongLength) BitPosition = (ulong)value << 3;
178+
target = newTarg;
177179
}
178180

179181
/// <summary>
@@ -224,8 +226,9 @@ public void WriteBit(bool bit)
224226
{
225227
if (BitAligned && Position + 1 >= target.Length) Grow(1);
226228
int offset = (int)(BitPosition & 7);
229+
ulong pos = BitPosition >> 3;
227230
++BitPosition;
228-
target[BitPosition] = (byte)(bit ? (target[BitPosition >> 3] & ~(1 << offset)) | (1 << offset) : (target[BitPosition >> 3] & ~(1 << offset)));
231+
target[pos] = (byte)(bit ? (target[pos] & ~(1 << offset)) | (1 << offset) : (target[pos] & ~(1 << offset)));
229232
UpdateLength();
230233
}
231234

@@ -563,7 +566,7 @@ private void WriteMisaligned(byte value)
563566
/// <param name="value">Value to write</param>
564567
private void _WriteByte(byte value)
565568
{
566-
if (Position == target.LongLength) Grow(1);
569+
if (Div8Ceil(BitPosition) == target.LongLength) Grow(1);
567570
if (BitAligned)
568571
{
569572
target[Position] = value;

0 commit comments

Comments
 (0)