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 993397769e Fix shapefile index offset and length in incorrect unit
993397769e is described below

commit 993397769e99a9fc00909e94378db7cf7b48d237
Author: jsorel <johann.so...@geomatys.com>
AuthorDate: Fri Jun 14 09:41:17 2024 +0200

    Fix shapefile index offset and length in incorrect unit
---
 .../main/org/apache/sis/storage/shapefile/ShapefileStore.java     | 8 +++++---
 .../main/org/apache/sis/storage/shapefile/shp/ShapeWriter.java    | 4 ++--
 .../main/org/apache/sis/storage/shapefile/shx/IndexWriter.java    | 6 ++++--
 3 files changed, 11 insertions(+), 7 deletions(-)

diff --git 
a/incubator/src/org.apache.sis.storage.shapefile/main/org/apache/sis/storage/shapefile/ShapefileStore.java
 
b/incubator/src/org.apache.sis.storage.shapefile/main/org/apache/sis/storage/shapefile/ShapefileStore.java
index 93c7731086..afebd5164e 100644
--- 
a/incubator/src/org.apache.sis.storage.shapefile/main/org/apache/sis/storage/shapefile/ShapefileStore.java
+++ 
b/incubator/src/org.apache.sis.storage.shapefile/main/org/apache/sis/storage/shapefile/ShapefileStore.java
@@ -1113,8 +1113,8 @@ public final class ShapefileStore extends DataStore 
implements WritableFeatureSe
                 }
 
                 //unchanged files
-                ShpFiles.replace(files.cpgFile, tempFiles.getCpg(true));
-                ShpFiles.replace(files.prjFile, tempFiles.getPrj(true));
+                if (files.cpgFile != null) Files.copy(files.cpgFile, 
tempFiles.getCpg(true), StandardCopyOption.REPLACE_EXISTING);
+                if (files.prjFile != null) Files.copy(files.prjFile, 
tempFiles.getPrj(true), StandardCopyOption.REPLACE_EXISTING);
 
                 //start new files
 
@@ -1169,7 +1169,9 @@ public final class ShapefileStore extends DataStore 
implements WritableFeatureSe
             final long recordEndPosition = shpWriter.getSteamPosition();
 
             //write index
-            shxWriter.writeRecord(Math.toIntExact(recordStartPosition), 
Math.toIntExact(recordEndPosition - recordStartPosition));
+            final int recordStartPositionWord = 
Math.toIntExact(recordStartPosition / 2); // divide by 2 for word size
+            final int recordEndPositionWord = 
Math.toIntExact(recordEndPosition / 2); // divide by 2 for word size
+            shxWriter.writeRecord(recordStartPositionWord, 
recordEndPositionWord - recordStartPositionWord);
 
             //copy dbf fields
             Object[] fields = new Object[dbfHeader.fields.length];
diff --git 
a/incubator/src/org.apache.sis.storage.shapefile/main/org/apache/sis/storage/shapefile/shp/ShapeWriter.java
 
b/incubator/src/org.apache.sis.storage.shapefile/main/org/apache/sis/storage/shapefile/shp/ShapeWriter.java
index d1cf62abc2..eacf5ab287 100644
--- 
a/incubator/src/org.apache.sis.storage.shapefile/main/org/apache/sis/storage/shapefile/shp/ShapeWriter.java
+++ 
b/incubator/src/org.apache.sis.storage.shapefile/main/org/apache/sis/storage/shapefile/shp/ShapeWriter.java
@@ -54,12 +54,12 @@ public final class ShapeWriter implements AutoCloseable{
     }
 
     /**
-     * Get current position in the stream.
+     * Get current byte position in the stream.
      *
      * @return current position in the stream
      */
     public long getSteamPosition() {
-        return channel.getBitOffset();
+        return channel.getStreamPosition();
     }
 
     /**
diff --git 
a/incubator/src/org.apache.sis.storage.shapefile/main/org/apache/sis/storage/shapefile/shx/IndexWriter.java
 
b/incubator/src/org.apache.sis.storage.shapefile/main/org/apache/sis/storage/shapefile/shx/IndexWriter.java
index 2a242521aa..bfba9fce07 100644
--- 
a/incubator/src/org.apache.sis.storage.shapefile/main/org/apache/sis/storage/shapefile/shx/IndexWriter.java
+++ 
b/incubator/src/org.apache.sis.storage.shapefile/main/org/apache/sis/storage/shapefile/shx/IndexWriter.java
@@ -17,6 +17,7 @@
 package org.apache.sis.storage.shapefile.shx;
 
 import java.io.IOException;
+import java.nio.ByteOrder;
 import org.apache.sis.storage.shapefile.shp.ShapeHeader;
 import org.apache.sis.io.stream.ChannelDataOutput;
 
@@ -65,11 +66,12 @@ public final class IndexWriter implements AutoCloseable{
     /**
      * Write a new record.
      *
-     * @param offset record offset
-     * @param length record length
+     * @param offset record offset, in words (2 bytes)
+     * @param length record length, in words (2 bytes)
      * @throws IOException If an I/O error occurs
      */
     public void writeRecord(int offset, int length) throws IOException {
+        channel.buffer.order(ByteOrder.BIG_ENDIAN);
         channel.writeInt(offset);
         channel.writeInt(length);
     }

Reply via email to