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 819c427f4b06c2463db698788d923bd20accfc4b Author: jsorel <johann.so...@geomatys.com> AuthorDate: Tue Aug 16 16:19:03 2022 +0200 Channel: avoid raising an exception when a flush is called with an index before current position, call has no effect. --- .../java/org/apache/sis/internal/storage/io/ChannelData.java | 4 ++-- .../apache/sis/internal/storage/io/ChannelDataOutputTest.java | 10 ++-------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/ChannelData.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/ChannelData.java index d434a7ce24..19b2c50287 100644 --- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/ChannelData.java +++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/ChannelData.java @@ -255,11 +255,11 @@ public abstract class ChannelData implements Markable { */ public final void flushBefore(final long position) throws IOException { final long currentPosition = getStreamPosition(); - if (position < bufferOffset || position > currentPosition) { + if (position > currentPosition) { throw new IndexOutOfBoundsException(Errors.format(Errors.Keys.ValueOutOfRange_4, "position", bufferOffset, currentPosition, position)); } - final int n = (int) (position - bufferOffset); + final int n = (int) Math.max(position - bufferOffset, 0); final int p = buffer.position() - n; final int r = buffer.limit() - n; flushAndSetPosition(n); // Number of bytes to forget. diff --git a/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/io/ChannelDataOutputTest.java b/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/io/ChannelDataOutputTest.java index 6c86ccbfc2..a7c74c4442 100644 --- a/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/io/ChannelDataOutputTest.java +++ b/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/io/ChannelDataOutputTest.java @@ -37,7 +37,7 @@ import static org.junit.Assert.*; * * @author Rémi Maréchal (Geomatys) * @author Martin Desruisseaux (Geomatys) - * @version 1.2 + * @version 1.3 * @since 0.5 * @module */ @@ -223,13 +223,7 @@ public strictfp class ChannelDataOutputTest extends ChannelDataTestCase { assertTrue(message, message.contains("position")); } testedStream.flush(); - try { - testedStream.flushBefore(0); - fail("Shall not flush at a position before buffer base."); - } catch (IndexOutOfBoundsException e) { - final String message = e.getMessage(); - assertTrue(message, message.contains("position")); - } + testedStream.flushBefore(0); // Should be a no-operation. assertStreamContentEquals(); }