/
ReadableStream.swift
60 lines (49 loc) 路 1.69 KB
/
ReadableStream.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/// A readable byte stream
public protocol ReadableStream: Stream {
func read(max: Int, into buffer: inout Bytes) throws -> Int
}
extension ReadableStream {
/// convenenience for not requiring a buffer to be passed
public func read(max: Int) throws -> Bytes {
var buffer = Bytes(repeating: 0, count: max)
let receivedCount = try read(max: max, into: &buffer)
return Array(buffer[0..<receivedCount])
}
/// Reads and filters non-valid ASCII characters
/// from the stream until a new line character is returned.
public func readLine() throws -> Bytes {
var line: Bytes = []
var lastByte: Byte? = nil
while let byte = try readByte() {
// Continues until a `crlf` sequence is found
if byte == .newLine && lastByte == .carriageReturn {
break
}
// Skip over any non-valid ASCII characters
if byte > .carriageReturn {
line += byte
}
lastByte = byte
}
return line
}
/// Reads all bytes from the stream using
/// a chunk size.
public func readAll(chunkSize: Int = 512) throws -> Bytes {
var lastSize = 0
var bytes: Bytes = []
while lastSize < chunkSize {
let chunk = try read(max: chunkSize)
bytes += chunk
lastSize = chunk.count
}
return bytes
}
/// Default implementation of receive grabs a one
/// byte array from the stream and returns the first.
///
/// This can be overridden with something more performant.
public func readByte() throws -> Byte? {
return try read(max: 1).first
}
}