Class FileConfigSource

java.lang.Object
com.kamikazejam.kamicommon.yaml.source.FileConfigSource
All Implemented Interfaces:
ConfigSource

public class FileConfigSource extends Object implements ConfigSource
A ConfigSource implementation that reads from and writes to a file on the filesystem.

Supports both read and write (save) operations.
  • Constructor Summary

    Constructors
    Constructor
    Description
    FileConfigSource(@NotNull File file)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    @Nullable File
    Returns the underlying File if this source is file-backed; otherwise null.
    Useful for compatibility with APIs that require a File.
    boolean
    Ensures the writable source has any required backing structures (e.g., parent directories/files).
    No-op for read-only sources.
    @NotNull String
    Returns the path to a resource stream for default configuration data, if available.
    This is typically used to load default settings bundled with the application/plugin.

    If no resource stream is associated, may return null.
    @NotNull String
    id()
    A human-readable identifier for this source (e.g., absolute file path, URL, logical name).
    Uniqueness is not required and the value is intended primarily for logging/debugging.
    boolean
    Indicates whether this source supports persisting data via write(...).
    Opens a fresh InputStream for the current contents of this source.
    The caller is responsible for closing the returned stream.
    boolean
    write(byte[] data, @NotNull Charset charset)
    Writes serialized data to this source using the provided charset.
    Implementations should overwrite the existing contents atomically when possible.

    For read-only sources, this method must throw UnsupportedOperationException.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • FileConfigSource

      public FileConfigSource(@NotNull @NotNull File file)
  • Method Details

    • id

      @NotNull public @NotNull String id()
      Description copied from interface: ConfigSource
      A human-readable identifier for this source (e.g., absolute file path, URL, logical name).
      Uniqueness is not required and the value is intended primarily for logging/debugging.
      Specified by:
      id in interface ConfigSource
      Returns:
      identifier string for this source (not necessarily unique)
    • openStream

      @NotNull public @NotNull Optional<InputStream> openStream() throws IOException
      Description copied from interface: ConfigSource
      Opens a fresh InputStream for the current contents of this source.
      The caller is responsible for closing the returned stream.
      Specified by:
      openStream in interface ConfigSource
      Returns:
      an Optional containing a new InputStream if content is available; empty if absent
      Throws:
      IOException - if the stream cannot be opened due to I/O errors
    • isWritable

      public boolean isWritable()
      Description copied from interface: ConfigSource
      Indicates whether this source supports persisting data via write(...).
      Specified by:
      isWritable in interface ConfigSource
      Returns:
      true if writable; false if read-only
    • ensureExistsIfWritable

      public boolean ensureExistsIfWritable() throws IOException
      Description copied from interface: ConfigSource
      Ensures the writable source has any required backing structures (e.g., parent directories/files).
      No-op for read-only sources.
      Specified by:
      ensureExistsIfWritable in interface ConfigSource
      Returns:
      true if the source exists or was created successfully; false otherwise
      Throws:
      IOException - if creation fails due to I/O errors
    • write

      public boolean write(byte[] data, @NotNull @NotNull Charset charset) throws IOException
      Description copied from interface: ConfigSource
      Writes serialized data to this source using the provided charset.
      Implementations should overwrite the existing contents atomically when possible.

      For read-only sources, this method must throw UnsupportedOperationException.
      Specified by:
      write in interface ConfigSource
      Parameters:
      data - bytes to persist
      charset - character set used to interpret textual data (if relevant)
      Returns:
      true if the data was persisted successfully; false otherwise
      Throws:
      IOException - if an I/O error occurs while writing
    • asFileIfPresent

      @Nullable public @Nullable File asFileIfPresent()
      Description copied from interface: ConfigSource
      Returns the underlying File if this source is file-backed; otherwise null.
      Useful for compatibility with APIs that require a File.
      Specified by:
      asFileIfPresent in interface ConfigSource
      Returns:
      File when applicable; null for non-file-backed sources
    • getResourceStreamPath

      @NotNull public @NotNull String getResourceStreamPath()
      Description copied from interface: ConfigSource
      Returns the path to a resource stream for default configuration data, if available.
      This is typically used to load default settings bundled with the application/plugin.

      If no resource stream is associated, may return null.
      Specified by:
      getResourceStreamPath in interface ConfigSource