ByteStream

open class ByteStream: StreamBase

Abstract base class for byte streams.

  • Immutable empty byte stream, for which readByte will always return -1.

    Declaration

    Swift

    public static let empty: ByteStream = EmptyByteStream()
  • Default initializer.

    Declaration

    Swift

    override public init()
  • Assuming this stream is a UTF-8 byte stream, map it to a character stream (of UTF-16 code units).

    Declaration

    Swift

    open func asText() -> CharStream

    Return Value

    New character stream, which reads from this UTF-8 byte stream and converts to UTF-16 code units.

  • Does this stream support undoRead?

    See also

    withUndo.

    Declaration

    Swift

    open var canUndo: Bool
  • Copy all remaining data until the end of this stream into a target stream, then close this stream.

    Declaration

    Swift

    open func copyTo(target: ByteStream, closeTarget: Bool = true) throws -> Void

    Parameters

    target

    Output stream.

    closeTarget

    Close target stream? (defaults to true)

  • Copy all remaining data until the end of the stream into a file, then close this stream.

    Declaration

    Swift

    open func copyToFile(_ file: String) throws -> Void

    Parameters

    file

    Output file name.

  • Data type with a DataType.code of DataType.BYTE_STREAM.

    Declaration

    Swift

    override open var dataType: DataType
  • Declaration

    Swift

    open class func fileWriter(file: String) throws -> ByteStream

    Parameters

    file

    Output file name.

    Return Value

    A stream that will write the contents of a data file.

  • Declaration

    Swift

    open class func fromBinary(data: Data, offset: Int = (0 as Int), length: Int = (2147483647 as Int)) -> ByteStream

    Parameters

    data

    Source data.

    offset

    Starting offset (zero-based).

    length

    Maximum number of bytes to read.

    Return Value

    A new stream for reading from a binary value.

  • Declaration

    Swift

    open class func fromFile(_ file: String) throws -> ByteStream

    Parameters

    file

    Input file name.

    Return Value

    A stream that will read the contents of a data file.

  • Declaration

    Swift

    open class func fromStream(_ stream: StreamBase) -> ByteStream

    Parameters

    stream

    Underlying stream.

    Return Value

    stream (if stream is a ByteStream) or ByteStream.fromText(stream) (if stream is a CharStream).

  • Declaration

    Swift

    open class func fromText(stream: CharStream) -> ByteStream

    Parameters

    stream

    Underlying character stream.

    Return Value

    A new UTF-8 byte stream which reads from an underlying UTF-16 character stream.

  • Read all remaining data until the end of the stream, then close this stream. Caution: this function loads all stream content into memory at once. Consider reading a chunk-at-a-time in a loop, using readBinary.

    Declaration

    Swift

    open func readAndClose() throws -> Data

    Return Value

    The remaining data.

  • Read up to length bytes from this stream (may read less, even if the end of stream is not reached).

    Declaration

    Swift

    open func readBinary(length: Int = (20000 as Int)) throws -> Data?

    Parameters

    length

    Maximum number of bytes to read (must be greater than zero). Defaults to 20,000.

    Return Value

    Binary value with at most length bytes, or nil if there is no remaining data in the stream.

  • Read a single byte from this stream.

    Declaration

    Swift

    open func readByte() throws -> Int

    Return Value

    Unsigned integer equivalent of the byte read, or -1 at end of stream.

  • Declaration

    Swift

    open class func toBuffer(_ buffer: ByteBuffer) -> ByteStream

    Parameters

    buffer

    Buffer to be written to.

    Return Value

    A new byte stream which writes to a provided buffer.

  • Convert this data value to a string. If the dataType is defined by XML Schema Part 2: Datatypes, then the corresponding lexical format is used. JSON format is used for structured values (arrays and objects).

    Declaration

    Swift

    override open func toString() -> String

    Return Value

    Lexical representation of this data value.

  • Undo read of one byte.

    Throws

    DataStreamException if undo is not supported by this stream.

    Declaration

    Swift

    open func undoRead(_ value: Int) throws -> Void

    Parameters

    value

    Byte value that was previously read by the caller and is to be made available to read again.

  • See also

    canUndo.

    Declaration

    Swift

    open func withUndo() throws -> ByteStream

    Return Value

    A wrapper (if needed) of this stream supporting undoRead, or this stream if it already supports undoRead.

  • Write to this stream the portion of data from the specified offset with the specified length.

    Declaration

    Swift

    open func writeBinary(data: Data) throws -> Void

    Parameters

    data

    Source data.

  • Write a single byte to this stream.

    Declaration

    Swift

    open func writeByte(_ value: Int) throws -> Void

    Parameters

    value

    Byte value to be written.