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

jsorel 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 3119af99df fix possible null and handle large number writing in dbf 
number fields
3119af99df is described below

commit 3119af99dfc30613772db03325c65b58cb8b25a7
Author: jsorel <johann.so...@geomatys.com>
AuthorDate: Mon Aug 5 16:58:19 2024 +0200

    fix possible null and handle large number writing in dbf number fields
---
 .../org/apache/sis/storage/shapefile/dbf/DBFField.java | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git 
a/incubator/src/org.apache.sis.storage.shapefile/main/org/apache/sis/storage/shapefile/dbf/DBFField.java
 
b/incubator/src/org.apache.sis.storage.shapefile/main/org/apache/sis/storage/shapefile/dbf/DBFField.java
index 4a6293796d..4670033b9e 100644
--- 
a/incubator/src/org.apache.sis.storage.shapefile/main/org/apache/sis/storage/shapefile/dbf/DBFField.java
+++ 
b/incubator/src/org.apache.sis.storage.shapefile/main/org/apache/sis/storage/shapefile/dbf/DBFField.java
@@ -342,7 +342,7 @@ public final class DBFField {
 
     private void writeChar(ChannelDataOutput channel, Object value) throws 
IOException {
         final String txt = (String) value;
-        final byte[] bytes = txt.getBytes(charset);
+        final byte[] bytes = txt == null ? new byte[0] : txt.getBytes(charset);
         if (bytes.length >= fieldLength) {
             channel.write(bytes, 0, fieldLength);
         } else {
@@ -373,10 +373,18 @@ public final class DBFField {
     }
 
     private void writeNumber(ChannelDataOutput channel, Object value) throws 
IOException {
-        final Number v = ((Number) value);
-        final String str = format.format(v.doubleValue());
-        final int length = str.length();
-        ensureLength(str);
+        if (value == null) value = Double.NaN;
+        double dv = ((Number) value).doubleValue();
+        String str = format.format(dv);
+        int length = str.length();
+        try {
+            ensureLength(str);
+        } catch (IOException ex) {
+            //number is too great, replace it with infinite
+            dv = dv < 0 ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
+            str = format.format(dv);
+            length = str.length();
+        }
         channel.repeat(fieldLength - length, (byte)' ');
         channel.write(str.getBytes(StandardCharsets.US_ASCII));
     }

Reply via email to