Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Frizlab/stream-reader
A simple stream protocol in Swift with concrete implementations
https://github.com/Frizlab/stream-reader
Last synced: about 2 months ago
JSON representation
A simple stream protocol in Swift with concrete implementations
- Host: GitHub
- URL: https://github.com/Frizlab/stream-reader
- Owner: Frizlab
- License: mit
- Created: 2017-08-20T16:30:27.000Z (about 7 years ago)
- Default Branch: main
- Last Pushed: 2024-02-03T01:38:26.000Z (8 months ago)
- Last Synced: 2024-07-13T19:43:47.538Z (2 months ago)
- Language: Swift
- Size: 174 KB
- Stars: 6
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.adoc
- License: License.txt
Awesome Lists containing this project
README
= Stream Reader
François LamboleyA simple stream reader protocol for Swift named `StreamReader`, with two
concrete implementations: `GenericStreamReader` and `DataReader`.The `GenericStreamReader` can read from any `GenericReadStream`, which the
`FileDescriptor` (from https://github.com/apple/swift-system[SystemPackage]),
`FileHandle` and `IntputStream` classes have been made to conform to.== Usage Examples
=== Reading a Stream to the End
[source,swift]
----
let data = ...
let reader = DataReader(data: data)
let readData = try reader.readDataToEnd()
assert(readData == data)
----=== Reading a Stream Until a Delimitor Is Found
[source,swift]
----
let inputStream = ...
let reader = InputStreamReader(stream: inputStream, bufferSize: 1024, bufferSizeIncrement: 512)
/* Read the stream until a newline (whether macOS or Classic MacOS) is found, and returns the data without the newline. */
let (line, separator) = try reader.readData(upTo: [Data("\n".utf8), Data("\r".utf8)], matchingMode: .anyMatchWins, failIfNotFound: false, includeDelimiter: false)
_ = try reader.readData(size: separator.count) /* We must read the line separator before next read, probably :) */
----Note: In the example above, if the file has Windows new lines, this will add an
empty new line after each line (the separator for Windows being `\r\n`).Stream Reader has also a dedicated method to read a line in a stream:
[source,swift]
----
/* Does not return the line separator, _but_ set stream position after the line separator. */
let lineData = try reader.readLine(allowUnixNewLines: true, allowLegacyMacOSNewLines: true, allowWindowsNewLines: true).line
----== TODO
Make the reads async? This would change a lot of things, but the core of the
project should stay the same.Or maybe just be thread-safe, idk.