sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 03/03: Make interpretation of attribute value robust to the case where the value is unsigned.
Date Sat, 23 Feb 2019 13:36:54 GMT
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 e4aede8c9b64a7330b6a69bf426f144cb09b2b25
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Sat Feb 23 14:35:54 2019 +0100

    Make interpretation of attribute value robust to the case where the value is unsigned.
---
 .../sis/internal/netcdf/ucar/DecoderWrapper.java   | 15 +++++-------
 .../org/apache/sis/internal/netcdf/ucar/Utils.java | 28 ++++++++++++++++++++++
 .../sis/internal/netcdf/ucar/VariableWrapper.java  |  9 ++++---
 3 files changed, 38 insertions(+), 14 deletions(-)

diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
index 8a89aed..2ece9d3 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
@@ -264,10 +264,7 @@ public final class DecoderWrapper extends Decoder implements CancelTask
{
             for (final Group group : groups) {
                 final Attribute attribute = findAttribute(group, name);
                 if (attribute != null && attribute.isString()) {
-                    String value = attribute.getStringValue();
-                    if (value != null && !(value = value.trim()).isEmpty()) {
-                        return value;
-                    }
+                    return Utils.nonEmpty(attribute.getStringValue());
                 }
             }
         }
@@ -288,10 +285,10 @@ public final class DecoderWrapper extends Decoder implements CancelTask
{
                 if (attribute != null) {
                     final Number value = attribute.getNumericValue();
                     if (value != null) {
-                        return value;
+                        return Utils.fixSign(value, attribute.isUnsigned());
                     }
-                    String asString = attribute.getStringValue();
-                    if (asString != null && !(asString = asString.trim()).isEmpty())
{
+                    String asString = Utils.nonEmpty(attribute.getStringValue());
+                    if (asString != null) {
                         return parseNumber(asString);
                     }
                 }
@@ -312,8 +309,8 @@ public final class DecoderWrapper extends Decoder implements CancelTask
{
             for (final Group group : groups) {
                 final Attribute attribute = findAttribute(group, name);
                 if (attribute != null && attribute.isString()) {
-                    String value = attribute.getStringValue();
-                    if (value != null && !(value = value.trim()).isEmpty()) {
+                    String value = Utils.nonEmpty(attribute.getStringValue());
+                    if (value != null) {
                         final CalendarDate date;
                         try {
                             date = CalendarDateFormatter.isoStringToCalendarDate(Calendar.proleptic_gregorian,
value);
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/Utils.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/Utils.java
index cae6d92..629b5f3 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/Utils.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/Utils.java
@@ -54,4 +54,32 @@ final class Utils {
         }
         return text;
     }
+
+    /**
+     * If {@code isUnsigned} is {@code true} but the given value is negative, makes it positive.
+     *
+     * @param  number  the attribute value, or {@code null}.
+     * @return whether the number is unsigned.
+     */
+    static Number fixSign(Number number, final boolean isUnsigned) {
+        if (isUnsigned) {
+            if (number instanceof Byte) {
+                final byte value = (byte) number;
+                if (value < 0) {
+                    number = Byte.toUnsignedInt(value);
+                }
+            } else if (number instanceof Short) {
+                final short value = (Short) number;
+                if (value < 0) {
+                    number = Short.toUnsignedInt(value);
+                }
+            } else if (number instanceof Integer) {
+                final int value = (Integer) number;
+                if (value < 0) {
+                    number = Integer.toUnsignedLong(value);
+                }
+            }
+        }
+        return number;
+    }
 }
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/VariableWrapper.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/VariableWrapper.java
index 393c85e..1c0b007 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/VariableWrapper.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/VariableWrapper.java
@@ -327,14 +327,13 @@ final class VariableWrapper extends Variable {
             final Object[] values = new Object[attribute.getLength()];
             for (int i=0; i<values.length; i++) {
                 if (numeric) {
-                    if ((values[i] = attribute.getNumericValue(i)) != null) {
-                        hasValues = true;
-                    }
+                    values[i] = Utils.fixSign(attribute.getNumericValue(i), attribute.isUnsigned());
+                    hasValues |= (values[i] != null);
                 } else {
                     Object value = attribute.getValue(i);
                     if (value != null) {
-                        String text = value.toString().trim();
-                        if (!text.isEmpty()) {
+                        String text = Utils.nonEmpty(value.toString());
+                        if (text != null) {
                             values[i] = text;
                             hasValues = true;
                         }


Mime
View raw message