Class WritableResourceSupport

java.lang.Object
org.apache.sis.internal.storage.WritableResourceSupport
All Implemented Interfaces:
Localized

public final class WritableResourceSupport extends Object implements Localized
Helper classes for the management of WritableGridCoverageResource.CommonOption.
Since:
1.2
Version:
1.2
  • Field Details

    • resource

      private final GridCoverageResource resource
      The resource where to write.
    • replace

      private boolean replace
      true if the
      invalid reference
      WritableGridCoverageResource.CommonOption.REPLACE
      option has been specified. At most one of replace and update can be true.
    • update

      private boolean update
      true if the
      invalid reference
      WritableGridCoverageResource.CommonOption.UPDATE
      option has been specified. At most one of replace and update can be true.
  • Constructor Details

    • WritableResourceSupport

      public WritableResourceSupport(GridCoverageResource resource, WritableGridCoverageResource.Option[] options)
      Creates a new helper class for the given options.
      Parameters:
      resource - the resource where to write.
      options - configuration of the write operation.
  • Method Details

    • getLocale

      public final Locale getLocale()
      Returns the locale used by the resource for error messages, or null if unknown.
      Specified by:
      getLocale in interface Localized
      Returns:
      the locale used by the resource for error messages, or null if unknown.
    • channel

      public final ChannelDataOutput channel(ChannelDataInput input) throws IOException, DataStoreException
      Returns the writable channel positioned at the beginning of the stream. The returned channel should not be closed because it is the same channel than the one used by input. Caller should invoke ChannelDataOutput.flush() after usage.
      Parameters:
      input - the input from which to get the writable channel.
      Returns:
      the writable channel.
      Throws:
      IOException - if the stream position cannot be reset.
      DataStoreException - if the channel is read-only.
    • replace

      public final boolean replace(ChannelDataInput input) throws IOException, DataStoreException
      Returns true if the caller should add or replace the resource or false if it needs to update an existing resource. Current heuristic:
      • If the given channel is empty, then this method always returns true.
      • Otherwise this method returns true if the REPLACE option was specified, or returns false if the UPDATE option was specified, or thrown a ResourceAlreadyExistsException otherwise.
      Parameters:
      input - the channel to test for emptiness, or null if unknown.
      Returns:
      whether the caller should replace (true) or update (false) the resource.
      Throws:
      IOException - if an error occurred while checking the channel length.
      ResourceAlreadyExistsException - if the resource exists and the writer should neither updating or replacing it.
      DataStoreException - if another kind of error occurred with the resource.
    • isEmpty

      private static boolean isEmpty(ChannelDataInput input) throws IOException
      Returns true if the given channel is empty. In case of doubt, this method conservatively returns false.
      Parameters:
      input - the channel to test for emptiness, or null if unknown.
      Returns:
      true if the channel is empty, or false if not or if unknown.
      Throws:
      IOException
    • update

      public final GridCoverage update(GridCoverage coverage) throws DataStoreException
      Reads the current coverage in the resource and updates its content with cell values from the given coverage. This method can be used as a simple implementation of WritableGridCoverageResource.CommonOption.UPDATE. This method returns the updated coverage; it is caller responsibility to write it.

      This method can be used when updating the coverage requires to read it fully, then write if fully. Advanced writers should try to update only the modified parts (typically some tiles) instead.

      Parameters:
      coverage - the coverage to use for updating the currently existing coverage.
      Returns:
      the updated coverage that the caller should write.
      Throws:
      DataStoreException - if an error occurred while reading or updating the coverage.
    • getAffineTransform2D

      public final AffineTransform getAffineTransform2D(GridExtent extent, org.opengis.referencing.operation.MathTransform gridToCRS) throws DataStoreException
      Returns the "grid to CRS" transform as a two-dimensional affine transform. This is a convenience method for writers that support only this kind of transform.
      Parameters:
      extent - the extent of the grid coverage to write.
      gridToCRS - the "grid to CRS" transform of the coverage to write.
      Returns:
      the given "grid to CRS" as a two-dimensional affine transform.
      Throws:
      DataStoreException - if the affine transform cannot be extracted from the given "grid to CRS" transform.
    • canNotWrite

      public final String canNotWrite() throws DataStoreException
      Returns the message for an exception saying that we cannot write the resource.
      Returns:
      a localized "Cannot write resource" message.
      Throws:
      DataStoreException - if an error occurred while preparing the error message.
    • rotationNotSupported

      public final String rotationNotSupported(String format)
      Returns the message for an exception saying that rotations are not supported.
      Parameters:
      format - name of the format that does not support rotations.
      Returns:
      a localized "rotation not supported" message.