Interface Resource

All Known Implementing Classes:
AbstractResource, ByteArrayResource, ClassPathResource, FileResource, InputStreamResource, JarResource, UrlResource

public interface Resource
Resource Interface - Unified resource access abstraction 资源接口 - 统一的资源访问抽象

Abstract interface for resource access from different sources (classpath, file, URL, JAR).

用于从不同来源(classpath、文件、URL、JAR)访问资源的抽象接口。

Features | 主要功能:

  • Existence check - 存在性检查
  • Content reading - 内容读取
  • Metadata access - 元数据访问
  • Relative resource creation - 相对资源创建

Usage Examples | 使用示例:

Resource resource = new ClassPathResource("config.yml");
if (resource.exists()) {
    String content = resource.getString();
}

Security | 安全性:

  • Thread-safe: Depends on implementation - 线程安全: 取决于实现
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-classloader V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Get content length in bytes 获取内容长度(字节)
    createRelative(String relativePath)
    Create relative resource 创建相对资源
    boolean
    Check if resource exists 检查资源是否存在
    default byte[]
    Read resource as byte array 读取资源为字节数组
    Get resource description 获取资源描述
    Get file if resource is file-based 获取文件(如果是文件资源)
    Get filename 获取文件名
    Get input stream for resource 获取资源的输入流
    Get path if resource is file-based 获取路径(如果是文件资源)
    default String
    Read resource as string (UTF-8) 读取资源为字符串(UTF-8)
    default String
    getString(Charset charset)
    Read resource as string with specified charset 使用指定字符集读取资源为字符串
    Get resource URI 获取资源 URI
    Get resource URL 获取资源 URL
    boolean
    Check if resource is a file 检查资源是否为文件
    boolean
    Check if resource is readable 检查资源是否可读
    long
    Get last modified timestamp 获取最后修改时间戳
    default List<String>
    Read all lines (UTF-8) 读取所有行(UTF-8)
    default List<String>
    readLines(Charset charset)
    Read all lines with specified charset 使用指定字符集读取所有行
  • Method Details

    • exists

      boolean exists()
      Check if resource exists 检查资源是否存在
      Returns:
      true if exists | 存在返回 true
    • isReadable

      boolean isReadable()
      Check if resource is readable 检查资源是否可读
      Returns:
      true if readable | 可读返回 true
    • isFile

      boolean isFile()
      Check if resource is a file 检查资源是否为文件
      Returns:
      true if is file | 是文件返回 true
    • getInputStream

      InputStream getInputStream() throws IOException
      Get input stream for resource 获取资源的输入流
      Returns:
      input stream | 输入流
      Throws:
      IOException - if cannot open stream | 无法打开流时抛出
    • getUrl

      URL getUrl() throws IOException
      Get resource URL 获取资源 URL
      Returns:
      resource URL | 资源 URL
      Throws:
      IOException - if cannot get URL | 无法获取 URL 时抛出
    • getUri

      URI getUri() throws IOException
      Get resource URI 获取资源 URI
      Returns:
      resource URI | 资源 URI
      Throws:
      IOException - if cannot get URI | 无法获取 URI 时抛出
    • getFile

      Optional<File> getFile()
      Get file if resource is file-based 获取文件(如果是文件资源)
      Returns:
      optional file | 可选的文件
    • getPath

      Optional<Path> getPath()
      Get path if resource is file-based 获取路径(如果是文件资源)
      Returns:
      optional path | 可选的路径
    • contentLength

      long contentLength() throws IOException
      Get content length in bytes 获取内容长度(字节)
      Returns:
      content length or -1 if unknown | 内容长度,未知时返回 -1
      Throws:
      IOException - if cannot determine length | 无法确定长度时抛出
    • lastModified

      long lastModified() throws IOException
      Get last modified timestamp 获取最后修改时间戳
      Returns:
      last modified time in milliseconds or 0 if unknown | 最后修改时间(毫秒),未知时返回 0
      Throws:
      IOException - if cannot determine time | 无法确定时间时抛出
    • getFilename

      String getFilename()
      Get filename 获取文件名
      Returns:
      filename or null | 文件名或 null
    • getDescription

      String getDescription()
      Get resource description 获取资源描述
      Returns:
      description | 描述
    • getBytes

      default byte[] getBytes() throws IOException
      Read resource as byte array 读取资源为字节数组
      Returns:
      byte array | 字节数组
      Throws:
      IOException - if read fails | 读取失败时抛出
    • getString

      default String getString() throws IOException
      Read resource as string (UTF-8) 读取资源为字符串(UTF-8)
      Returns:
      content as string | 字符串内容
      Throws:
      IOException - if read fails | 读取失败时抛出
    • getString

      default String getString(Charset charset) throws IOException
      Read resource as string with specified charset 使用指定字符集读取资源为字符串
      Parameters:
      charset - character set | 字符集
      Returns:
      content as string | 字符串内容
      Throws:
      IOException - if read fails | 读取失败时抛出
    • readLines

      default List<String> readLines() throws IOException
      Read all lines (UTF-8) 读取所有行(UTF-8)
      Returns:
      list of lines | 行列表
      Throws:
      IOException - if read fails | 读取失败时抛出
    • readLines

      default List<String> readLines(Charset charset) throws IOException
      Read all lines with specified charset 使用指定字符集读取所有行
      Parameters:
      charset - character set | 字符集
      Returns:
      list of lines | 行列表
      Throws:
      IOException - if read fails | 读取失败时抛出
    • createRelative

      Resource createRelative(String relativePath) throws IOException
      Create relative resource 创建相对资源
      Parameters:
      relativePath - relative path | 相对路径
      Returns:
      relative resource | 相对资源
      Throws:
      IOException - if cannot create relative resource | 无法创建相对资源时抛出