|
5 | 5 |
|
6 | 6 | import Foundation |
7 | 7 |
|
| 8 | +/// A type that contains functions for reading `Data` byte-by-byte. |
8 | 9 | public protocol ByteReader: AnyObject { |
9 | 10 |
|
| 11 | + /// Size of the `data` (in bytes). |
10 | 12 | var size: Int { get } |
11 | 13 |
|
| 14 | + /// Data which is being read. |
12 | 15 | var data: Data { get } |
13 | 16 |
|
| 17 | + /// Offset to a byte in the `data` which will be read next. |
14 | 18 | var offset: Int { get set } |
15 | 19 |
|
| 20 | + /// Creates an instance for reading bytes from the `data`. |
16 | 21 | init(data: Data) |
17 | 22 |
|
| 23 | + /// Reads a byte and returns it, advancing by one position. |
18 | 24 | func byte() -> UInt8 |
19 | 25 |
|
| 26 | + /// Reads `count` bytes and returns them as a `[UInt8]` array, advancing by `count` positions. |
20 | 27 | func bytes(count: Int) -> [UInt8] |
21 | 28 |
|
| 29 | + /// Reads `fromBytes` bytes and returns them as a `Int` number, advancing by `fromBytes` positions. |
22 | 30 | func int(fromBytes count: Int) -> Int |
23 | 31 |
|
| 32 | + /// Reads `fromBytes` bytes and returns them as a `UInt64` number, advancing by `fromBytes` positions. |
24 | 33 | func uint64(fromBytes count: Int) -> UInt64 |
25 | 34 |
|
| 35 | + /// Reads `fromBytes` bytes and returns them as a `UInt32` number, advancing by `fromBytes` positions. |
26 | 36 | func uint32(fromBytes count: Int) -> UInt32 |
27 | 37 |
|
| 38 | + /// Reads `fromBytes` bytes and returns them as a `UInt16` number, advancing by `fromBytes` positions. |
28 | 39 | func uint16(fromBytes count: Int) -> UInt16 |
29 | 40 |
|
30 | 41 | } |
31 | 42 |
|
32 | 43 | extension ByteReader { |
33 | 44 |
|
| 45 | + /// Creates an instance for reading bytes by using the `data` and the `offset` of the specified `BitReader`. |
34 | 46 | public init(_ bitReader: BitReader) { |
35 | 47 | self.init(data: bitReader.data) |
36 | 48 | self.offset = bitReader.offset |
37 | 49 | } |
38 | 50 |
|
| 51 | + /// Amount of bytes left to read. |
39 | 52 | public var bytesLeft: Int { |
40 | 53 | return data.endIndex - offset |
41 | 54 | } |
42 | 55 |
|
| 56 | + /// Amount of bytes that were already read. |
43 | 57 | public var bytesRead: Int { |
44 | 58 | return offset - data.startIndex |
45 | 59 | } |
46 | 60 |
|
| 61 | + /** |
| 62 | + True, if the `offset` points at any position after the last byte in `data`, which generally means that all data |
| 63 | + has been read. |
| 64 | + */ |
47 | 65 | public var isFinished: Bool { |
48 | 66 | return data.endIndex <= offset |
49 | 67 | } |
50 | 68 |
|
| 69 | + /** |
| 70 | + Reads `fromBytes` bytes by either using `uint64(fromBytes:)` or `uint32(fromBytes:)` depending on the platform's |
| 71 | + integer bit width, converts the result to `Int`, and returns it, advancing by `fromBytes` positions. |
| 72 | + */ |
51 | 73 | public func int(fromBytes count: Int) -> Int { |
52 | 74 | if MemoryLayout<Int>.size == 8 { |
53 | 75 | return Int(truncatingIfNeeded: self.uint64(fromBytes: count)) |
|
0 commit comments