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 2cef81fd013c84b049b4ed3c69d3be0a118acaf9
Author: Martin Desruisseaux <martin.desruisse...@geomatys.com>
AuthorDate: Fri Aug 13 20:32:02 2021 +0200

    Provide a slightly optimized implementation of `ChannelDataInput.readBit()` 
method.
---
 .../org/apache/sis/internal/storage/io/ChannelData.java   | 15 +++++++++++++++
 .../apache/sis/internal/storage/io/ChannelDataInput.java  |  3 ++-
 2 files changed, 17 insertions(+), 1 deletion(-)

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 1743b85..4bf35b6 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
@@ -126,6 +126,21 @@ public abstract class ChannelData implements Markable {
     }
 
     /**
+     * Implementation of {@link ChannelDataInput#readBit()} provided here for 
performance reasons.
+     * It is caller responsibility to ensure that the {@link #buffer} contains 
at least one byte.
+     */
+    final int readBitFromBuffer() {
+        final int bp = buffer.position();
+        final long position = Math.addExact(bufferOffset, bp);
+        if ((bitPosition >>> BIT_OFFSET_SIZE) != position) {
+            bitPosition = position << BIT_OFFSET_SIZE;
+        }
+        final int bitOffset = (Byte.SIZE - 1) - (int) (bitPosition++ & ((1L << 
BIT_OFFSET_SIZE) - 1));
+        final byte value = (bitOffset != 0) ? buffer.get(bp) : buffer.get();
+        return (value & (1 << bitOffset)) == 0 ? 0 : 1;
+    }
+
+    /**
      * Returns the current bit offset, as an integer between 0 and 7 inclusive.
      *
      * <p>According {@link javax.imageio.stream.ImageInputStream} contract, 
the bit offset shall be reset to 0
diff --git 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/ChannelDataInput.java
 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/ChannelDataInput.java
index adf7f5f..9c728cf 100644
--- 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/ChannelDataInput.java
+++ 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/io/ChannelDataInput.java
@@ -212,7 +212,8 @@ public class ChannelDataInput extends ChannelData {
      * @throws IOException if an error occurred while reading (including EOF).
      */
     public final int readBit() throws IOException {
-        return (int) readBits(1);
+        ensureBufferContains(Byte.BYTES);
+        return readBitFromBuffer();
     }
 
     /**

Reply via email to