From da1af8b2600126975f17da73eb456cf0db4bb333 Mon Sep 17 00:00:00 2001 From: Brett Okken Date: Tue, 10 Sep 2024 14:22:47 -0500 Subject: [PATCH] internal: let ByteBuffer read 4 bytes as int in single call (#594) --- .../xerial/snappy/SnappyFramedInputStream.java | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/xerial/snappy/SnappyFramedInputStream.java b/src/main/java/org/xerial/snappy/SnappyFramedInputStream.java index 97a640c7..8e975735 100644 --- a/src/main/java/org/xerial/snappy/SnappyFramedInputStream.java +++ b/src/main/java/org/xerial/snappy/SnappyFramedInputStream.java @@ -16,6 +16,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; +import java.nio.ByteOrder; import java.nio.channels.Channels; import java.nio.channels.ClosedChannelException; import java.nio.channels.ReadableByteChannel; @@ -669,17 +670,9 @@ private FrameMetaData getFrameMetaData(ByteBuffer frameHeader) private FrameData getFrameData(ByteBuffer content) throws IOException { - return new FrameData(getCrc32c(content), 4); - } - - private int getCrc32c(ByteBuffer content) - { - - final int position = content.position(); - - return ((content.get(position + 3) & 0xFF) << 24) - | ((content.get(position + 2) & 0xFF) << 16) - | ((content.get(position + 1) & 0xFF) << 8) - | (content.get(position) & 0xFF); + // the first 4 bytes are the crc32c value in little endian order + content.order(ByteOrder.LITTLE_ENDIAN); + final int crc32c = content.getInt(content.position()); + return new FrameData(crc32c, 4); } }