Skip to content

Commit

Permalink
internal: let ByteBuffer read 4 bytes as int in single call (#594)
Browse files Browse the repository at this point in the history
  • Loading branch information
bokken authored Sep 10, 2024
1 parent 1af05fe commit da1af8b
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions src/main/java/org/xerial/snappy/SnappyFramedInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit da1af8b

Please sign in to comment.