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

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


The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
     new a8171ac1d1 Add a safety for a `String[]` sometime represented by UCAR 
library as `Object[]`.
a8171ac1d1 is described below

commit a8171ac1d142c5ff455143743e7b21420d370098
Author: Martin Desruisseaux <[email protected]>
AuthorDate: Tue Jul 21 11:19:28 2026 +0200

    Add a safety for a `String[]` sometime represented by UCAR library as 
`Object[]`.
---
 .../org/apache/sis/storage/netcdf/base/Axis.java   | 23 ++++++++++++----------
 .../apache/sis/storage/netcdf/base/Variable.java   |  7 +++++++
 .../sis/storage/netcdf/ucar/VariableWrapper.java   | 22 ++++++++++++++++++++-
 3 files changed, 41 insertions(+), 11 deletions(-)

diff --git 
a/endorsed/src/org.apache.sis.storage.netcdf/main/org/apache/sis/storage/netcdf/base/Axis.java
 
b/endorsed/src/org.apache.sis.storage.netcdf/main/org/apache/sis/storage/netcdf/base/Axis.java
index 9e63a7741d..5eb1bd8775 100644
--- 
a/endorsed/src/org.apache.sis.storage.netcdf/main/org/apache/sis/storage/netcdf/base/Axis.java
+++ 
b/endorsed/src/org.apache.sis.storage.netcdf/main/org/apache/sis/storage/netcdf/base/Axis.java
@@ -910,18 +910,21 @@ public final class Axis extends NamedElement {
      * @throws DataStoreException if a logical error occurred.
      */
     final Vector read() throws IOException, DataStoreException {
-        final TransferFunction tr = coordinates.getTransferFunction();
-        if (tr.getType() == TransferFunctionType.LINEAR) {
-            Vector data = coordinates.read();
-            if (gridSizes != null) {
-                data = data.subList(0, getSizeProduct(0));              // 
Trim trailing NaN values.
+        RuntimeException cause = null;
+        try {
+            final TransferFunction tr = coordinates.getTransferFunction();
+            if (tr.getType() == TransferFunctionType.LINEAR) {
+                Vector data = coordinates.read();
+                if (gridSizes != null) {
+                    data = data.subList(0, getSizeProduct(0));              // 
Trim trailing NaN values.
+                }
+                return data.transform(tr.getScale(), tr.getOffset());       // 
Apply scale and offset attributes, if any.
             }
-            data = data.transform(tr.getScale(), tr.getOffset());       // 
Apply scale and offset attributes, if any.
-            return data;
-        } else {
-            throw new DataStoreException(coordinates.decoder.resources()
-                    .getString(Resources.Keys.CanNotUseAxis_1, getName()));
+        } catch (RuntimeException e) {
+            cause = e;
         }
+        throw new DataStoreException(coordinates.decoder.resources()
+                .getString(Resources.Keys.CanNotUseAxis_1, getName()), cause);
     }
 
     /**
diff --git 
a/endorsed/src/org.apache.sis.storage.netcdf/main/org/apache/sis/storage/netcdf/base/Variable.java
 
b/endorsed/src/org.apache.sis.storage.netcdf/main/org/apache/sis/storage/netcdf/base/Variable.java
index 901adff21d..af3483cda5 100644
--- 
a/endorsed/src/org.apache.sis.storage.netcdf/main/org/apache/sis/storage/netcdf/base/Variable.java
+++ 
b/endorsed/src/org.apache.sis.storage.netcdf/main/org/apache/sis/storage/netcdf/base/Variable.java
@@ -996,6 +996,7 @@ public abstract class Variable extends Node {
      * @throws IOException if an error occurred while reading the data.
      * @throws DataStoreException if a logical error occurred.
      * @throws ArithmeticException if the size of the variable exceeds {@link 
Integer#MAX_VALUE}, or other overflow occurs.
+     * @throws IllegalArgumentException if the type of the array which is read 
by this method is not recognized.
      */
     @SuppressWarnings("ReturnOfCollectionOrArrayField")
     public final Vector read() throws IOException, DataStoreException {
@@ -1018,6 +1019,7 @@ public abstract class Variable extends Node {
      * @throws IOException if an error occurred while reading the data.
      * @throws DataStoreException if a logical error occurred.
      * @throws ArithmeticException if the size of the variable exceeds {@link 
Integer#MAX_VALUE}, or other overflow occurs.
+     * @throws ArrayStoreException if an error occurred while converting the 
type of array elements.
      */
     @SuppressWarnings("ReturnOfCollectionOrArrayField")
     public final List<?> readAnyType() throws IOException, DataStoreException {
@@ -1048,6 +1050,7 @@ public abstract class Variable extends Node {
      * @throws IOException if an error occurred while reading the data.
      * @throws DataStoreException if a logical error occurred.
      * @throws ArithmeticException if the size of the region to read exceeds 
{@link Integer#MAX_VALUE}, or other overflow occurs.
+     * @throws ArrayStoreException if an error occurred while converting the 
type of array elements.
      */
     public abstract Vector read(GridExtent area, long[] subsampling) throws 
IOException, DataStoreException;
 
@@ -1063,6 +1066,7 @@ public abstract class Variable extends Node {
      * @throws IOException if an error occurred while reading the data.
      * @throws DataStoreException if a logical error occurred.
      * @throws ArithmeticException if the size of the region to read exceeds 
{@link Integer#MAX_VALUE}, or other overflow occurs.
+     * @throws ArrayStoreException if an error occurred while converting the 
type of array elements.
      */
     public abstract List<?> readAnyType(GridExtent area, long[] subsampling) 
throws IOException, DataStoreException;
 
@@ -1073,6 +1077,7 @@ public abstract class Variable extends Node {
      * @return the data as an array of a Java primitive type.
      * @throws IOException if an error occurred while reading the data.
      * @throws DataStoreException if a logical error occurred.
+     * @throws ArrayStoreException if an error occurred while converting the 
type of array elements.
      */
     protected abstract Object readFully() throws IOException, 
DataStoreException;
 
@@ -1083,6 +1088,7 @@ public abstract class Variable extends Node {
      * @param  array          the values as an array of primitive type (for 
example {@code float[]}.
      * @param  forceNumerics  whether to force the replacement of character 
strings by real numbers.
      * @throws ArithmeticException if the dimensions of this variable are too 
large.
+     * @throws IllegalArgumentException if the type of the given array is not 
recognized.
      */
     final void setValues(final Object array, final boolean forceNumerics) {
         final DataType dataType = getDataType();
@@ -1192,6 +1198,7 @@ public abstract class Variable extends Node {
      * @param  data        the data to wrap in a vector.
      * @param  isUnsigned  whether the data type is an unsigned type.
      * @return vector wrapping the given data.
+     * @throws IllegalArgumentException if the type of the given data object 
is not recognized.
      */
     protected static Vector createDecimalVector(final Object data, final 
boolean isUnsigned) {
         if (data instanceof float[]) {
diff --git 
a/endorsed/src/org.apache.sis.storage.netcdf/main/org/apache/sis/storage/netcdf/ucar/VariableWrapper.java
 
b/endorsed/src/org.apache.sis.storage.netcdf/main/org/apache/sis/storage/netcdf/ucar/VariableWrapper.java
index 9fd5decf6d..021a3a0b9a 100644
--- 
a/endorsed/src/org.apache.sis.storage.netcdf/main/org/apache/sis/storage/netcdf/ucar/VariableWrapper.java
+++ 
b/endorsed/src/org.apache.sis.storage.netcdf/main/org/apache/sis/storage/netcdf/ucar/VariableWrapper.java
@@ -523,6 +523,8 @@ final class VariableWrapper extends 
org.apache.sis.storage.netcdf.base.Variable
      * Multi-dimensional variables are flattened as a one-dimensional array 
(wrapped in a vector).
      * This method may replace fill/missing values by NaN values and caches 
the returned vector.
      *
+     * @throws ArrayStoreException if an error occurred while converting the 
<abbr>UCAR</abbr> array.
+     *
      * @see #read()
      */
     @Override
@@ -538,6 +540,7 @@ final class VariableWrapper extends 
org.apache.sis.storage.netcdf.base.Variable
      * @param  subsampling  subsampling along each dimension, or {@code null} 
if none.
      * @return the data as a vector wrapping a Java array.
      * @throws ArithmeticException if an argument exceeds the capacity of 32 
bits integer.
+     * @throws ArrayStoreException if an error occurred while converting the 
<abbr>UCAR</abbr> array.
      */
     @Override
     public Vector read(final GridExtent area, final long[] subsampling) throws 
IOException, DataStoreException {
@@ -553,6 +556,7 @@ final class VariableWrapper extends 
org.apache.sis.storage.netcdf.base.Variable
      * @param  subsampling  subsampling along each dimension, or {@code null} 
if none.
      * @return the data as a list of {@link Number} or {@link String} 
instances.
      * @throws ArithmeticException if an argument exceeds the capacity of 32 
bits integer.
+     * @throws ArrayStoreException if an error occurred while converting the 
<abbr>UCAR</abbr> array.
      */
     @Override
     public List<?> readAnyType(final GridExtent area, final long[] 
subsampling) throws IOException, DataStoreException {
@@ -573,6 +577,7 @@ final class VariableWrapper extends 
org.apache.sis.storage.netcdf.base.Variable
      * @param  subsampling  subsampling along each dimension, or {@code null} 
if none.
      * @return the data as an array of a Java primitive type.
      * @throws ArithmeticException if an argument exceeds the capacity of 32 
bits integer.
+     * @throws ArrayStoreException if an error occurred while converting the 
<abbr>UCAR</abbr> array.
      *
      * @see #read()
      * @see #read(GridExtent, long[])
@@ -604,9 +609,24 @@ final class VariableWrapper extends 
org.apache.sis.storage.netcdf.base.Variable
      * Returns the one-dimensional Java array for the given UCAR array, 
avoiding copying if possible.
      * If {@link #hasRealValues()} returns {@code true}, then this method 
replaces fill and missing
      * values by {@code NaN} values.
+     *
+     * @throws ArrayStoreException if an error occurred while converting the 
<abbr>UCAR</abbr> array.
      */
     private Object get1DJavaArray(final Array array) {
-        final Object data = 
array.get1DJavaArray(ucar.ma2.DataType.getType(array));
+        final var type = ucar.ma2.DataType.getType(array);
+        Object data = array.get1DJavaArray(type);
+        switch (type) {
+            case STRING: {
+                /*
+                 * For an unknown reason, the UCAR library sometime represents
+                 * array of this type as `String[]` and sometime as `Object[]`.
+                 */
+                if (data.getClass() == Object[].class) {
+                    final var source = (Object[]) data;
+                    data = Arrays.copyOf(source, source.length, 
String[].class);
+                }
+            }
+        }
         replaceNaN(data);
         return data;
     }

Reply via email to