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 dee3118b74d45b7bc1e079c820e14d440324d166
Author: Martin Desruisseaux <martin.desruisse...@geomatys.com>
AuthorDate: Wed Feb 21 11:50:38 2024 +0100

    Fix an exception when the amount of bytes to read from S3 or HTTP file 
exceed the buffer capacity.
---
 .../org/apache/sis/io/stream/FileCacheByteChannel.java | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git 
a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/io/stream/FileCacheByteChannel.java
 
b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/io/stream/FileCacheByteChannel.java
index 1b316fc5d7..1db976624b 100644
--- 
a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/io/stream/FileCacheByteChannel.java
+++ 
b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/io/stream/FileCacheByteChannel.java
@@ -326,6 +326,8 @@ public abstract class FileCacheByteChannel extends 
ByteRangeChannel {
     /**
      * A temporary buffer for transferring data when we cannot write directly 
in the destination buffer.
      * It shall be a buffer backed by a Java array, not a direct buffer. 
Created when first needed.
+     *
+     * @see #transfer()
      */
     private ByteBuffer transfer;
 
@@ -606,8 +608,8 @@ public abstract class FileCacheByteChannel extends 
ByteRangeChannel {
     }
 
     /**
-     * Attempts to read up to <i>r</i> bytes from the channel,
-     * where <i>r</i> is the number of bytes remaining in the buffer.
+     * Attempts to read up to <var>r</var> bytes from the channel,
+     * where <var>r</var> is the number of bytes remaining in the buffer.
      * Bytes are written in the given buffer starting at the current position.
      * Upon return, the buffer position is advanced by the number of bytes 
read.
      *
@@ -666,7 +668,11 @@ public abstract class FileCacheByteChannel extends 
ByteRangeChannel {
             buffer = dst;
         } else {
             buffer = transfer();
-            buffer.clear().limit(dst.remaining());
+            buffer.clear();
+            final int r = dst.remaining();
+            if (r < BUFFER_SIZE) {
+                buffer.limit(r);
+            }
         }
         /*
          * Transfer bytes from the input stream to the buffer. The bytes are 
also copied to the temporary file.
@@ -688,7 +694,7 @@ public abstract class FileCacheByteChannel extends 
ByteRangeChannel {
     }
 
     /**
-     * Attempts to read up to <i>r</i> bytes from the cache.
+     * Attempts to read up to <var>r</var> bytes from the cache.
      * This method does not use the connection (it may be null).
      * The {@link #position} field is updated by the number of bytes read.
      *
@@ -765,8 +771,8 @@ public abstract class FileCacheByteChannel extends 
ByteRangeChannel {
     }
 
     /**
-     * Attempts to write up to <i>r</i> bytes to the channel,
-     * where <i>r</i> is the number of bytes remaining in the buffer.
+     * Attempts to write up to <var>r</var> bytes to the channel,
+     * where <var>r</var> is the number of bytes remaining in the buffer.
      * Bytes are read from the given buffer starting at the current position.
      * Upon return, the buffer position is advanced by the number of bytes 
written.
      *

Reply via email to