目录

类

class BufferedInputStream<T> where T <: InputStream

public class BufferedInputStream<T> <: InputStream where T <: InputStream {
    public init(input: T)
    public init(input: T, buffer: Array<Byte>)
    public init(input: T, capacity: Int64)
}

功能:提供带缓冲区的输入流。

可将其他 InputStream 类型的输入流(如 ByteArrayStream)绑定到 BufferedInputStream 实例,从该实例读取数据时,先把数据从被绑定的流读入缓冲区暂存,再从缓冲区读取用户需要的数据。

[h2]init(T)

public init(input: T)

功能:创建 BufferedInputStream 实例,缓冲区容量取默认值 4096。

参数:

[h2]init(T, Array<Byte>)

public init(input: T, buffer: Array<Byte>)

功能:创建 BufferedInputStream 实例。

其内部使用的缓存区由入参决定,在注重性能的场景下,通过复用传入的 buffer,可以减少内存分配次数,提高性能。

参数:

异常:

[h2]init(T, Int64)

public init(input: T, capacity: Int64)

功能:创建 BufferedInputStream 实例。

参数:

异常:

[h2]func read(Array<Byte>)

public func read(buffer: Array<Byte>): Int64

功能:从绑定的输入流读出数据到 buffer 中。

参数:

返回值:

异常:

[h2]func reset(T)

public func reset(input: T): Unit

功能:绑定新的输入流,重置状态,但不重置 capacity。

参数:

[h2]extend BufferedInputStream <: Resource

func close()

public func close(): Unit

功能:关闭当前流。

  • 调用此方法后不可再调用 BufferedInputStream 的其他接口,否则会造成不可期现象。

func isClosed()

public func isClosed(): Bool

功能:判断当前流是否关闭。

返回值:

[h2]extend BufferedInputStream <: Seekable

prop length

public prop length: Int64

功能:返回当前流中的总数据量。

类型:Int64

prop position

public prop position: Int64

功能:返回当前光标位置。

类型:Int64

prop remainLength

public prop remainLength: Int64

功能:返回当前流中未读的数据量。

类型:Int64

func seek(SeekPosition)

public func seek(sp: SeekPosition): Int64

功能:移动光标到指定的位置。

  • 指定的位置不能位于流中数据头部之前。
  • 指定位置可以超过流中数据末尾。
  • 调用该函数会先清空缓存区,再移动光标的位置。

参数:

返回值:

class BufferedOutputStream<T> where T <: OutputStream

public class BufferedOutputStream<T> <: OutputStream where T <: OutputStream {
    public init(output: T)
    public init(output: T, buffer: Array<Byte>)
    public init(output: T, capacity: Int64)
}

功能:提供带缓冲区的输出流。

可将其他 OutputStream 类型的输入流(如 ByteArrayStream)绑定到 BufferedOutputStream 实例,从该实例写入数据时,先把数据写入缓冲区暂存,再从缓冲区写入数据到流中。

[h2]init(T)

public init(output: T)

功能:创建 BufferedOutputStream 实例,缓冲区容量取默认值 4096。

参数:

[h2]init(T, Array<Byte>)

public init(output: T, buffer: Array<Byte>)

功能:创建 BufferedOutputStream 实例。

其内部使用的缓存区由入参决定,在注重性能的场景下,通过复用传入的 buffer,可以减少内存分配次数,提高性能。

参数:

异常:

[h2]init(T, Int64)

public init(output: T, capacity: Int64)

功能:创建 BufferedOutputStream 实例。

参数:

异常:

[h2]func flush()

public func flush(): Unit

功能:刷新 BufferedOutputStream:将内部缓冲区的剩余数据写入绑定的输出流,并刷新 BufferedOutputStream。

[h2]func reset(T)

public func reset(output: T): Unit

功能:绑定新的输出流,重置状态,但不重置 capacity。

参数:

[h2]func write(Array<Byte>)

public func write(buffer: Array<Byte>): Unit

功能:将 buffer 中的数据写入到绑定的输出流中。

参数:

[h2]extend BufferedOutputStream <: Resource

func close()

public func close(): Unit

功能:关闭当前流。

  • 调用此方法后不可再调用 BufferedOutputStream 的其他接口,否则会造成不可期现象。

func isClosed()

public func isClosed(): Bool

功能:判断当前流是否关闭。

返回值:

[h2]extend BufferedOutputStream <: Seekable

prop length

public prop length: Int64

功能:返回当前流中的总数据量。

类型:Int64

prop position

public prop position: Int64

功能:返回当前光标位置。

类型:Int64

prop remainLength

public prop remainLength: Int64

功能:返回当前流中未读的数据量。

类型:Int64

func seek(SeekPosition)

public func seek(sp: SeekPosition): Int64

功能:移动光标到指定的位置。

  • 指定的位置不能位于流中数据头部之前。
  • 指定位置可以超过流中数据末尾。
  • 调用该函数会先将缓存区内的数据写到绑定的输出流里,再移动光标的位置。

参数:

返回值:

class ByteArrayStream

public class ByteArrayStream <: IOStream & Seekable {
    public init()
    public init(capacity: Int64)
}

功能:基于 Array<Byte> 数据类型,提供对字节流的写入、读取等操作。

[h2]prop length

public prop length: Int64

功能:返回当前流中的总数据量。

类型:Int64

[h2]prop position

public prop position: Int64

功能:获取当前光标位置。

类型:Int64

[h2]prop remainLength

public prop remainLength: Int64

功能:获取当前流中未读的数据量。

类型:Int64

[h2]init()

public init()

功能:创建 ByteArrayStream 实例,默认的初始容量是 32。

[h2]init(Int64)

public init(capacity: Int64)

功能:创建 ByteArrayStream 实例。

参数:

异常:

[h2]static func fromString(String)

public static func fromString(data: String): ByteArrayStream

功能:通过 String 类型构造一个 ByteArrayStream。

参数:

返回值:

[h2]func bytes()

public func bytes(): Array<Byte>

功能:获取当前 ByteArrayStream 中未被读取的数据的切片。

  • 缓冲区进行读取,写入或重置等修改操作会导致这个切片失效。
  • 对切片的修改会影响缓冲区的内容。

返回值:

[h2]func capacity()

public func capacity(): Int64

功能:获取当前缓冲区容量。

返回值:

[h2]func clear()

public func clear(): Unit

功能:清除当前 ByteArrayStream 中所有数据。

[h2]func clone()

public func clone(): ByteArrayStream

功能:用当前 ByteArrayStream 中的数据来构造一个新的 ByteArrayStream。

返回值:

[h2]func copyTo(OutputStream)

public func copyTo(output: OutputStream): Unit

功能:将当前 ByteArrayStream 中未被读取的所有数据拷贝到 output 流中。

参数:

[h2]func read(Array<Byte>)

public func read(buffer: Array<Byte>): Int64

功能:从输入流中读取数据放到 buffer 中。

参数:

返回值:

异常:

[h2]func readString()

public func readString(): String

功能:读取流中的剩余数据,并转换为 String 类型,做 UTF-8 编码检查。

返回值:

异常:

[h2]func readStringUnchecked()

public unsafe func readStringUnchecked(): String

功能:读取流中的剩余数据,并转换为 String 类型,不做 UTF-8 编码检查。

返回值:

[h2]func readToEnd()

public func readToEnd(): Array<Byte>

功能:获取当前 ByteArrayStream 中未被读取的数据。

返回值:

[h2]func reserve(Int64)

public func reserve(additional: Int64): Unit

功能:将缓冲区扩容指定大小。

  • 当缓冲区剩余字节数大于等于 additional 时不发生扩容。
  • 当缓冲区剩余字节数量小于 additional 时,取(additional + capacity)与(capacity的1.5倍向下取整)两个值中的最大值进行扩容。

参数:

异常:

[h2]func seek(SeekPosition)

public func seek(sp: SeekPosition): Int64

功能:将光标跳转到指定位置。

  • 指定的位置不能位于流中数据头部之前。
  • 指定位置可以超过流中数据末尾。

参数:

返回值:

异常:

[h2]func write(Array<Byte>)

public func write(buffer: Array<Byte>): Unit

功能:将 buffer 中的数据写入到输出流中。

参数:

异常:

class ChainedInputStream<T> where T <: InputStream

public class ChainedInputStream<T> <: InputStream where T <: InputStream {
    public init(input: Array<T>)
}

功能:提供顺序从 InputStream 数组中读取数据的能力。

[h2]init(Array<T>)

public init(input: Array<T>)

功能:创建 ChainedInputStream 实例。

参数:

异常:

[h2]func read(Array<Byte>)

public func read(buffer: Array<Byte>): Int64

功能:依次从绑定 InputStream 数组中读出数据到 buffer 中。

参数:

返回值:

异常:

class MultiOutputStream<T> where T <: OutputStream

public class MultiOutputStream<T> <: OutputStream where T <: OutputStream {
    public init(output: Array<T>)
}

功能:提供将数据同时写入到 OutputStream 数组中每个输出流中的能力。

[h2]init(Array<T>)

public init(output: Array<T>)

功能:创建 MultiOutputStream 实例。

参数:

异常:

[h2]func flush()

public func flush(): Unit

功能:刷新绑定的输出流数组里的每个输出流。

[h2]func write(Array<Byte>)

public func write(buffer: Array<Byte>): Unit

功能:将 buffer 同时写入到绑定的 OutputStream 数组里的每个输出流中。

参数:

class StringReader<T> where T <: InputStream

public class StringReader<T> where T <: InputStream {
    public init(input: T)
}

功能:提供从 InputStream 输入流中读出数据并转换成字符或字符串的能力。

  • StringReader 内部默认有缓冲区,缓冲区容量 4096 个字节。
  • StringReader 目前仅支持 UTF-8 编码,暂不支持 UTF-16、UTF-32。

[h2]init(T)

public init(input: T)

功能:创建 StringReader 实例。

参数:

[h2]func lines()

public func lines(): Iterator<String>

功能:获得 StringReader 的行迭代器。

相当于循环调用 func readln(),内部遇到非法字符时也会抛出异常。

  • 每行都由换行符进行分隔。
  • 换行符是 \n \r \r\n 之一。
  • 每行不包括换行符。

返回值:

异常:

[h2]func read()

public func read(): ?Rune

功能:按字符读取流中的数据。

返回值:

异常:

[h2]func readToEnd()

public func readToEnd(): String

功能:读取流中所有剩余数据。

返回值:

异常:

[h2]func readUntil((Rune)->Bool)

public func readUntil(predicate: (Rune)->Bool): Option<String>

功能:从流内读取到使 predicate 返回 true 的字符位置(包含这个字符)或者流结束位置的数据。

参数:

返回值:

异常:

[h2]func readUntil(Rune)

public func readUntil(v: Rune): Option<String>

功能:从流内读取到指定字符(包含指定字符)或者流结束位置的数据。

参数:

返回值:

异常:

[h2]func readln()

public func readln(): Option<String>

功能:按行读取流中的数据。

  • 读取的数据会去掉原换行符。

返回值:

异常:

[h2]func runes()

public func runes(): Iterator<Rune>

功能:获得 StringReader 的 Rune 迭代器。

返回值:

异常:

[h2]extend StringReader <: Resource

func close()

public func close(): Unit

功能:关闭当前流。

  • 调用此方法后不可再调用 StringReader 的其他接口,否则会造成不可期现象。

func isClosed()

public func isClosed(): Bool

功能:判断当前流是否关闭。

返回值:

[h2]extend StringReader <: Seekable

prop length

public prop length: Int64

功能:返回当前流中的总数据量。

类型:Int64

prop position

public prop position: Int64

功能:返回当前光标位置。

类型:Int64

prop remainLength

public prop remainLength: Int64

功能:返回当前流中未读的数据量。

类型:Int64

func seek(SeekPosition)

public func seek(sp: SeekPosition): Int64

功能:移动光标到指定的位置。

  • 指定的位置不能位于流中数据头部之前。
  • 指定位置可以超过流中数据末尾。

参数:

返回值:

class StringWriter<T> where T <: OutputStream

public class StringWriter<T> where T <: OutputStream {
    public init(output: T)
}

功能:提供将 String 以及一些 ToString 类型转换成指定编码格式和字节序配置的字符串并写入到输出流的能力。

  • StringWriter 内部默认有缓冲区,缓冲区容量 4096 个字节。
  • StringWriter 目前仅支持 UTF-8 编码,暂不支持 UTF-16、UTF-32。

[h2]init(T)

public init(output: T)

功能:创建 StringWriter 实例。

参数:

[h2]func flush()

public func flush(): Unit

功能:刷新内部缓冲区,将缓冲区数据写入 output 中,并刷新 output。

[h2]func write(Bool)

public func write(v: Bool): Unit

功能:写入 Bool 类型。

参数:

[h2]func write(Float16)

public func write(v: Float16): Unit

功能:写入 Float16 类型。

参数:

[h2]func write(Float32)

public func write(v: Float32): Unit

功能:写入 Float32 类型。

参数:

[h2]func write(Float64)

public func write(v: Float64): Unit

功能:写入 Float64 类型。

参数:

[h2]func write(Int16)

public func write(v: Int16): Unit

功能:写入 Int16 类型。

参数:

[h2]func write(Int32)

public func write(v: Int32): Unit

功能:写入 Int32 类型。

参数:

[h2]func write(Int64)

public func write(v: Int64): Unit

功能:写入 Int64 类型。

参数:

[h2]func write(Int8)

public func write(v: Int8): Unit

功能:写入 Int8 类型。

参数:

[h2]func write(Rune)

public func write(v: Rune): Unit

功能:写入 Rune 类型。

参数:

[h2]func write(String)

public func write(v: String): Unit

功能:写入字符串。

参数:

[h2]func write(UInt16)

public func write(v: UInt16): Unit

功能:写入 UInt16 类型。

参数:

[h2]func write(UInt32)

public func write(v: UInt32): Unit

功能:写入 UInt32 类型。

参数:

[h2]func write(UInt64)

public func write(v: UInt64): Unit

功能:写入 UInt64 类型。

参数:

[h2]func write(UInt8)

public func write(v: UInt8): Unit

功能:写入 UInt8 类型。

参数:

[h2]func write<T>(T) where T <: ToString

public func write<T>(v: T): Unit where T <: ToString

功能:写入 ToString 类型。

参数:

[h2]func writeln()

public func writeln(): Unit

功能:写入换行符。

[h2]func writeln(Bool)

public func writeln(v: Bool): Unit

功能:写入 Bool 类型 + 换行符。

参数:

[h2]func writeln(Float16)

public func writeln(v: Float16): Unit

功能:写入 Float16 类型 + 换行符。

参数:

[h2]func writeln(Float32)

public func writeln(v: Float32): Unit

功能:写入 Float32 类型 + 换行符。

参数:

[h2]func writeln(Float64)

public func writeln(v: Float64): Unit

功能:写入 Float64 类型 + 换行符。

参数:

[h2]func writeln(Int16)

public func writeln(v: Int16): Unit

功能:写入 Int16 类型 + 换行符。

参数:

[h2]func writeln(Int32)

public func writeln(v: Int32): Unit

功能:写入 Int32 类型 + 换行符。

参数:

[h2]func writeln(Int64)

public func writeln(v: Int64): Unit

功能:写入 Int64 类型 + 换行符。

参数:

[h2]func writeln(Int8)

public func writeln(v: Int8): Unit

功能:写入 Int8 类型 + 换行符。

参数:

[h2]func writeln(Rune)

public func writeln(v: Rune): Unit

功能:写入 Rune 类型 + 换行符。

参数:

[h2]func writeln(String)

public func writeln(v: String): Unit

功能:写入字符串 + 换行符。

参数:

[h2]func writeln(UInt16)

public func writeln(v: UInt16): Unit

功能:写入 UInt16 类型 + 换行符。

参数:

[h2]func writeln(UInt32)

public func writeln(v: UInt32): Unit

功能:写入 UInt32 类型 + 换行符。

参数:

[h2]func writeln(UInt64)

public func writeln(v: UInt64): Unit

功能:写入 UInt64 类型 + 换行符。

参数:

[h2]func writeln(UInt8)

public func writeln(v: UInt8): Unit

功能:写入 UInt8 类型 + 换行符。

参数:

[h2]func writeln<T>(T) where T <: ToString

public func writeln<T>(v: T): Unit where T <: ToString

功能:写入 ToString 类型 + 换行符。

参数:

[h2]extend StringWriter <: Resource

func close()

public func close(): Unit

功能:关闭当前流。

  • 调用此方法后不可再调用 StringWriter 的其他接口,否则会造成不可期现象。

func isClosed()

public func isClosed(): Bool

功能:判断当前流是否关闭。

返回值:

[h2]extend StringWriter <: Seekable

prop length

public prop length: Int64

功能:返回当前流中的总数据量。

类型:Int64

prop position

public prop position: Int64

功能:返回当前光标位置。

类型:Int64

prop remainLength

public prop remainLength: Int64

功能:返回当前流中未读的数据量。

类型:Int64

func seek(SeekPosition)

public func seek(sp: SeekPosition): Int64

功能:移动光标到指定的位置。

  • 指定的位置不能位于流中数据头部之前。
  • 指定位置可以超过流中数据末尾。

参数:

返回值: