Class ResourceLoader

java.lang.Object
cloud.opencode.base.test.ResourceLoader

public final class ResourceLoader extends Object
Resource Loader - Test resource loading utility 资源加载器 - 测试资源加载工具

Utility class for loading test resources from classpath or filesystem.

用于从类路径或文件系统加载测试资源的工具类。

Features | 主要功能:

  • Load resources from classpath - 从类路径加载资源
  • Load resources from filesystem - 从文件系统加载资源
  • Support for text, bytes, properties - 支持文本、字节、属性文件
  • Support for different charsets - 支持不同字符集

Usage Examples | 使用示例:

// Load text file from classpath
String content = ResourceLoader.loadString("test-data.json");

// Load as lines
List<String> lines = ResourceLoader.loadLines("test-data.txt");

// Load properties
Properties props = ResourceLoader.loadProperties("test-config.properties");

// Load bytes
byte[] bytes = ResourceLoader.loadBytes("test-image.png");

Security | 安全性:

  • Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
  • Null-safe: Yes (throws IllegalArgumentException for null/missing resources) - 空值安全: 是(对空值/缺失资源抛出IllegalArgumentException)
Since:
JDK 25, opencode-base-test V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • loadString

      public static String loadString(String resourcePath)
      Loads a resource as a string using UTF-8 encoding. 使用 UTF-8 编码将资源加载为字符串。
      Parameters:
      resourcePath - the resource path | 资源路径
      Returns:
      the content as string | 内容字符串
      Throws:
      IllegalArgumentException - if resource not found | 如果资源未找到
    • loadString

      public static String loadString(String resourcePath, Charset charset)
      Loads a resource as a string with specified charset. 使用指定字符集将资源加载为字符串。
      Parameters:
      resourcePath - the resource path | 资源路径
      charset - the charset | 字符集
      Returns:
      the content as string | 内容字符串
      Throws:
      IllegalArgumentException - if resource not found | 如果资源未找到
    • loadStringOptional

      public static Optional<String> loadStringOptional(String resourcePath)
      Loads a resource as a string, returning Optional. 将资源加载为字符串,返回 Optional。
      Parameters:
      resourcePath - the resource path | 资源路径
      Returns:
      the content as Optional | 内容 Optional
    • loadLines

      public static List<String> loadLines(String resourcePath)
      Loads a resource as a list of lines. 将资源加载为行列表。
      Parameters:
      resourcePath - the resource path | 资源路径
      Returns:
      the lines | 行列表
      Throws:
      IllegalArgumentException - if resource not found | 如果资源未找到
    • loadLines

      public static List<String> loadLines(String resourcePath, Charset charset)
      Loads a resource as a list of lines with specified charset. 使用指定字符集将资源加载为行列表。
      Parameters:
      resourcePath - the resource path | 资源路径
      charset - the charset | 字符集
      Returns:
      the lines | 行列表
      Throws:
      IllegalArgumentException - if resource not found | 如果资源未找到
    • loadBytes

      public static byte[] loadBytes(String resourcePath)
      Loads a resource as a byte array. 将资源加载为字节数组。
      Parameters:
      resourcePath - the resource path | 资源路径
      Returns:
      the bytes | 字节数组
      Throws:
      IllegalArgumentException - if resource not found | 如果资源未找到
    • loadProperties

      public static Properties loadProperties(String resourcePath)
      Loads a properties file. 加载属性文件。
      Parameters:
      resourcePath - the resource path | 资源路径
      Returns:
      the properties | 属性
      Throws:
      IllegalArgumentException - if resource not found | 如果资源未找到
    • getResourceStream

      public static InputStream getResourceStream(String resourcePath)
      Gets an InputStream for the resource. 获取资源的输入流。
      Parameters:
      resourcePath - the resource path | 资源路径
      Returns:
      the input stream | 输入流
      Throws:
      IllegalArgumentException - if resource not found | 如果资源未找到
    • getResourceURL

      public static Optional<URL> getResourceURL(String resourcePath)
      Gets the URL for the resource. 获取资源的 URL。
      Parameters:
      resourcePath - the resource path | 资源路径
      Returns:
      the URL or empty | URL 或空
    • exists

      public static boolean exists(String resourcePath)
      Checks if a resource exists. 检查资源是否存在。
      Parameters:
      resourcePath - the resource path | 资源路径
      Returns:
      true if exists | 如果存在返回 true
    • loadFile

      public static String loadFile(Path path)
      Loads a file from filesystem as string. 从文件系统加载文件为字符串。
      Parameters:
      path - the file path | 文件路径
      Returns:
      the content | 内容
      Throws:
      IllegalArgumentException - if file not found | 如果文件未找到
    • loadFile

      public static String loadFile(Path path, Charset charset)
      Loads a file from filesystem as string with charset. 使用字符集从文件系统加载文件为字符串。
      Parameters:
      path - the file path | 文件路径
      charset - the charset | 字符集
      Returns:
      the content | 内容
      Throws:
      IllegalArgumentException - if file not found | 如果文件未找到
    • loadFileLines

      public static List<String> loadFileLines(Path path)
      Loads file lines from filesystem. 从文件系统加载文件行。
      Parameters:
      path - the file path | 文件路径
      Returns:
      the lines | 行列表
      Throws:
      IllegalArgumentException - if file not found | 如果文件未找到
    • loadFileBytes

      public static byte[] loadFileBytes(Path path)
      Loads file bytes from filesystem. 从文件系统加载文件字节。
      Parameters:
      path - the file path | 文件路径
      Returns:
      the bytes | 字节数组
      Throws:
      IllegalArgumentException - if file not found | 如果文件未找到