Files
container/Sources/ContainerizationIO/ReadStream.swift
T
Danny Canter 6bc4bf5124 Source code documentation updates (#9)
This change adds documentation to quite a few existing public types that
didn't have a blurb before.

Additionally, this fixes a couple things that I think either didn't make
sense when going to document them:
- Rename ConnectionStream to VsockConnectionStream. This type only
functions for vsock connections.
- Deletes NsLock+Closure. This was not used anywhere.
- Rename ContainerizationOCI/Config.swift to ImageConfig.swift.

Signed-off-by: Danny Canter <danny_canter@apple.com>
2025-06-05 16:16:18 -07:00

129 lines
4.1 KiB
Swift

//===----------------------------------------------------------------------===//
// Copyright © 2025 Apple Inc. and the containerization project authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//
import ContainerizationOS
import Foundation
import NIO
/// `ReadStream` is a utility type for streaming data from a `URL`
/// or `Data` blob.
public class ReadStream {
public static let bufferSize = Int(1.mib())
private var _stream: InputStream
private let _buffSize: Int
private let _data: Data?
private let _url: URL?
public init() {
_stream = InputStream(data: .init())
_buffSize = Self.bufferSize
self._data = Data()
self._url = nil
}
public init(url: URL, bufferSize: Int = bufferSize) throws {
guard FileManager.default.fileExists(atPath: url.path) else {
throw Error.noSuchFileOrDirectory(url)
}
guard let stream = InputStream(url: url) else {
throw Error.failedToCreateStream
}
self._stream = stream
self._buffSize = bufferSize
self._url = url
self._data = nil
}
public init(data: Data, bufferSize: Int = bufferSize) {
self._stream = InputStream(data: data)
self._buffSize = bufferSize
self._url = nil
self._data = data
}
public func reset() throws {
self._stream.close()
if let url = self._url {
guard let s = InputStream(url: url) else {
throw Error.failedToCreateStream
}
self._stream = s
return
}
let data = self._data ?? Data()
self._stream = InputStream(data: data)
}
public var stream: AsyncStream<ByteBuffer> {
AsyncStream { cont in
self._stream.open()
defer { self._stream.close() }
let readBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: _buffSize)
while true {
let byteRead = self._stream.read(readBuffer, maxLength: _buffSize)
if byteRead <= 0 {
readBuffer.deallocate()
cont.finish()
break
} else {
let data = Data(bytes: readBuffer, count: byteRead)
let buffer = ByteBuffer(bytes: data)
cont.yield(buffer)
}
}
}
}
public var dataStream: AsyncStream<Data> {
AsyncStream { cont in
self._stream.open()
defer { self._stream.close() }
let readBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: self._buffSize)
while true {
let byteRead = self._stream.read(readBuffer, maxLength: self._buffSize)
if byteRead <= 0 {
readBuffer.deallocate()
cont.finish()
break
} else {
let data = Data(bytes: readBuffer, count: byteRead)
cont.yield(data)
}
}
}
}
}
extension ReadStream {
enum Error: Swift.Error, CustomStringConvertible {
case failedToCreateStream
case noSuchFileOrDirectory(_ p: URL)
var description: String {
switch self {
case .failedToCreateStream:
return "failed to create stream"
case .noSuchFileOrDirectory(let p):
return "no such file or directory: \(p.path)"
}
}
}
}