@@ -45,19 +45,28 @@ public BitReader(Stream stream)
45
45
/// Reads a single bit
46
46
/// </summary>
47
47
/// <returns>The bit read</returns>
48
- public bool ReadBit ( ) => bitSource . ReadBit ( ) ;
48
+ public bool ReadBit ( )
49
+ {
50
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
51
+ return bitSource . ReadBit ( ) ;
52
+ }
49
53
50
54
/// <summary>
51
55
/// Reads a single bit
52
56
/// </summary>
53
57
/// <returns>The bit read</returns>
54
- public bool ReadBool ( ) => ReadBit ( ) ;
58
+ public bool ReadBool ( )
59
+ {
60
+ if ( bitSource == null ) return source . ReadByte ( ) != 0 ;
61
+ else return ReadBit ( ) ;
62
+ }
55
63
56
64
/// <summary>
57
65
/// Skips pad bits and aligns the position to the next byte
58
66
/// </summary>
59
67
public void SkipPadBits ( )
60
68
{
69
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
61
70
while ( ! bitSource . BitAligned ) ReadBit ( ) ;
62
71
}
63
72
@@ -279,6 +288,7 @@ public Quaternion ReadRotation(int bytesPerAngle)
279
288
/// <returns>The bits that were read</returns>
280
289
public ulong ReadBits ( int bitCount )
281
290
{
291
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
282
292
if ( bitCount > 64 ) throw new ArgumentOutOfRangeException ( "Cannot read more than 64 bits into a 64-bit value!" ) ;
283
293
if ( bitCount < 0 ) throw new ArgumentOutOfRangeException ( "Cannot read fewer than 0 bits!" ) ;
284
294
ulong read = 0 ;
@@ -293,42 +303,38 @@ public ulong ReadBits(int bitCount)
293
303
/// <param name="bitCount">How many bits to read. Minimum 0, maximum 64.</param>
294
304
/// <returns>The bits that were read</returns>
295
305
public byte ReadByteBits ( int bitCount )
296
- {
297
- if ( bitCount > 8 )
298
- throw new ArgumentOutOfRangeException ( "Cannot read more than 8 bits into an 8-bit value!" )
299
- ;
300
- if ( bitCount < 0 )
301
- throw new ArgumentOutOfRangeException ( "Cannot read fewer than 0 bits!" )
302
- ;
306
+ {
307
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
308
+ if ( bitCount > 8 ) throw new ArgumentOutOfRangeException ( "Cannot read more than 8 bits into an 8-bit value!" ) ;
309
+ if ( bitCount < 0 ) throw new ArgumentOutOfRangeException ( "Cannot read fewer than 0 bits!" ) ;
310
+
303
311
int result = 0 ;
304
312
ByteBool convert = new ByteBool ( ) ;
305
313
for ( int i = 0 ; i < bitCount ; ++ i )
306
- result |= convert . Collapse ( ReadBit ( ) ) << i
307
- ;
308
- return ( byte ) result
309
- ;
310
- }
314
+ result |= convert . Collapse ( ReadBit ( ) ) << i ;
315
+ return ( byte ) result ;
316
+ }
311
317
312
318
/// <summary>
313
319
/// Read a nibble (4 bits) from the stream.
314
320
/// </summary>
315
321
/// <param name="asUpper">Whether or not the nibble should be left-shifted by 4 bits</param>
316
322
/// <returns>The nibble that was read</returns>
317
323
public byte ReadNibble ( bool asUpper )
318
- {
319
- ByteBool convert = new ByteBool ( )
320
- ;
321
- byte result = ( byte ) (
322
- convert . Collapse ( ReadBit ( ) ) |
324
+ {
325
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
326
+ ByteBool convert = new ByteBool ( ) ;
327
+
328
+ byte result = ( byte ) (
329
+ convert . Collapse ( ReadBit ( ) ) |
323
330
( convert . Collapse ( ReadBit ( ) ) << 1 ) |
324
331
( convert . Collapse ( ReadBit ( ) ) << 2 ) |
325
332
( convert . Collapse ( ReadBit ( ) ) << 3 )
326
- )
327
- ;
333
+ ) ;
328
334
if ( asUpper ) result <<= 4 ;
329
- return result
330
- ;
331
- }
335
+ return result ;
336
+
337
+ }
332
338
333
339
// Marginally faster than the one that accepts a bool
334
340
/// <summary>
@@ -337,13 +343,14 @@ public byte ReadNibble(bool asUpper)
337
343
/// <returns>The nibble that was read</returns>
338
344
public byte ReadNibble ( )
339
345
{
346
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
340
347
ByteBool convert = new ByteBool ( ) ;
341
- return ( byte ) (
342
- convert . Collapse ( ReadBit ( ) ) |
348
+ return ( byte ) (
349
+ convert . Collapse ( ReadBit ( ) ) |
343
350
( convert . Collapse ( ReadBit ( ) ) << 1 ) |
344
351
( convert . Collapse ( ReadBit ( ) ) << 2 ) |
345
352
( convert . Collapse ( ReadBit ( ) ) << 3 )
346
- ) ;
353
+ ) ;
347
354
}
348
355
349
356
public sbyte ReadSByte ( ) => ( sbyte ) ReadByte ( ) ;
@@ -464,6 +471,7 @@ public StringBuilder ReadStringPacked(StringBuilder builder = null)
464
471
public StringBuilder ReadStringDiff ( string compare , bool oneByteChars = false ) => ReadStringDiff ( null , compare , oneByteChars ) ;
465
472
public StringBuilder ReadStringDiff ( StringBuilder builder , string compare , bool oneByteChars = false )
466
473
{
474
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
467
475
int expectedLength = ( int ) ReadUInt32Packed ( ) ;
468
476
if ( builder == null ) builder = new StringBuilder ( expectedLength ) ;
469
477
else if ( builder . Capacity < expectedLength ) builder . Capacity = expectedLength ;
@@ -495,6 +503,7 @@ public StringBuilder ReadStringDiff(StringBuilder builder, string compare, bool
495
503
496
504
public StringBuilder ReadStringDiff ( StringBuilder compareAndBuffer , bool oneByteChars = false )
497
505
{
506
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
498
507
int expectedLength = ( int ) ReadUInt32Packed ( ) ;
499
508
if ( compareAndBuffer == null ) throw new ArgumentNullException ( "Buffer cannot be null" ) ;
500
509
else if ( compareAndBuffer . Capacity < expectedLength ) compareAndBuffer . Capacity = expectedLength ;
@@ -526,6 +535,7 @@ public StringBuilder ReadStringDiff(StringBuilder compareAndBuffer, bool oneByte
526
535
public StringBuilder ReadStringPackedDiff ( string compare ) => ReadStringPackedDiff ( null , compare ) ;
527
536
public StringBuilder ReadStringPackedDiff ( StringBuilder builder , string compare )
528
537
{
538
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
529
539
int expectedLength = ( int ) ReadUInt32Packed ( ) ;
530
540
if ( builder == null ) builder = new StringBuilder ( expectedLength ) ;
531
541
else if ( builder . Capacity < expectedLength ) builder . Capacity = expectedLength ;
@@ -557,6 +567,7 @@ public StringBuilder ReadStringPackedDiff(StringBuilder builder, string compare)
557
567
558
568
public StringBuilder ReadStringPackedDiff ( StringBuilder compareAndBuffer )
559
569
{
570
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
560
571
int expectedLength = ( int ) ReadUInt32Packed ( ) ;
561
572
if ( compareAndBuffer == null ) throw new ArgumentNullException ( "Buffer cannot be null" ) ;
562
573
else if ( compareAndBuffer . Capacity < expectedLength ) compareAndBuffer . Capacity = expectedLength ;
@@ -595,6 +606,7 @@ public byte[] ReadByteArray(byte[] readTo = null, long knownLength = -1)
595
606
596
607
public byte [ ] ReadByteArrayDiff ( byte [ ] readTo = null , long knownLength = - 1 )
597
608
{
609
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
598
610
if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
599
611
byte [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new byte [ knownLength ] : readTo ;
600
612
ulong dBlockStart = bitSource . BitPosition + ( ulong ) ( readTo == null ? 0 : Math . Min ( knownLength , readTo . LongLength ) ) ;
@@ -641,6 +653,7 @@ public short[] ReadShortArrayPacked(short[] readTo = null, long knownLength = -1
641
653
642
654
public short [ ] ReadShortArrayDiff ( short [ ] readTo = null , long knownLength = - 1 )
643
655
{
656
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
644
657
if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
645
658
short [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new short [ knownLength ] : readTo ;
646
659
ulong dBlockStart = bitSource . BitPosition + ( ulong ) ( readTo == null ? 0 : Math . Min ( knownLength , readTo . LongLength ) ) ;
@@ -671,6 +684,7 @@ public short[] ReadShortArrayDiff(short[] readTo = null, long knownLength = -1)
671
684
672
685
public short [ ] ReadShortArrayPackedDiff ( short [ ] readTo = null , long knownLength = - 1 )
673
686
{
687
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
674
688
if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
675
689
short [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new short [ knownLength ] : readTo ;
676
690
ulong data = bitSource . BitPosition + ( ulong ) ( readTo == null ? 0 : Math . Min ( knownLength , readTo . LongLength ) ) ;
@@ -717,6 +731,7 @@ public ushort[] ReadUShortArrayPacked(ushort[] readTo = null, long knownLength =
717
731
718
732
public ushort [ ] ReadUShortArrayDiff ( ushort [ ] readTo = null , long knownLength = - 1 )
719
733
{
734
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
720
735
if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
721
736
ushort [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new ushort [ knownLength ] : readTo ;
722
737
ulong dBlockStart = bitSource . BitPosition + ( ulong ) ( readTo == null ? 0 : Math . Min ( knownLength , readTo . LongLength ) ) ;
@@ -747,6 +762,7 @@ public ushort[] ReadUShortArrayDiff(ushort[] readTo = null, long knownLength = -
747
762
748
763
public ushort [ ] ReadUShortArrayPackedDiff ( ushort [ ] readTo = null , long knownLength = - 1 )
749
764
{
765
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
750
766
if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
751
767
ushort [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new ushort [ knownLength ] : readTo ;
752
768
ulong data = bitSource . BitPosition + ( ulong ) ( readTo == null ? 0 : Math . Min ( knownLength , readTo . LongLength ) ) ;
@@ -793,6 +809,7 @@ public int[] ReadIntArrayPacked(int[] readTo = null, long knownLength = -1)
793
809
794
810
public int [ ] ReadIntArrayDiff ( int [ ] readTo = null , long knownLength = - 1 )
795
811
{
812
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
796
813
if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
797
814
int [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new int [ knownLength ] : readTo ;
798
815
ulong dBlockStart = bitSource . BitPosition + ( ulong ) ( readTo == null ? 0 : Math . Min ( knownLength , readTo . LongLength ) ) ;
@@ -823,6 +840,7 @@ public int[] ReadIntArrayDiff(int[] readTo = null, long knownLength = -1)
823
840
824
841
public int [ ] ReadIntArrayPackedDiff ( int [ ] readTo = null , long knownLength = - 1 )
825
842
{
843
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
826
844
if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
827
845
int [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new int [ knownLength ] : readTo ;
828
846
ulong data = bitSource . BitPosition + ( ulong ) ( readTo == null ? 0 : Math . Min ( knownLength , readTo . LongLength ) ) ;
@@ -869,6 +887,7 @@ public uint[] ReadUIntArrayPacked(uint[] readTo = null, long knownLength = -1)
869
887
870
888
public uint [ ] ReadUIntArrayDiff ( uint [ ] readTo = null , long knownLength = - 1 )
871
889
{
890
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
872
891
if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
873
892
uint [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new uint [ knownLength ] : readTo ;
874
893
ulong dBlockStart = bitSource . BitPosition + ( ulong ) ( readTo == null ? 0 : Math . Min ( knownLength , readTo . LongLength ) ) ;
@@ -915,6 +934,7 @@ public long[] ReadLongArrayPacked(long[] readTo = null, long knownLength = -1)
915
934
916
935
public long [ ] ReadLongArrayDiff ( long [ ] readTo = null , long knownLength = - 1 )
917
936
{
937
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
918
938
if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
919
939
long [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new long [ knownLength ] : readTo ;
920
940
ulong dBlockStart = bitSource . BitPosition + ( ulong ) ( readTo == null ? 0 : Math . Min ( knownLength , readTo . LongLength ) ) ;
@@ -945,6 +965,7 @@ public long[] ReadLongArrayDiff(long[] readTo = null, long knownLength = -1)
945
965
946
966
public long [ ] ReadLongArrayPackedDiff ( long [ ] readTo = null , long knownLength = - 1 )
947
967
{
968
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
948
969
if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
949
970
long [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new long [ knownLength ] : readTo ;
950
971
ulong data = bitSource . BitPosition + ( ulong ) ( readTo == null ? 0 : Math . Min ( knownLength , readTo . LongLength ) ) ;
@@ -991,6 +1012,7 @@ public ulong[] ReadULongArrayPacked(ulong[] readTo = null, long knownLength = -1
991
1012
992
1013
public ulong [ ] ReadULongArrayDiff ( ulong [ ] readTo = null , long knownLength = - 1 )
993
1014
{
1015
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
994
1016
if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
995
1017
ulong [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new ulong [ knownLength ] : readTo ;
996
1018
ulong dBlockStart = bitSource . BitPosition + ( ulong ) ( readTo == null ? 0 : Math . Min ( knownLength , readTo . LongLength ) ) ;
@@ -1021,6 +1043,7 @@ public ulong[] ReadULongArrayDiff(ulong[] readTo = null, long knownLength = -1)
1021
1043
1022
1044
public ulong [ ] ReadULongArrayPackedDiff ( ulong [ ] readTo = null , long knownLength = - 1 )
1023
1045
{
1046
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
1024
1047
if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
1025
1048
ulong [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new ulong [ knownLength ] : readTo ;
1026
1049
ulong data = bitSource . BitPosition + ( ulong ) ( readTo == null ? 0 : Math . Min ( knownLength , readTo . LongLength ) ) ;
@@ -1067,6 +1090,7 @@ public float[] ReadFloatArrayPacked(float[] readTo = null, long knownLength = -1
1067
1090
1068
1091
public float [ ] ReadFloatArrayDiff ( float [ ] readTo = null , long knownLength = - 1 )
1069
1092
{
1093
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
1070
1094
if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
1071
1095
float [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new float [ knownLength ] : readTo ;
1072
1096
ulong dBlockStart = bitSource . BitPosition + ( ulong ) ( readTo == null ? 0 : Math . Min ( knownLength , readTo . LongLength ) ) ;
@@ -1097,6 +1121,7 @@ public float[] ReadFloatArrayDiff(float[] readTo = null, long knownLength = -1)
1097
1121
1098
1122
public float [ ] ReadFloatArrayPackedDiff ( float [ ] readTo = null , long knownLength = - 1 )
1099
1123
{
1124
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
1100
1125
if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
1101
1126
float [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new float [ knownLength ] : readTo ;
1102
1127
ulong data = bitSource . BitPosition + ( ulong ) ( readTo == null ? 0 : Math . Min ( knownLength , readTo . LongLength ) ) ;
@@ -1143,6 +1168,7 @@ public double[] ReadDoubleArrayPacked(double[] readTo = null, long knownLength =
1143
1168
1144
1169
public double [ ] ReadDoubleArrayDiff ( double [ ] readTo = null , long knownLength = - 1 )
1145
1170
{
1171
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
1146
1172
if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
1147
1173
double [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new double [ knownLength ] : readTo ;
1148
1174
ulong dBlockStart = bitSource . BitPosition + ( ulong ) ( readTo == null ? 0 : Math . Min ( knownLength , readTo . LongLength ) ) ;
@@ -1173,6 +1199,7 @@ public double[] ReadDoubleArrayDiff(double[] readTo = null, long knownLength = -
1173
1199
1174
1200
public double [ ] ReadDoubleArrayPackedDiff ( double [ ] readTo = null , long knownLength = - 1 )
1175
1201
{
1202
+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
1176
1203
if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
1177
1204
double [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new double [ knownLength ] : readTo ;
1178
1205
ulong data = bitSource . BitPosition + ( ulong ) ( readTo == null ? 0 : Math . Min ( knownLength , readTo . LongLength ) ) ;
0 commit comments