Class OpenResource

java.lang.Object
cloud.opencode.base.classloader.OpenResource

public final class OpenResource extends Object
OpenResource - Resource Loading Facade OpenResource - 资源加载门面

Unified entry point for resource loading operations.

资源加载操作的统一入口。

Features | 主要功能:

  • Load single resource - 加载单个资源
  • Load multiple resources - 加载多个资源
  • Quick read methods - 快速读取方法
  • Resource existence check - 资源存在性检查

Supported Protocols | 支持的协议:

  • classpath: - classpath resource | classpath 资源
  • classpath*: - all matching classpath resources | 所有匹配的 classpath 资源
  • file: - file system resource | 文件系统资源
  • url: - URL resource | URL 资源
  • jar: - JAR resource | JAR 资源

Usage Examples | 使用示例:

// Load single resource
Resource config = OpenResource.get("classpath:config.yml");
String content = config.getString();

// Quick read
String content = OpenResource.readString("classpath:config.yml");

// Load multiple resources
List<Resource> xmlFiles = OpenResource.getAll("classpath*:META-INF/*.xml");

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-classloader V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • get

      public static Resource get(String location)
      Get single resource by location 按位置获取单个资源
      Parameters:
      location - resource location | 资源位置
      Returns:
      resource | 资源
    • classpath

      public static Resource classpath(String path)
      Get classpath resource 获取 classpath 资源
      Parameters:
      path - resource path | 资源路径
      Returns:
      resource | 资源
    • file

      public static Resource file(String path)
      Get file resource 获取文件资源
      Parameters:
      path - file path | 文件路径
      Returns:
      resource | 资源
    • url

      public static Resource url(String url)
      Get URL resource 获取 URL 资源
      Parameters:
      url - URL string | URL 字符串
      Returns:
      resource | 资源
    • bytes

      public static Resource bytes(byte[] content)
      Create byte array resource 创建字节数组资源
      Parameters:
      content - byte array content | 字节数组内容
      Returns:
      resource | 资源
    • bytes

      public static Resource bytes(byte[] content, String description)
      Create byte array resource with description 创建带描述的字节数组资源
      Parameters:
      content - byte array content | 字节数组内容
      description - description | 描述
      Returns:
      resource | 资源
    • getAll

      public static List<Resource> getAll(String locationPattern)
      Get all resources matching location pattern 获取所有匹配位置模式的资源
      Parameters:
      locationPattern - location pattern | 位置模式
      Returns:
      list of resources | 资源列表
    • exists

      public static boolean exists(String location)
      Check if resource exists 检查资源是否存在
      Parameters:
      location - resource location | 资源位置
      Returns:
      true if exists | 存在返回 true
    • readString

      public static String readString(String location) throws IOException
      Read resource as string (UTF-8) 读取资源为字符串(UTF-8)
      Parameters:
      location - resource location | 资源位置
      Returns:
      content as string | 字符串内容
      Throws:
      IOException - if read fails | 读取失败时抛出
    • readBytes

      public static byte[] readBytes(String location) throws IOException
      Read resource as byte array 读取资源为字节数组
      Parameters:
      location - resource location | 资源位置
      Returns:
      content as bytes | 字节内容
      Throws:
      IOException - if read fails | 读取失败时抛出
    • readLines

      public static List<String> readLines(String location) throws IOException
      Read resource as lines 读取资源为行列表
      Parameters:
      location - resource location | 资源位置
      Returns:
      list of lines | 行列表
      Throws:
      IOException - if read fails | 读取失败时抛出
    • loader

      public static ResourceLoader loader()
      Create resource loader 创建资源加载器
      Returns:
      resource loader | 资源加载器
    • loader

      public static ResourceLoader loader(ClassLoader classLoader)
      Create resource loader with classloader 使用类加载器创建资源加载器
      Parameters:
      classLoader - class loader | 类加载器
      Returns:
      resource loader | 资源加载器
    • createWatcher

      public static ResourceWatcher createWatcher() throws IOException
      Create a new ResourceWatcher for monitoring file changes 创建新的 ResourceWatcher 用于监听文件变更
      Returns:
      resource watcher | 资源监听器
      Throws:
      IOException - if cannot create watcher | 无法创建监听器时抛出