55
66import Foundation
77
8+ /**
9+ A type that contains functions for reading `Data` bit-by-bit and byte-by-byte,
10+ assuming "LSB 0" bit numbering scheme.
11+ */
812public final class LsbBitReader : ByteReader , BitReader {
913
1014 private var bitMask : UInt8 = 1
1115
16+ /// True, if reader's BIT pointer is aligned with the BYTE border.
1217 public var isAligned : Bool {
1318 return self . bitMask == 1
1419 }
1520
21+ /**
22+ Reads bit and returns it, advancing by one BIT position.
23+
24+ - Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
25+ to check if the end is reached.
26+ */
1627 public func bit( ) -> UInt8 {
1728 let bit : UInt8 = self . data [ self . offset] & self . bitMask > 0 ? 1 : 0
1829
@@ -26,6 +37,12 @@ public final class LsbBitReader: ByteReader, BitReader {
2637 return bit
2738 }
2839
40+ /**
41+ Reads `count` bits and returns them as a `Int` number, advancing by `count` BIT positions.
42+
43+ - Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
44+ to check if the end is reached.
45+ */
2946 public func int( fromBits count: Int ) -> Int {
3047 guard count > 0
3148 else { return 0 }
@@ -48,6 +65,13 @@ public final class LsbBitReader: ByteReader, BitReader {
4865 return result
4966 }
5067
68+ /**
69+ Aligns reader's BIT pointer to the BYTE border, i.e. moves BIT pointer to the first BIT of the next BYTE.
70+
71+ - Note: If reader is already aligned, then does nothing.
72+ - Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` AFTER calling this method
73+ to check if the end was reached.
74+ */
5175 public func align( ) {
5276 guard self . bitMask != 1
5377 else { return }
@@ -58,26 +82,61 @@ public final class LsbBitReader: ByteReader, BitReader {
5882
5983 // MARK: ByteReader's methods.
6084
85+ /**
86+ Reads byte and returns it, advancing by one BYTE position.
87+
88+ - Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
89+ to check if the end is reached.
90+ - Precondition: Reader MUST be aligned.
91+ */
6192 public override func byte( ) -> UInt8 {
6293 precondition ( isAligned, " BitReader is not aligned. " )
6394 return super. byte ( )
6495 }
6596
97+ /**
98+ Reads `count` bytes and returns them as an array of `UInt8`, advancing by `count` BYTE positions.
99+
100+ - Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
101+ to check if the end is reached.
102+ - Precondition: Reader MUST be aligned.
103+ */
66104 public override func bytes( count: Int ) -> [ UInt8 ] {
67105 precondition ( isAligned, " BitReader is not aligned. " )
68106 return super. bytes ( count: count)
69107 }
70108
109+ /**
110+ Reads 8 bytes and returns them as a `UInt64` number, advancing by 8 BYTE positions.
111+
112+ - Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
113+ to check if the end is reached.
114+ - Precondition: Reader MUST be aligned.
115+ */
71116 public override func uint64( ) -> UInt64 {
72117 precondition ( isAligned, " BitReader is not aligned. " )
73118 return super. uint64 ( )
74119 }
75120
121+ /**
122+ Reads 4 bytes and returns them as a `UInt32` number, advancing by 4 BYTE positions.
123+
124+ - Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
125+ to check if the end is reached.
126+ - Precondition: Reader MUST be aligned.
127+ */
76128 public override func uint32( ) -> UInt32 {
77129 precondition ( isAligned, " BitReader is not aligned. " )
78130 return super. uint32 ( )
79131 }
80132
133+ /**
134+ Reads 2 bytes and returns them as a `UInt16` number, advancing by 2 BYTE positions.
135+
136+ - Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
137+ to check if the end is reached.
138+ - Precondition: Reader MUST be aligned.
139+ */
81140 public override func uint16( ) -> UInt16 {
82141 precondition ( isAligned, " BitReader is not aligned. " )
83142 return super. uint16 ( )
0 commit comments