This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit b28d142efb1c927fbd9eb42988570a7403e33870
Author: Martin Desruisseaux <martin.desruisse...@geomatys.com>
AuthorDate: Tue Apr 19 19:19:12 2022 +0200

    API adjustment in preparation for the addition of "World file" writer.
---
 .../org/apache/sis/internal/storage/PRJDataStore.java | 17 ++++++++++-------
 .../org/apache/sis/internal/storage/URIDataStore.java |  2 +-
 .../sis/internal/storage/WritableResourceSupport.java | 19 +++++++++++++++----
 .../sis/internal/storage/ascii/WritableStore.java     |  4 ++--
 .../sis/internal/storage/image/FormatFilter.java      |  5 +++--
 5 files changed, 31 insertions(+), 16 deletions(-)

diff --git 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/PRJDataStore.java
 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/PRJDataStore.java
index d0175e3e3d..f373297557 100644
--- 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/PRJDataStore.java
+++ 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/PRJDataStore.java
@@ -81,7 +81,7 @@ public abstract class PRJDataStore extends URIDataStore {
      *
      * @see #getComponentFiles()
      */
-    private static final String PRJ = "prj";
+    protected static final String PRJ = "prj";
 
     /**
      * Character encoding in {@code *.prj} or other auxiliary files,
@@ -366,7 +366,7 @@ public abstract class PRJDataStore extends URIDataStore {
      * The default implementation does the same computation as the 
super-class, then adds the sibling
      * file with {@code ".prj"} extension if it exists.
      *
-     * @return the URI as a path, or an empty array if the URI is null.
+     * @return the main file and auxiliary files as paths, or an empty array 
if unknown.
      * @throws DataStoreException if the URI can not be converted to a {@link 
Path}.
      */
     @Override
@@ -382,6 +382,7 @@ public abstract class PRJDataStore extends URIDataStore {
      * This is a helper method for {@link #getComponentFiles()} implementation.
      *
      * @param  auxiliaries  filename extension (without leading dot) of all 
auxiliary files.
+     *         Null elements are silently ignored.
      * @return the URI as a path, followed by all auxiliary files that exist.
      * @throws DataStoreException if the URI can not be converted to a {@link 
Path}.
      */
@@ -392,12 +393,14 @@ public abstract class PRJDataStore extends URIDataStore {
             final Path path = paths[0];
             final String base = getBaseFilename(path);
             for (final String extension : auxiliaries) {
-                final Path p = path.resolveSibling(base.concat(extension));
-                if (Files.isRegularFile(p)) {
-                    if (count >= paths.length) {
-                        paths = Arrays.copyOf(paths, count + 
auxiliaries.length);
+                if (extension != null) {
+                    final Path p = path.resolveSibling(base.concat(extension));
+                    if (Files.isRegularFile(p)) {
+                        if (count >= paths.length) {
+                            paths = Arrays.copyOf(paths, count + 
auxiliaries.length);
+                        }
+                        paths[count++] = p;
                     }
-                    paths[count++] = p;
                 }
             }
             paths = ArraysExt.resize(paths, count);
diff --git 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/URIDataStore.java
 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/URIDataStore.java
index 1242721b7c..32b1b139cc 100644
--- 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/URIDataStore.java
+++ 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/URIDataStore.java
@@ -149,7 +149,7 @@ public abstract class URIDataStore extends DataStore 
implements StoreResource, R
      * The default implementation returns the storage specified at 
construction time if it was
      * a {@link Path} or {@link File}, or converts the URI to a {@link Path} 
otherwise.
      *
-     * @return the URI as a path, or an empty array if the URI is null.
+     * @return the URI as a path, or an empty array if unknown.
      * @throws DataStoreException if the URI can not be converted to a {@link 
Path}.
      */
     @Override
diff --git 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/WritableResourceSupport.java
 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/WritableResourceSupport.java
index 2fd5c99537..326a0ea3ee 100644
--- 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/WritableResourceSupport.java
+++ 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/WritableResourceSupport.java
@@ -133,7 +133,7 @@ public final class WritableResourceSupport implements 
Localized {
      *       or thrown a {@link ResourceAlreadyExistsException} otherwise.</li>
      * </ul>
      *
-     * @param  input  the channel to test for emptiness.
+     * @param  input  the channel to test for emptiness, or {@code null} if 
unknown.
      * @return whether the caller should replace ({@code true}) or update 
({@code false}) the resource.
      * @throws IOException if an error occurred while checking the channel 
length.
      * @throws ResourceAlreadyExistsException if the resource exists and the 
writer
@@ -142,17 +142,28 @@ public final class WritableResourceSupport implements 
Localized {
      */
     public final boolean replace(final ChannelDataInput input) throws 
IOException, DataStoreException {
         if (update) {
-            return input.length() == 0;
-        } else if (replace || input.length() == 0) {
+            return isEmpty(input);
+        } else if (replace || isEmpty(input)) {
             return true;
         } else {
             Object identifier = resource.getIdentifier().orElse(null);
-            if (identifier == null) identifier = input.filename;
+            if (identifier == null && input != null) identifier = 
input.filename;
             throw new 
ResourceAlreadyExistsException(Resources.forLocale(getLocale())
                     .getString(Resources.Keys.ResourceAlreadyExists_1, 
identifier));
         }
     }
 
+    /**
+     * Returns {@code true} if the given channel is empty.
+     * In case of doubt, this method conservatively returns {@code false}.
+     *
+     * @param  input  the channel to test for emptiness, or {@code null} if 
unknown.
+     * @return {@code true} if the channel is empty, or {@code false} if not 
or if unknown.
+     */
+    private static boolean isEmpty(final ChannelDataInput input) throws 
IOException {
+        return (input != null) && input.length() == 0;
+    }
+
     /**
      * 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 {@link 
WritableGridCoverageResource.CommonOption#UPDATE}.
diff --git 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ascii/WritableStore.java
 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ascii/WritableStore.java
index 4246b2ac5b..e2085e7241 100644
--- 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ascii/WritableStore.java
+++ 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ascii/WritableStore.java
@@ -196,8 +196,8 @@ final class WritableStore extends Store implements 
WritableGridCoverageResource
         final int band = 0;                                 // May become 
configurable in a future version.
         try {
             /*
-             * If `output` is null, we are in write-only mode and there is no 
previously existing image.
-             * Otherwise an image may exist and the behavior will depends on 
which options were supplied.
+             * If `output` is non-null, we are in write-only mode and there is 
no previously existing image.
+             * Otherwise an image may exist and the behavior will depend on 
which options were supplied.
              */
             if (output == null && !h.replace(input().input)) {
                 coverage = h.update(coverage);
diff --git 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/FormatFilter.java
 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/FormatFilter.java
index 3645b98c6b..8b66e05ce0 100644
--- 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/FormatFilter.java
+++ 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/FormatFilter.java
@@ -123,6 +123,7 @@ enum FormatFilter {
 
     /**
      * Finds a provider for the given input, or returns {@code null} if none.
+     * This is used by {@link StoreProvider#probeContent(StorageConnector)}.
      *
      * @param  identifier  the property value to use as a filtering criterion, 
or {@code null} if none.
      * @param  connector   provider of the input to be given to the {@code 
canDecodeInput(…)} method.
@@ -208,7 +209,7 @@ enum FormatFilter {
      *
      * @param  identifier  the property value to use as a filtering criterion, 
or {@code null} if none.
      * @param  output      the output to be given to the new reader instance.
-     * @param  image       the image to write.
+     * @param  image       the image to write, or {@code null} if unknown.
      * @param  deferred    initially empty map to be populated with providers 
tested by this method.
      * @return the new image writer instance with its output initialized, or 
{@code null} if none was found.
      * @throws DataStoreException if an error occurred while opening a stream 
from the storage connector.
@@ -221,7 +222,7 @@ enum FormatFilter {
         while (it.hasNext()) {
             final ImageWriterSpi provider = it.next();
             if (deferred.putIfAbsent(provider, Boolean.FALSE) == null) {
-                if (provider.canEncodeImage(image)) {
+                if (image == null || provider.canEncodeImage(image)) {
                     for (final Class<?> type : provider.getOutputTypes()) {
                         if (ArraysExt.contains(VALID_OUTPUTS, type)) {
                             final Object output = connector.getStorageAs(type);

Reply via email to