Class PathHandle

All Implemented Interfaces:
ResourceHandle, Rebindable

public class PathHandle extends AbstractResourceHandle
A filesystem-backed resource handle for physical local files.

This handle provides access to real files on the host filesystem and is categorized as a non-virtual (physical) source.

Author:
anahata
  • Field Details

    • path

      @NonNull private final @NonNull String path
      The absolute path to the local file.
  • Constructor Details

    • PathHandle

      public PathHandle()
  • Method Details

    • getName

      public String getName()
      Returns a user-friendly name for the source.

      Extracts the file name from the path.

      Returns:
      The source name.
    • getUri

      public URI getUri()
      Gets the unique URI for this resource.

      Returns the file:// URI for the path.

      Returns:
      The identifier URI.
    • getMimeType

      public String getMimeType()
      Returns the detected MIME type of the resource.

      Uses Apache Tika for robust MIME detection of disk files.

      Returns:
      The MIME type string (e.g., "text/plain", "image/png").
    • getLastModified

      public long getLastModified()
      Returns the last modified timestamp in milliseconds.

      Retrieves the last modified time from the filesystem.

      Returns:
      The timestamp, or 0 if unknown.
    • exists

      public boolean exists()
      Checks if the resource physically or virtually exists.

      Checks if the file exists on the filesystem.

      Returns:
      true if the source is available.
    • openStream

      public InputStream openStream() throws IOException
      Opens a fresh input stream to the resource content.

      Opens a standard filesystem input stream.

      Returns:
      A new InputStream instance.
      Throws:
      IOException - if the stream cannot be opened.
    • isWritable

      public boolean isWritable()
      Determines if the resource is writable in the current environment.

      Determines writability based on OS permissions.

      Returns:
      true if the handle supports the ResourceHandle.write(String) operation.
    • write

      public void write(String content) throws IOException
      Agnostically writes text content back to the resource.

      Purity Note: Read-only handles should throw UnsupportedOperationException.

      Writes content using atomic filesystem options (TRUNCATE_EXISTING).

      Parameters:
      content - The text to write.
      Throws:
      IOException - if the write fails.
    • isVirtual

      public boolean isVirtual()
      Determines if this is a virtual, memory-backed handle.

      High-Fidelity Strategy: Virtual handles (snippets) typically trigger the 'Preview-as-Editor' mode in viewers to ensure constant IDE fidelity without card-swapping.

      Implementation details: returns false as this is a physical filesystem resource.

      Returns:
      true if virtual (in-memory content).
    • length

      public long length()
      Returns the total length of the resource content in characters/bytes.

      Returns the physical file size on disk.

      Returns:
      The length, or -1 if the length is unknown or streaming.