@@ -45,19 +45,28 @@ public BitReader(Stream stream)
4545 /// Reads a single bit
4646 /// </summary>
4747 /// <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+ }
4953
5054 /// <summary>
5155 /// Reads a single bit
5256 /// </summary>
5357 /// <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+ }
5563
5664 /// <summary>
5765 /// Skips pad bits and aligns the position to the next byte
5866 /// </summary>
5967 public void SkipPadBits ( )
6068 {
69+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
6170 while ( ! bitSource . BitAligned ) ReadBit ( ) ;
6271 }
6372
@@ -279,6 +288,7 @@ public Quaternion ReadRotation(int bytesPerAngle)
279288 /// <returns>The bits that were read</returns>
280289 public ulong ReadBits ( int bitCount )
281290 {
291+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
282292 if ( bitCount > 64 ) throw new ArgumentOutOfRangeException ( "Cannot read more than 64 bits into a 64-bit value!" ) ;
283293 if ( bitCount < 0 ) throw new ArgumentOutOfRangeException ( "Cannot read fewer than 0 bits!" ) ;
284294 ulong read = 0 ;
@@ -293,42 +303,38 @@ public ulong ReadBits(int bitCount)
293303 /// <param name="bitCount">How many bits to read. Minimum 0, maximum 64.</param>
294304 /// <returns>The bits that were read</returns>
295305 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+
303311 int result = 0 ;
304312 ByteBool convert = new ByteBool ( ) ;
305313 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+ }
311317
312318 /// <summary>
313319 /// Read a nibble (4 bits) from the stream.
314320 /// </summary>
315321 /// <param name="asUpper">Whether or not the nibble should be left-shifted by 4 bits</param>
316322 /// <returns>The nibble that was read</returns>
317323 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 ( ) ) |
323330 ( convert . Collapse ( ReadBit ( ) ) << 1 ) |
324331 ( convert . Collapse ( ReadBit ( ) ) << 2 ) |
325332 ( convert . Collapse ( ReadBit ( ) ) << 3 )
326- )
327- ;
333+ ) ;
328334 if ( asUpper ) result <<= 4 ;
329- return result
330- ;
331- }
335+ return result ;
336+
337+ }
332338
333339 // Marginally faster than the one that accepts a bool
334340 /// <summary>
@@ -337,13 +343,14 @@ public byte ReadNibble(bool asUpper)
337343 /// <returns>The nibble that was read</returns>
338344 public byte ReadNibble ( )
339345 {
346+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
340347 ByteBool convert = new ByteBool ( ) ;
341- return ( byte ) (
342- convert . Collapse ( ReadBit ( ) ) |
348+ return ( byte ) (
349+ convert . Collapse ( ReadBit ( ) ) |
343350 ( convert . Collapse ( ReadBit ( ) ) << 1 ) |
344351 ( convert . Collapse ( ReadBit ( ) ) << 2 ) |
345352 ( convert . Collapse ( ReadBit ( ) ) << 3 )
346- ) ;
353+ ) ;
347354 }
348355
349356 public sbyte ReadSByte ( ) => ( sbyte ) ReadByte ( ) ;
@@ -464,6 +471,7 @@ public StringBuilder ReadStringPacked(StringBuilder builder = null)
464471 public StringBuilder ReadStringDiff ( string compare , bool oneByteChars = false ) => ReadStringDiff ( null , compare , oneByteChars ) ;
465472 public StringBuilder ReadStringDiff ( StringBuilder builder , string compare , bool oneByteChars = false )
466473 {
474+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
467475 int expectedLength = ( int ) ReadUInt32Packed ( ) ;
468476 if ( builder == null ) builder = new StringBuilder ( expectedLength ) ;
469477 else if ( builder . Capacity < expectedLength ) builder . Capacity = expectedLength ;
@@ -495,6 +503,7 @@ public StringBuilder ReadStringDiff(StringBuilder builder, string compare, bool
495503
496504 public StringBuilder ReadStringDiff ( StringBuilder compareAndBuffer , bool oneByteChars = false )
497505 {
506+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
498507 int expectedLength = ( int ) ReadUInt32Packed ( ) ;
499508 if ( compareAndBuffer == null ) throw new ArgumentNullException ( "Buffer cannot be null" ) ;
500509 else if ( compareAndBuffer . Capacity < expectedLength ) compareAndBuffer . Capacity = expectedLength ;
@@ -526,6 +535,7 @@ public StringBuilder ReadStringDiff(StringBuilder compareAndBuffer, bool oneByte
526535 public StringBuilder ReadStringPackedDiff ( string compare ) => ReadStringPackedDiff ( null , compare ) ;
527536 public StringBuilder ReadStringPackedDiff ( StringBuilder builder , string compare )
528537 {
538+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
529539 int expectedLength = ( int ) ReadUInt32Packed ( ) ;
530540 if ( builder == null ) builder = new StringBuilder ( expectedLength ) ;
531541 else if ( builder . Capacity < expectedLength ) builder . Capacity = expectedLength ;
@@ -557,6 +567,7 @@ public StringBuilder ReadStringPackedDiff(StringBuilder builder, string compare)
557567
558568 public StringBuilder ReadStringPackedDiff ( StringBuilder compareAndBuffer )
559569 {
570+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
560571 int expectedLength = ( int ) ReadUInt32Packed ( ) ;
561572 if ( compareAndBuffer == null ) throw new ArgumentNullException ( "Buffer cannot be null" ) ;
562573 else if ( compareAndBuffer . Capacity < expectedLength ) compareAndBuffer . Capacity = expectedLength ;
@@ -595,6 +606,7 @@ public byte[] ReadByteArray(byte[] readTo = null, long knownLength = -1)
595606
596607 public byte [ ] ReadByteArrayDiff ( byte [ ] readTo = null , long knownLength = - 1 )
597608 {
609+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
598610 if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
599611 byte [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new byte [ knownLength ] : readTo ;
600612 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
641653
642654 public short [ ] ReadShortArrayDiff ( short [ ] readTo = null , long knownLength = - 1 )
643655 {
656+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
644657 if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
645658 short [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new short [ knownLength ] : readTo ;
646659 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)
671684
672685 public short [ ] ReadShortArrayPackedDiff ( short [ ] readTo = null , long knownLength = - 1 )
673686 {
687+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
674688 if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
675689 short [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new short [ knownLength ] : readTo ;
676690 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 =
717731
718732 public ushort [ ] ReadUShortArrayDiff ( ushort [ ] readTo = null , long knownLength = - 1 )
719733 {
734+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
720735 if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
721736 ushort [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new ushort [ knownLength ] : readTo ;
722737 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 = -
747762
748763 public ushort [ ] ReadUShortArrayPackedDiff ( ushort [ ] readTo = null , long knownLength = - 1 )
749764 {
765+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
750766 if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
751767 ushort [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new ushort [ knownLength ] : readTo ;
752768 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)
793809
794810 public int [ ] ReadIntArrayDiff ( int [ ] readTo = null , long knownLength = - 1 )
795811 {
812+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
796813 if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
797814 int [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new int [ knownLength ] : readTo ;
798815 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)
823840
824841 public int [ ] ReadIntArrayPackedDiff ( int [ ] readTo = null , long knownLength = - 1 )
825842 {
843+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
826844 if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
827845 int [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new int [ knownLength ] : readTo ;
828846 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)
869887
870888 public uint [ ] ReadUIntArrayDiff ( uint [ ] readTo = null , long knownLength = - 1 )
871889 {
890+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
872891 if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
873892 uint [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new uint [ knownLength ] : readTo ;
874893 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)
915934
916935 public long [ ] ReadLongArrayDiff ( long [ ] readTo = null , long knownLength = - 1 )
917936 {
937+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
918938 if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
919939 long [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new long [ knownLength ] : readTo ;
920940 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)
945965
946966 public long [ ] ReadLongArrayPackedDiff ( long [ ] readTo = null , long knownLength = - 1 )
947967 {
968+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
948969 if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
949970 long [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new long [ knownLength ] : readTo ;
950971 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
9911012
9921013 public ulong [ ] ReadULongArrayDiff ( ulong [ ] readTo = null , long knownLength = - 1 )
9931014 {
1015+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
9941016 if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
9951017 ulong [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new ulong [ knownLength ] : readTo ;
9961018 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)
10211043
10221044 public ulong [ ] ReadULongArrayPackedDiff ( ulong [ ] readTo = null , long knownLength = - 1 )
10231045 {
1046+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
10241047 if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
10251048 ulong [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new ulong [ knownLength ] : readTo ;
10261049 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
10671090
10681091 public float [ ] ReadFloatArrayDiff ( float [ ] readTo = null , long knownLength = - 1 )
10691092 {
1093+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
10701094 if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
10711095 float [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new float [ knownLength ] : readTo ;
10721096 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)
10971121
10981122 public float [ ] ReadFloatArrayPackedDiff ( float [ ] readTo = null , long knownLength = - 1 )
10991123 {
1124+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
11001125 if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
11011126 float [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new float [ knownLength ] : readTo ;
11021127 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 =
11431168
11441169 public double [ ] ReadDoubleArrayDiff ( double [ ] readTo = null , long knownLength = - 1 )
11451170 {
1171+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
11461172 if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
11471173 double [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new double [ knownLength ] : readTo ;
11481174 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 = -
11731199
11741200 public double [ ] ReadDoubleArrayPackedDiff ( double [ ] readTo = null , long knownLength = - 1 )
11751201 {
1202+ if ( bitSource == null ) throw new InvalidOperationException ( "Cannot read bits on a non BitStream stream" ) ;
11761203 if ( knownLength < 0 ) knownLength = ( long ) ReadUInt64Packed ( ) ;
11771204 double [ ] writeTo = readTo == null || readTo . LongLength != knownLength ? new double [ knownLength ] : readTo ;
11781205 ulong data = bitSource . BitPosition + ( ulong ) ( readTo == null ? 0 : Math . Min ( knownLength , readTo . LongLength ) ) ;
0 commit comments