@@ -64,7 +64,7 @@ public BitStream(byte[] target)
64
64
{
65
65
this . target = target ;
66
66
Resizable = false ;
67
- BitLength = ( ulong ) ( target . Length << 3 ) ;
67
+ BitLength = ( ulong ) ( target . Length << 3 ) ;
68
68
}
69
69
70
70
/// <summary>
@@ -96,15 +96,16 @@ public BitStream(byte[] target)
96
96
/// <summary>
97
97
/// Current buffer size. The buffer will not be resized (if possible) until Position is equal to Capacity and an attempt to write data is made.
98
98
/// </summary>
99
- public long Capacity {
99
+ public long Capacity
100
+ {
100
101
get => target . LongLength ; // Optimized CeilingExact
101
102
set
102
103
{
103
104
if ( value < Length ) throw new ArgumentOutOfRangeException ( "New capcity too small!" ) ;
104
105
SetCapacity ( value ) ;
105
106
}
106
107
}
107
-
108
+
108
109
/// <summary>
109
110
/// The current length of data considered to be "written" to the buffer.
110
111
/// </summary>
@@ -113,7 +114,7 @@ public long Capacity {
113
114
/// <summary>
114
115
/// The index that will be written to when any call to write data is made to this stream.
115
116
/// </summary>
116
- public override long Position { get => ( long ) ( BitPosition >> 3 ) ; set => BitPosition = ( ulong ) value << 3 ; }
117
+ public override long Position { get => ( long ) ( BitPosition >> 3 ) ; set => BitPosition = ( ulong ) value << 3 ; }
117
118
118
119
/// <summary>
119
120
/// Bit offset into the buffer that new data will be written to.
@@ -221,7 +222,7 @@ private void SetCapacity(long value)
221
222
public override void SetLength ( long value )
222
223
{
223
224
if ( value < 0 ) throw new IndexOutOfRangeException ( "Cannot set a negative length!" ) ;
224
- if ( value > Capacity ) Grow ( value - Capacity ) ;
225
+ if ( value > Capacity ) Grow ( value - Capacity ) ;
225
226
BitLength = ( ulong ) value << 3 ;
226
227
}
227
228
@@ -310,7 +311,7 @@ public void WriteDouble(double value)
310
311
/// <param name="value">Value to write</param>
311
312
public void WriteSinglePacked ( float value )
312
313
{
313
- lock ( holder_f )
314
+ lock ( holder_f )
314
315
lock ( holder_i )
315
316
{
316
317
holder_f [ 0 ] = value ;
@@ -356,8 +357,8 @@ public void WriteRangedSingle(float value, float minValue, float maxValue, int b
356
357
{
357
358
if ( bytes < 1 || bytes > 4 ) throw new ArgumentOutOfRangeException ( "Result must occupy between 1 and 4 bytes!" ) ;
358
359
if ( value < minValue || value > maxValue ) throw new ArgumentOutOfRangeException ( "Given value does not match the given constraints!" ) ;
359
- uint result = ( uint ) ( ( ( value + minValue ) / ( maxValue + minValue ) ) * ( ( 0x100 * bytes ) - 1 ) ) ;
360
- for ( int i = 0 ; i < bytes ; ++ i ) _WriteByte ( ( byte ) ( result >> ( i << 3 ) ) ) ;
360
+ uint result = ( uint ) ( ( ( value + minValue ) / ( maxValue + minValue ) ) * ( ( 0x100 * bytes ) - 1 ) ) ;
361
+ for ( int i = 0 ; i < bytes ; ++ i ) _WriteByte ( ( byte ) ( result >> ( i << 3 ) ) ) ;
361
362
}
362
363
363
364
/// <summary>
@@ -371,7 +372,7 @@ public void WriteRangedDouble(double value, double minValue, double maxValue, in
371
372
{
372
373
if ( bytes < 1 || bytes > 8 ) throw new ArgumentOutOfRangeException ( "Result must occupy between 1 and 8 bytes!" ) ;
373
374
if ( value < minValue || value > maxValue ) throw new ArgumentOutOfRangeException ( "Given value does not match the given constraints!" ) ;
374
- ulong result = ( ulong ) ( ( ( value + minValue ) / ( maxValue + minValue ) ) * ( ( 0x100 * bytes ) - 1 ) ) ;
375
+ ulong result = ( ulong ) ( ( ( value + minValue ) / ( maxValue + minValue ) ) * ( ( 0x100 * bytes ) - 1 ) ) ;
375
376
for ( int i = 0 ; i < bytes ; ++ i ) _WriteByte ( ( byte ) ( result >> ( i << 3 ) ) ) ;
376
377
}
377
378
@@ -383,7 +384,7 @@ public void WriteRangedDouble(double value, double minValue, double maxValue, in
383
384
public void WriteRotation ( Quaternion rotation , int bytesPerAngle )
384
385
{
385
386
if ( bytesPerAngle < 1 || bytesPerAngle > 4 ) throw new ArgumentOutOfRangeException ( "Bytes per angle must be at least 1 byte and at most 4 bytes!" ) ;
386
- if ( bytesPerAngle == 4 ) WriteVector3 ( rotation . eulerAngles ) ;
387
+ if ( bytesPerAngle == 4 ) WriteVector3 ( rotation . eulerAngles ) ;
387
388
else
388
389
{
389
390
Vector3 rot = rotation . eulerAngles ;
@@ -425,15 +426,15 @@ public double ReadDouble()
425
426
return holder_d [ 0 ] ;
426
427
}
427
428
}
428
-
429
+
429
430
/// <summary>
430
431
/// Read a single-precision floating point value from the stream from a varint
431
432
/// </summary>
432
433
/// <returns>The read value</returns>
433
434
public float ReadSinglePacked ( )
434
435
{
435
436
uint read = ReadUInt32Packed ( ) ;
436
- lock ( holder_f )
437
+ lock ( holder_f )
437
438
lock ( holder_i )
438
439
{
439
440
holder_i [ 0 ] = BinaryHelpers . SwapEndian ( read ) ;
@@ -527,7 +528,7 @@ public void WriteNibble(byte value)
527
528
value &= 0x0F ;
528
529
int offset = ( int ) ( BitPosition & 7 ) , offset_inv = 8 - offset ;
529
530
target [ Position ] = ( byte ) ( ( target [ Position ] & ( 0xFF >> offset_inv ) ) | ( byte ) ( value << offset ) ) ;
530
- if ( offset > 4 ) target [ Position + 1 ] = ( byte ) ( ( target [ Position + 1 ] & ( 0xFF << ( offset & 3 ) ) ) | ( byte ) ( value >> offset_inv ) ) ;
531
+ if ( offset > 4 ) target [ Position + 1 ] = ( byte ) ( ( target [ Position + 1 ] & ( 0xFF << ( offset & 3 ) ) ) | ( byte ) ( value >> offset_inv ) ) ;
531
532
BitPosition += 4 ;
532
533
}
533
534
UpdateLength ( ) ;
@@ -546,13 +547,13 @@ public void WriteNibble(byte value)
546
547
/// <param name="bitCount">Amount of bits to write</param>
547
548
public void WriteBits ( ulong value , int bitCount )
548
549
{
549
- if ( BitPosition + ( ulong ) bitCount > ( ( ulong ) target . LongLength << 3 ) ) Grow ( Div8Ceil ( BitPosition + ( ulong ) bitCount ) ) ;
550
+ if ( BitPosition + ( ulong ) bitCount > ( ( ulong ) target . LongLength << 3 ) ) Grow ( Div8Ceil ( BitPosition + ( ulong ) bitCount ) ) ;
550
551
if ( bitCount > 64 ) throw new ArgumentOutOfRangeException ( "Cannot read more than 64 bits from a 64-bit value!" ) ;
551
552
if ( bitCount < 0 ) throw new ArgumentOutOfRangeException ( "Cannot read fewer than 0 bits!" ) ;
552
553
int count = - 8 ;
553
- while ( bitCount > ( count += 8 ) ) _WriteULongByte ( value >> count ) ;
554
+ while ( bitCount > ( count += 8 ) ) _WriteULongByte ( value >> count ) ;
554
555
BitPosition += ( ulong ) count ;
555
- if ( ( bitCount & 7 ) != 0 ) _WriteBits ( ( byte ) ( value >> count ) , bitCount & 7 ) ;
556
+ if ( ( bitCount & 7 ) != 0 ) _WriteBits ( ( byte ) ( value >> count ) , bitCount & 7 ) ;
556
557
UpdateLength ( ) ;
557
558
}
558
559
/// <summary>
@@ -570,7 +571,7 @@ private void _WriteBits(byte value, int bitCount)
570
571
target [ Position ] = ( byte ) (
571
572
( target [ Position ] & ( 0xFF >> offset_inv ) ) | // Bits prior to value (lower)
572
573
( target [ Position ] & ( 0xFF << ( offset + bitCount ) ) ) | // Bits after value (higher)
573
- ( value << offset ) // Bits to write
574
+ ( value << offset ) // Bits to write
574
575
) ;
575
576
if ( bitCount + offset > 8 )
576
577
target [ Position + 1 ] = ( byte ) (
@@ -733,15 +734,15 @@ public void WriteUInt64Packed(ulong value)
733
734
/// Read an unsigned long (UInt64) from the stream.
734
735
/// </summary>
735
736
/// <returns>Value read from stream.</returns>
736
- public ulong ReadUInt64 ( ) => ( ulong ) (
737
+ public ulong ReadUInt64 ( ) => (
737
738
_ReadByte ( ) |
738
- ( _ReadByte ( ) << 8 ) |
739
- ( _ReadByte ( ) << 16 ) |
740
- ( _ReadByte ( ) << 24 ) |
741
- ( _ReadByte ( ) << 32 ) |
742
- ( _ReadByte ( ) << 40 ) |
743
- ( _ReadByte ( ) << 48 ) |
744
- ( _ReadByte ( ) << 56 )
739
+ ( ( ulong ) _ReadByte ( ) << 8 ) |
740
+ ( ( ulong ) _ReadByte ( ) << 16 ) |
741
+ ( ( ulong ) _ReadByte ( ) << 24 ) |
742
+ ( ( ulong ) _ReadByte ( ) << 32 ) |
743
+ ( ( ulong ) _ReadByte ( ) << 40 ) |
744
+ ( ( ulong ) _ReadByte ( ) << 48 ) |
745
+ ( ( ulong ) _ReadByte ( ) << 56 )
745
746
) ;
746
747
/// <summary>
747
748
/// Read a signed long (Int64) from the stream.
@@ -881,19 +882,19 @@ public override void WriteByte(byte value)
881
882
/// <param name="count">How many bytes to read. Set to value less than one to read until ReadByte returns -1</param>
882
883
public void CopyFrom ( Stream s , int count = - 1 )
883
884
{
884
- if ( s is BitStream b ) Write ( b . target , 0 , count < 0 ? ( int ) b . Length : count ) ;
885
+ if ( s is BitStream b ) Write ( b . target , 0 , count < 0 ? ( int ) b . Length : count ) ;
885
886
else
886
887
{
887
888
int read ;
888
889
bool readToEnd = count < 0 ;
889
- while ( ( readToEnd || count -- > 0 ) && ( read = s . ReadByte ( ) ) != - 1 )
890
+ while ( ( readToEnd || count -- > 0 ) && ( read = s . ReadByte ( ) ) != - 1 )
890
891
_WriteIntByte ( read ) ;
891
892
UpdateLength ( ) ;
892
893
}
893
894
}
894
-
895
+
895
896
// TODO: Implement CopyFrom() for BitStream with bitCount parameter
896
-
897
+
897
898
/// <summary>
898
899
/// Update length of data considered to be "written" to the stream.
899
900
/// </summary>
0 commit comments