SKStream

public class SKStream

bstraction for a source of bytes. Subclasses can be backed by memory, or a file, or something else.

Classic streams APIs are sort of async, in that on a request for N bytes, they may return fewer than N bytes on a given call, in which case the caller can try again to get more bytes, eventually (modulo an error) receiving their total N bytes.

Skia streams behave differently. They are effectively synchronous, and will always return all N bytes of the request if possible. If they return fewer (the read call returns the number of bytes read) then that means there is no more data (at EOF or hit an error). The caller should not call again in hopes of fulfilling more of the request.

  • Returns true when all the bytes in the stream have been read.

    This may return true early (when there are no more bytes to be read) or late (after the first unsuccessful read).

    Declaration

    Swift

    public var isAtEnd: Bool { get }
  • Reads the next Int8 from the stream, returns nil if the end was reached before the read completed

    Declaration

    Swift

    public func readInt8() -> Int8?
  • Reads the next Int16 from the stream, returns nil if the end was reached before the read completed

    Declaration

    Swift

    public func readInt16() -> Int16?
  • Reads the next Int32 from the stream, returns nil if the end was reached before the read completed

    Declaration

    Swift

    public func readInt32() -> Int32?
  • Reads the next UInt8 from the stream, returns nil if the end was reached before the read completed

    Declaration

    Swift

    public func readUInt8() -> UInt8?
  • Reads the next UInt16 from the stream, returns nil if the end was reached before the read completed

    Declaration

    Swift

    public func readUInt16() -> UInt16?
  • Reads the next UInt16 from the stream, returns nil if the end was reached before the read completed

    Declaration

    Swift

    public func readUInt32() -> UInt32?
  • Reads or skips size number of bytes. If buffer == nil, skip size bytes, return how many were skipped. If buffer != nil, copy size bytes into buffer, return how many were copied.

    Declaration

    Swift

    public func read(_ buffer: inout [UInt8], _ size: Int)

    Parameters

    buffer

    copy size bytes into buffer

    size

    the number of bytes to skip or copy

    Return Value

    the number of bytes actually read.

  • Attempt to peek at size bytes. If this stream supports peeking, copy min(size, peekable bytes) into buffer, and return the number of bytes copied. If the stream does not support peeking, or cannot peek any bytes, return 0 and leave buffer unchanged. The stream is guaranteed to be in the same visible state after this call, regardless of success or failure.

    Declaration

    Swift

    public func peek(_ buffer: inout [UInt8], _ size: Int)

    Parameters

    buffer

    Must not be nil, and must be at least size bytes. Destination to copy bytes.

    size

    Number of bytes to copy.

    Return Value

    The number of bytes peeked/copied.

  • Attempts to skip the specified number of bytes.

    Declaration

    Swift

    public func skip(_ size: Int) -> Int

    Return Value

    The number of bytes actually skipped.

  • Rewinds to the beginning of the stream. Returns true if the stream is known to be at the beginning after this call returns.

    Declaration

    Swift

    public func rewind()
  • Changes the stream position by the specified value in the stream. If this cannot be done, returns false. If an attempt is made to seek past the end of the stream, the position will be set to the end of the stream.

    Declaration

    Swift

    public func seek(_ delta: Int) -> Bool

    Parameters

    delta

    the number of bytes to seek forward or backwards

    Return Value

    true if it was possible to seek to that position, false otherwise

  • Seeks to an absolute position in the stream. If this cannot be done, returns false. If an attempt is made to seek past the end of the stream, the position will be set to the end of the stream.

    Declaration

    Swift

    public func seekAbsolute(_ position: Int) -> Bool

    Parameters

    position

    the target position for this stream

    Return Value

    true if it was possible to seek to that position, false otherwise

  • Returns the starting address for the data. If this cannot be done, returns nil.

    Declaration

    Swift

    public func getMemoryBase() -> UnsafeRawPointer!
  • Returns true if this stream can report it’s current position.

    Declaration

    Swift

    public var hasPosition: Bool { get }
  • Returns the current position in the stream. If this cannot be done, returns 0

    Declaration

    Swift

    public var position: Int { get }
  • Returns true if this stream can report it’s total length.

    Declaration

    Swift

    public var hasLength: Bool { get }
  • Returns the total length of the stream. If this cannot be done, returns 0.

    Declaration

    Swift

    public var length: Int { get }