|
1 |
| -= SimpleStream |
| 1 | += Stream Reader |
2 | 2 | François Lamboley <fload@me.com>
|
3 | 3 |
|
4 |
| -A simple stream protocol for Swift named `SimpleStream`, with two concrete implementations: |
5 |
| -`SimpleGenericReadStream` and `SimpleDataStream`. |
| 4 | +A simple stream reader protocol for Swift named `StreamReader`, with two |
| 5 | +concrete implementations: `GenericStreamReader` and `DataReader`. |
6 | 6 |
|
7 |
| -The `SimpleGenericReadStream` can read from any `GenericReadStream`, which the |
8 |
| -`FileHandle` and `IntputStream` classes have been made conform to. |
| 7 | +The `GenericStreamReader` can read from any `GenericReadStream`, which the |
| 8 | +`FileDescriptor` (from https://github.com/apple/swift-system[SystemPackage]), |
| 9 | +`FileHandle` and `IntputStream` classes have been made to conform to. |
9 | 10 |
|
10 | 11 | == Usage Examples
|
11 | 12 | === Reading a Stream to the End
|
12 | 13 | [source,swift]
|
13 | 14 | ----
|
14 |
| -let ds = SimpleDataStream(data: d) |
15 |
| -let rd = try ds.readDataToEnd() |
16 |
| -assert(rd == d) |
| 15 | +let data = ... |
| 16 | +let reader = DataReader(data: data) |
| 17 | +let readData = try reader.readDataToEnd() |
| 18 | +assert(readData == data) |
17 | 19 | ----
|
18 | 20 |
|
19 |
| -### Reading a Stream Until a Delimitor Is Found |
| 21 | +=== Reading a Stream Until a Delimitor Is Found |
20 | 22 | [source,swift]
|
21 | 23 | ----
|
22 |
| -let s = SimpleInputStream(stream: yourInputStream, bufferSize: 1024, bufferSizeIncrement: 512, streamReadSizeLimit: nil) |
23 |
| -/* Read the stream until a newline (whether macOS or Classic Mac OS) is found, |
24 |
| - * does not include the newline in the result. */ |
25 |
| -let line = try s.readData(upTo: [Data("\n".utf8), Data("\r".utf8)], matchingMode: .anyMatchWins, includeDelimiter: false).data |
26 |
| -/* Note: If the file has Windows new lines, this will add an empty new line |
27 |
| - * after each line (the separator for Windows being "\r\n"). */ |
| 24 | +let inputStream = ... |
| 25 | +let reader = InputStreamReader(stream: inputStream, bufferSize: 1024, bufferSizeIncrement: 512) |
| 26 | +/* Read the stream until a newline (whether macOS or Classic MacOS) is found, and returns the data without the newline. */ |
| 27 | +let (line, separator) = try reader.readData(upTo: [Data("\n".utf8), Data("\r".utf8)], matchingMode: .anyMatchWins, failIfNotFound: false, includeDelimiter: false) |
| 28 | +_ = try reader.readData(size: separator.count) /* We must read the line separator before next read, probably :) */ |
| 29 | +---- |
| 30 | + |
| 31 | +Note: In the example above, if the file has Windows new lines, this will add an |
| 32 | +empty new line after each line (the separator for Windows being `\r\n`). |
| 33 | + |
| 34 | +Stream Reader has also a dedicated method to read a line in a stream: |
| 35 | +[source,swift] |
| 36 | +---- |
| 37 | +/* Does not return the line separator, _but_ set stream position after the line separator. */ |
| 38 | +let lineData = try reader.readLine(allowUnixNewLines: true, allowLegacyMacOSNewLines: true, allowWindowsNewLines: true).line |
28 | 39 | ----
|
29 | 40 |
|
30 | 41 | == TODO
|
31 |
| -Make the reads async! This will change a lot of things, but the core of the project should stay the same. |
| 42 | +Make the reads async? This would change a lot of things, but the core of the |
| 43 | +project should stay the same. |
| 44 | + |
| 45 | +Or maybe just be thread-safe, idk. |
0 commit comments