Class AbstractResource

java.lang.Object
cloud.opencode.base.classloader.resource.AbstractResource
All Implemented Interfaces:
Resource
Direct Known Subclasses:
ByteArrayResource, ClassPathResource, FileResource, InputStreamResource, JarResource, NestedJarResource, UrlResource

public abstract class AbstractResource extends Object implements Resource
Abstract Resource - Base implementation for Resource interface 抽象资源 - Resource 接口的基础实现

Provides common default implementations for Resource interface methods.

为 Resource 接口方法提供通用的默认实现。

Features | 主要功能:

  • Base implementation for resource abstraction - 资源抽象的基础实现
  • Common resource operations (exists, size, last modified) - 通用资源操作(存在性、大小、最后修改时间)
  • Template method pattern for concrete implementations - 模板方法模式用于具体实现

Usage Examples | 使用示例:

// Extend AbstractResource for custom resource types
// 扩展AbstractResource以实现自定义资源类型
public class MyResource extends AbstractResource {
    @Override
    public InputStream getInputStream() {
        return new ByteArrayInputStream(data);
    }
}

Security | 安全性:

  • Thread-safe: Implementation-dependent - 线程安全: 取决于实现
  • Null-safe: Yes (validates inputs) - 空值安全: 是(验证输入)
Since:
JDK 25, opencode-base-classloader V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • AbstractResource

      public AbstractResource()
  • Method Details

    • exists

      public boolean exists()
      Description copied from interface: Resource
      Check if resource exists 检查资源是否存在
      Specified by:
      exists in interface Resource
      Returns:
      true if exists | 存在返回 true
    • isReadable

      public boolean isReadable()
      Description copied from interface: Resource
      Check if resource is readable 检查资源是否可读
      Specified by:
      isReadable in interface Resource
      Returns:
      true if readable | 可读返回 true
    • isFile

      public boolean isFile()
      Description copied from interface: Resource
      Check if resource is a file 检查资源是否为文件
      Specified by:
      isFile in interface Resource
      Returns:
      true if is file | 是文件返回 true
    • getUri

      public URI getUri() throws IOException
      Description copied from interface: Resource
      Get resource URI 获取资源 URI
      Specified by:
      getUri in interface Resource
      Returns:
      resource URI | 资源 URI
      Throws:
      IOException - if cannot get URI | 无法获取 URI 时抛出
    • getFile

      public Optional<File> getFile()
      Description copied from interface: Resource
      Get file if resource is file-based 获取文件(如果是文件资源)
      Specified by:
      getFile in interface Resource
      Returns:
      optional file | 可选的文件
    • getPath

      public Optional<Path> getPath()
      Description copied from interface: Resource
      Get path if resource is file-based 获取路径(如果是文件资源)
      Specified by:
      getPath in interface Resource
      Returns:
      optional path | 可选的路径
    • contentLength

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

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

      public String getFilename()
      Description copied from interface: Resource
      Get filename 获取文件名
      Specified by:
      getFilename in interface Resource
      Returns:
      filename or null | 文件名或 null
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object