Class CharSource

java.lang.Object
cloud.opencode.base.io.source.CharSource

public abstract class CharSource extends Object
Abstract Character Source - A readable source of characters 抽象字符源 - 可读的字符来源

An abstraction over different sources of character data. Each instance represents an immutable source that can be read multiple times.

对不同字符数据源的抽象。每个实例代表一个可以多次读取的不可变源。

Features | 主要功能:

  • Unified character reading - 统一的字符读取
  • Read lines as stream - 以流方式读取行
  • Copy to Writer - 复制到Writer
  • Content equality checks - 内容相等性检查
  • Line counting - 行数统计

Usage Examples | 使用示例:

// From file
CharSource source = CharSource.fromPath(Paths.get("data.txt"));
String content = source.read();

// Read lines
List<String> lines = source.readLines();

// Stream lines
try (Stream<String> lineStream = source.lines()) {
    lineStream.filter(s -> !s.isEmpty()).forEach(System.out::println);
}

Security | 安全性:

  • Thread-safe: Yes (immutable source, each read opens new reader) - 线程安全: 是(不可变源,每次读取打开新Reader)
  • Null-safe: No, arguments must not be null - 空值安全: 否,参数不可为null
Since:
JDK 25, opencode-base-io V1.2.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • CharSource

      public CharSource()
  • Method Details

    • openStream

      public abstract Reader openStream()
      Opens a new Reader for reading. 打开新的Reader进行读取。

      The caller is responsible for closing the reader.

      调用者负责关闭Reader。

      Returns:
      a new Reader | 新的Reader
      Throws:
      OpenIOOperationException - if the reader cannot be opened | 如果Reader无法打开
    • openBufferedStream

      public BufferedReader openBufferedStream()
      Opens a new BufferedReader for reading. 打开新的BufferedReader进行读取。
      Returns:
      a new BufferedReader | 新的BufferedReader
      Throws:
      OpenIOOperationException - if the reader cannot be opened | 如果Reader无法打开
    • lengthIfKnown

      public Optional<Long> lengthIfKnown()
      Returns the length of this source in characters, if known. 返回此源的字符长度(如果已知)。
      Returns:
      an Optional containing the length, or empty if unknown | 包含长度的Optional,如果未知则为空
    • length

      public long length()
      Returns the length of this source in characters. 返回此源的字符长度。

      If the length is not known, this method reads the entire source.

      如果长度未知,此方法会读取整个源。

      Returns:
      the length in characters | 字符长度
      Throws:
      OpenIOOperationException - if reading fails | 如果读取失败
    • read

      public String read()
      Reads the entire contents as a string. 将整个内容读取为字符串。
      Returns:
      the content string | 内容字符串
      Throws:
      OpenIOOperationException - if reading fails | 如果读取失败
    • readFirstLine

      public Optional<String> readFirstLine()
      Reads the first line. 读取第一行。
      Returns:
      an Optional containing the first line, or empty if the source is empty | 包含第一行的Optional,如果源为空则为空
      Throws:
      OpenIOOperationException - if reading fails | 如果读取失败
    • readLines

      public List<String> readLines()
      Reads all lines as a list. 将所有行读取为列表。
      Returns:
      the list of lines | 行列表
      Throws:
      OpenIOOperationException - if reading fails | 如果读取失败
    • lines

      public Stream<String> lines()
      Returns a stream of lines. 返回行的流。

      The returned stream must be closed after use.

      返回的流在使用后必须关闭。

      Returns:
      a stream of lines | 行的流
      Throws:
      OpenIOOperationException - if the stream cannot be created | 如果流无法创建
    • countLines

      public long countLines()
      Counts the number of lines. 统计行数。
      Returns:
      the line count | 行数
      Throws:
      OpenIOOperationException - if reading fails | 如果读取失败
    • copyTo

      public long copyTo(Writer writer)
      Copies the contents to a Writer. 将内容复制到Writer。
      Parameters:
      writer - the target Writer | 目标Writer
      Returns:
      the number of characters copied | 复制的字符数
      Throws:
      OpenIOOperationException - if copying fails | 如果复制失败
    • copyTo

      public long copyTo(CharSink sink)
      Copies the contents to a CharSink. 将内容复制到CharSink。
      Parameters:
      sink - the target CharSink | 目标CharSink
      Returns:
      the number of characters copied | 复制的字符数
      Throws:
      OpenIOOperationException - if copying fails | 如果复制失败
    • copyTo

      public long copyTo(Appendable appendable)
      Copies the contents to an Appendable. 将内容复制到Appendable。
      Parameters:
      appendable - the target Appendable | 目标Appendable
      Returns:
      the number of characters copied | 复制的字符数
      Throws:
      OpenIOOperationException - if copying fails | 如果复制失败
    • contentEquals

      public boolean contentEquals(CharSource other)
      Checks if this source has the same content as another. 检查此源是否与另一个源具有相同的内容。
      Parameters:
      other - the other CharSource | 另一个CharSource
      Returns:
      true if contents are equal | 如果内容相等返回true
      Throws:
      OpenIOOperationException - if reading fails | 如果读取失败
    • isEmpty

      public boolean isEmpty()
      Checks if the source is empty. 检查源是否为空。
      Returns:
      true if empty | 如果为空返回true
      Throws:
      OpenIOOperationException - if reading fails | 如果读取失败
    • forEachLine

      public void forEachLine(Consumer<String> processor)
      Processes each line with a consumer. 使用消费者处理每一行。
      Parameters:
      processor - the line processor | 行处理器
      Throws:
      OpenIOOperationException - if reading fails | 如果读取失败
    • concat

      public static CharSource concat(CharSource... sources)
      Returns a concatenated CharSource. 返回连接后的CharSource。
      Parameters:
      sources - the sources to concatenate | 要连接的源
      Returns:
      a concatenated CharSource | 连接后的CharSource
    • concat

      public static CharSource concat(Iterable<? extends CharSource> sources)
      Returns a concatenated CharSource. 返回连接后的CharSource。
      Parameters:
      sources - the sources to concatenate | 要连接的源
      Returns:
      a concatenated CharSource | 连接后的CharSource
    • wrap

      public static CharSource wrap(CharSequence string)
      Creates a CharSource from a string. 从字符串创建CharSource。
      Parameters:
      string - the string content | 字符串内容
      Returns:
      a CharSource | CharSource
    • empty

      public static CharSource empty()
      Creates an empty CharSource. 创建空的CharSource。
      Returns:
      an empty CharSource | 空的CharSource
    • fromPath

      public static CharSource fromPath(Path path)
      Creates a CharSource from a file path with UTF-8 encoding. 从文件路径创建使用UTF-8编码的CharSource。
      Parameters:
      path - the file path | 文件路径
      Returns:
      a CharSource | CharSource
    • fromPath

      public static CharSource fromPath(Path path, Charset charset)
      Creates a CharSource from a file path. 从文件路径创建CharSource。
      Parameters:
      path - the file path | 文件路径
      charset - the charset | 字符集
      Returns:
      a CharSource | CharSource
    • fromUrl

      public static CharSource fromUrl(URL url)
      Creates a CharSource from a URL with UTF-8 encoding. 从URL创建使用UTF-8编码的CharSource。
      Parameters:
      url - the URL | URL
      Returns:
      a CharSource | CharSource
    • fromUrl

      public static CharSource fromUrl(URL url, Charset charset)
      Creates a CharSource from a URL. 从URL创建CharSource。
      Parameters:
      url - the URL | URL
      charset - the charset | 字符集
      Returns:
      a CharSource | CharSource
    • fromResource

      public static CharSource fromResource(String resourcePath)
      Creates a CharSource from a classpath resource with UTF-8 encoding. 从类路径资源创建使用UTF-8编码的CharSource。
      Parameters:
      resourcePath - the resource path | 资源路径
      Returns:
      a CharSource | CharSource
    • fromResource

      public static CharSource fromResource(String resourcePath, Charset charset)
      Creates a CharSource from a classpath resource. 从类路径资源创建CharSource。
      Parameters:
      resourcePath - the resource path | 资源路径
      charset - the charset | 字符集
      Returns:
      a CharSource | CharSource
    • fromResource

      public static CharSource fromResource(String resourcePath, Charset charset, ClassLoader classLoader)
      Creates a CharSource from a classpath resource. 从类路径资源创建CharSource。
      Parameters:
      resourcePath - the resource path | 资源路径
      charset - the charset | 字符集
      classLoader - the class loader | 类加载器
      Returns:
      a CharSource | CharSource