Skip to content

Commit ed5d7a7

Browse files
Added method to copy data from another stream
1 parent 9bdc78b commit ed5d7a7

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

MLAPI/NetworkingManagerComponents/Binary/BitStream.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,29 @@ public override void WriteByte(byte value)
625625
_WriteByte(value);
626626
UpdateLength();
627627
}
628+
/// <summary>
629+
/// Copy data from another stream
630+
/// </summary>
631+
/// <param name="s">Stream to copy from</param>
632+
/// <param name="count">How many bytes to read. Set to value less than one to read until ReadByte returns -1</param>
633+
public void CopyFrom(Stream s, int count = -1)
634+
{
635+
if(s is BitStream b)
636+
{
637+
ulong bytes = b.BitLength >> 3;
638+
Write(b.target, 0, (int)bytes);
639+
if ((b.BitLength & 7) != 0) _WriteBits(b.target[bytes - 1], (int)(b.BitLength & 7));
640+
}
641+
else
642+
{
643+
int read;
644+
bool readToEnd = count < 0;
645+
while((readToEnd || count-- > 0) && (read = s.ReadByte()) != -1)
646+
_WriteIntByte(read);
647+
}
648+
649+
UpdateLength();
650+
}
628651

629652
/// <summary>
630653
/// Update length of data considered to be "written" to the stream.

0 commit comments

Comments
 (0)