|
5 | 5 |
|
6 | 6 | import Foundation |
7 | 7 |
|
| 8 | +/// A type that contains functions for reading `Data` byte-by-byte. |
8 | 9 | public class ByteReader { |
9 | 10 |
|
| 11 | + /// Size of the `data` (in bytes). |
10 | 12 | public let size: Int |
| 13 | + |
| 14 | + /// Data which is being read. |
11 | 15 | public let data: Data |
| 16 | + |
| 17 | + /// Offset to the byte in `data` which will be read next. |
12 | 18 | public var offset: Int |
13 | 19 |
|
| 20 | + /** |
| 21 | + True, if `offset` points at any position after the last byte in `data`. |
| 22 | + |
| 23 | + - Note: It generally means that all bytes have been read. |
| 24 | + */ |
| 25 | + |
14 | 26 | public var isFinished: Bool { |
15 | 27 | return self.data.endIndex <= self.offset |
16 | 28 | } |
17 | 29 |
|
| 30 | + /// Creates an instance for reading `data`. |
18 | 31 | public init(data: Data) { |
19 | 32 | self.size = data.count |
20 | 33 | self.data = data |
21 | 34 | self.offset = data.startIndex |
22 | 35 | } |
23 | 36 |
|
| 37 | + /** |
| 38 | + Reads byte and returns it, advancing by one position. |
| 39 | + |
| 40 | + - Warning: Doesn't check for potential out of bounds error, i.e. doesn't check if `isFinished` is true. |
| 41 | + */ |
24 | 42 | public func byte() -> UInt8 { |
25 | 43 | self.offset += 1 |
26 | 44 | return self.data[self.offset - 1] |
27 | 45 | } |
28 | 46 |
|
| 47 | + /** |
| 48 | + Reads `count` bytes and returns them as an array of `UInt8`, advancing by `count` positions. |
| 49 | + |
| 50 | + - Warning: Doesn't check for potential out of bounds errors, i.e. doesn't check if `isFinished` will be true |
| 51 | + at any point during the reading of these bytes. |
| 52 | + */ |
29 | 53 | public func bytes(count: Int) -> [UInt8] { |
30 | 54 | let result = self.data[self.offset..<self.offset + count].toArray(type: UInt8.self, count: count) |
31 | 55 | self.offset += count |
32 | 56 | return result |
33 | 57 | } |
34 | 58 |
|
| 59 | + /** |
| 60 | + Reads 8 bytes and returns them as a `UInt64` number, advancing by 8 positions. |
| 61 | + |
| 62 | + - Warning: Doesn't check for potential out of bounds errors, i.e. doesn't check if `isFinished` will be true |
| 63 | + at any point during the reading of this number. |
| 64 | + */ |
35 | 65 | public func uint64() -> UInt64 { |
36 | 66 | let result = self.data[self.offset..<self.offset + 8].to(type: UInt64.self) |
37 | 67 | self.offset += 8 |
38 | 68 | return result |
39 | 69 | } |
40 | 70 |
|
| 71 | + /** |
| 72 | + Reads 4 bytes and returns them as a `UInt32` number, advancing by 4 positions. |
| 73 | + |
| 74 | + - Warning: Doesn't check for potential out of bounds errors, i.e. doesn't check if `isFinished` will be true |
| 75 | + at any point during the reading of this number. |
| 76 | + */ |
41 | 77 | public func uint32() -> UInt32 { |
42 | 78 | let result = self.data[self.offset..<self.offset + 4].to(type: UInt32.self) |
43 | 79 | self.offset += 4 |
44 | 80 | return result |
45 | 81 | } |
46 | 82 |
|
| 83 | + /** |
| 84 | + Reads 2 bytes and returns them as a `UInt16` number, advancing by 2 positions. |
| 85 | + |
| 86 | + - Warning: Doesn't check for potential out of bounds errors, i.e. doesn't check if `isFinished` will be true |
| 87 | + at any point during the reading of this number. |
| 88 | + */ |
47 | 89 | public func uint16() -> UInt16 { |
48 | 90 | let result = self.data[self.offset..<self.offset + 2].to(type: UInt16.self) |
49 | 91 | self.offset += 2 |
|
0 commit comments