Skip to content

Commit 3af5128

Browse files
committed
ByteArrayOutputStream API never throws
1 parent dca42bc commit 3af5128

File tree

3 files changed

+23
-30
lines changed

3 files changed

+23
-30
lines changed

Sources/Stream/Library/ByteArrayStream/ByteArrayOutputStream+StreamWriter.swift

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,34 @@ extension ByteArrayOutputStream: StreamWriter {
33
return bytes.count - position
44
}
55

6-
public func write(_ byte: UInt8) throws {
6+
public func write(_ byte: UInt8) {
77
bytes.append(byte)
88
}
99

10-
public func write<T: FixedWidthInteger>(_ value: T) throws {
10+
public func write<T: FixedWidthInteger>(_ value: T) {
1111
var value = value.bigEndian
1212
withUnsafeBytes(of: &value) { buffer in
1313
bytes.append(contentsOf: buffer)
1414
}
1515
}
1616

17-
public func write(_ bytes: UnsafeRawPointer, byteCount: Int) throws {
18-
var written = 0
19-
while written < byteCount {
20-
let count = try write(from: bytes, byteCount: byteCount)
21-
guard count > 0 else {
22-
throw StreamError.insufficientData
23-
}
24-
written += count
25-
}
17+
public func write(_ bytes: UnsafeRawPointer, byteCount: Int) {
18+
_ = write(from: bytes, byteCount: byteCount)
2619
}
2720

2821
// MARK: Override StreamWriter async functions
2922

30-
public func write(_ bytes: UnsafeRawBufferPointer) throws {
31-
try write(bytes.baseAddress!, byteCount: bytes.count)
23+
public func write(_ bytes: UnsafeRawBufferPointer) {
24+
write(bytes.baseAddress!, byteCount: bytes.count)
3225
}
3326

34-
public func write(_ bytes: [UInt8]) throws {
35-
try write(bytes, byteCount: bytes.count)
27+
public func write(_ bytes: [UInt8]) {
28+
write(bytes, byteCount: bytes.count)
3629
}
3730

38-
public func write(_ string: String) throws {
39-
try write([UInt8](string.utf8))
31+
public func write(_ string: String) {
32+
write([UInt8](string.utf8))
4033
}
4134

42-
public func flush() throws {}
35+
public func flush() {}
4336
}

Sources/Stream/Library/ByteArrayStream/ByteArrayOutputStream.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class ByteArrayOutputStream: OutputStream {
1111
@inline(__always)
1212
public func write(
1313
from buffer: UnsafeRawPointer,
14-
byteCount: Int) throws -> Int
14+
byteCount: Int) -> Int
1515
{
1616
bytes.append(contentsOf: UnsafeRawBufferPointer(
1717
start: buffer,

Tests/Stream/ByteArrayOutputStream/main.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ import Test
44
test.case("ByteArrayOutputStream") {
55
let outputStream = ByteArrayOutputStream()
66
let bytes = [UInt8]()
7-
expect(try outputStream.write(from: bytes, byteCount: 0) == 0)
7+
expect(outputStream.write(from: bytes, byteCount: 0) == 0)
88
}
99

1010
test.case("ByteArrayOutputStream Numeric") {
1111
let outputStream = ByteArrayOutputStream()
1212

13-
try outputStream.write(Int(-1))
14-
try outputStream.write(Int8(-2))
15-
try outputStream.write(Int16(-3))
16-
try outputStream.write(Int32(-4))
17-
try outputStream.write(Int64(-5))
18-
try outputStream.write(UInt(1))
19-
try outputStream.write(UInt8(2))
20-
try outputStream.write(UInt16(3))
21-
try outputStream.write(UInt32(4))
22-
try outputStream.write(UInt64(5))
13+
outputStream.write(Int(-1))
14+
outputStream.write(Int8(-2))
15+
outputStream.write(Int16(-3))
16+
outputStream.write(Int32(-4))
17+
outputStream.write(Int64(-5))
18+
outputStream.write(UInt(1))
19+
outputStream.write(UInt8(2))
20+
outputStream.write(UInt16(3))
21+
outputStream.write(UInt32(4))
22+
outputStream.write(UInt64(5))
2323

2424
let inputStream = ByteArrayInputStream(outputStream.bytes)
2525

0 commit comments

Comments
 (0)