CharStream

open class CharStream : StreamBase

Abstract base class for char streams, using UTF-16 code units.

  • Immutable empty char stream, for which readChar will always return -1.

    Declaration

    Swift

    public static let empty: CharStream
  • Immutable empty char stream (representing the case where there is no stream available), for which readChar will always return -1.

    See also

    empty.

    Declaration

    Swift

    public static let none: CharStream
  • Default initializer.

    Declaration

    Swift

    override public init()
  • Does this stream support undoRead?

    See also

    withUndo.

    Declaration

    Swift

    open var canUndo: Bool { get }
  • Copy all remaining text until the end of this stream into a target stream, then close this stream. Also propagate the entityTag and mediaType of this stream into the target stream.

    Declaration

    Swift

    open func copyTo(target: CharStream, closeTarget: Bool = true) throws

    Parameters

    target

    Output stream.

    closeTarget

    Close target stream? (defaults to true)

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

    Declaration

    Swift

    open func copyToFile(_ file: String) throws

    Parameters

    file

    Output file name.

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

    Declaration

    Swift

    override open var dataType: DataType { get }
  • Declaration

    Swift

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

    Parameters

    file

    Output file name.

    Return Value

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

  • Declaration

    Swift

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

    Parameters

    file

    Input file name.

    Return Value

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

  • Declaration

    Swift

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

    Parameters

    stream

    Underlying stream.

    Return Value

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

  • Declaration

    Swift

    open class func fromString(text: String, offset: Int = (0 as Int), length: Int = (2147483647 as Int)) -> CharStream

    Parameters

    text

    Source text.

    offset

    Starting offset (zero-based).

    length

    Maximum number of characters to read.

    Return Value

    A new stream for reading from a string value.

  • Read all remaining text until the end of this 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 readString.

    Declaration

    Swift

    open func readAndClose() throws -> String

    Return Value

    The remaining text.

  • Read a single character (UTF-16 code unit) from this stream.

    Declaration

    Swift

    open func readChar() throws -> Int

    Return Value

    Unsigned integer equivalent of the character read (a UTF-16 code unit), or -1 at end of stream.

  • Read a line of text content from the stream.

    Declaration

    Swift

    open func readLine() throws -> String?

    Return Value

    Text content, or nil at end of stream.

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

    Declaration

    Swift

    open func readString(length: Int = (20000 as Int)) throws -> String?

    Parameters

    length

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

    Return Value

    String value with at most length characters, or nil if there is no remaining text in the stream.

  • Declaration

    Swift

    open class func toBuffer(_ buffer: CharBuffer) -> CharStream

    Parameters

    buffer

    Buffer to be written to.

    Return Value

    A new character 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.

  • Data type code of the wrapped value, equivalent to dataType.code.

    Declaration

    Swift

    override open var typeCode: Int { get }
  • Undo read of one character.

    Throws

    DataStreamException if undo is not supported by this stream.

    Declaration

    Swift

    open func undoRead(_ value: unichar) throws

    Parameters

    value

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

  • Fluent API to set the mediaType for this stream.

    Declaration

    Swift

    open func withType(_ type: String?) -> CharStream

    Parameters

    type

    Media type.

    Return Value

    This stream.

  • See also

    canUndo.

    Declaration

    Swift

    open func withUndo() throws -> CharStream

    Return Value

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

  • Write a single character to this stream.

    Declaration

    Swift

    open func writeChar(_ value: unichar) throws

    Parameters

    value

    Character value to be written.

  • Write line content to the stream, followed by a newline character.

    Declaration

    Swift

    open func writeLine(text: String) throws

    Parameters

    text

    Line content.

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

    Declaration

    Swift

    open func writeString(text: String) throws

    Parameters

    text

    Source text.