/** * Compute the CRC32 of the segment of the byte array given by the specified size and offset * * @param bytes The bytes to checksum * @param offset the offset at which to begin checksumming * @param size the number of bytes to checksum * @return The CRC32 */ public static long crc32(byte[] bytes, int offset, int size) { Crc32 crc = new Crc32(); crc.update(bytes, offset, size); return crc.getValue(); }
/** * Compute the CRC32 of the byte array * * @param bytes The array to compute the checksum for * @return The CRC32 */ public static long crc32(byte[] bytes) { return crc32(bytes, 0, bytes.length); }
/** Create a new PureJavaCrc32 object. */ public Crc32() { reset(); }
/** * Compute the CRC32 of a byte buffer from a given offset (relative to the buffer's current position) * * @param buffer The buffer with the underlying data * @param offset The offset relative to the current position * @param size The number of bytes beginning from the offset to include * @return The CRC32 */ public static long crc32(ByteBuffer buffer, int offset, int size) { Crc32 crc = new Crc32(); Checksums.update(crc, buffer, offset, size); return crc.getValue(); }
/** * Compute the checksum of the record from the attributes, key and value payloads */ public static long computeChecksum(byte[] key, byte[] value, CompressionType type, int valueOffset, int valueSize) { Crc32 crc = new Crc32(); crc.update(CURRENT_MAGIC_VALUE); byte attributes = 0; if (type.id > 0) attributes = (byte) (attributes | (COMPRESSION_CODEC_MASK & type.id)); crc.update(attributes); // update for the key if (key == null) { crc.updateInt(-1); } else { crc.updateInt(key.length); crc.update(key, 0, key.length); } // update for the value if (value == null) { crc.updateInt(-1); } else { int size = valueSize >= 0 ? valueSize : (value.length - valueOffset); crc.updateInt(size); crc.update(value, valueOffset, size); } return crc.getValue(); }
@Test public void testUpdateLong() { final long value = Integer.MAX_VALUE + 1; final ByteBuffer buffer = ByteBuffer.allocate(8); buffer.putLong(value); Checksum crc1 = new Crc32(); Checksum crc2 = new Crc32(); Checksums.updateLong(crc1, value); crc2.update(buffer.array(), buffer.arrayOffset(), 8); assertEquals("Crc values should be the same", crc1.getValue(), crc2.getValue()); }
/** * Compute the checksum of the record from the attributes, key and value payloads */ private static long computeChecksum(byte magic, byte attributes, long timestamp, ByteBuffer key, ByteBuffer value) { Crc32 crc = new Crc32(); crc.update(magic); crc.update(attributes); if (magic > RecordBatch.MAGIC_VALUE_V0) Checksums.updateLong(crc, timestamp); // update for the key if (key == null) { Checksums.updateInt(crc, -1); } else { int size = key.remaining(); Checksums.updateInt(crc, size); Checksums.update(crc, key, size); } // update for the value if (value == null) { Checksums.updateInt(crc, -1); } else { int size = value.remaining(); Checksums.updateInt(crc, size); Checksums.update(crc, value, size); } return crc.getValue(); }
/** * Compute the checksum of the record from the record contents */ public long computeChecksum() { return Crc32.crc32(buffer, MAGIC_OFFSET, buffer.limit() - MAGIC_OFFSET); }
/** Create a new PureJavaCrc32 object. */ public Crc32() { reset(); }
/** * Compute the CRC32 of the segment of the byte array given by the specified size and offset * * @param bytes The bytes to checksum * @param offset the offset at which to begin checksumming * @param size the number of bytes to checksum * @return The CRC32 */ public static long crc32(byte[] bytes, int offset, int size) { Crc32 crc = new Crc32(); crc.update(bytes, offset, size); return crc.getValue(); }
/** * Write the header for a compressed record set in-place (i.e. assuming the compressed record data has already * been written at the value offset in a wrapped record). This lets you dynamically create a compressed message * set, and then go back later and fill in its size and CRC, which saves the need for copying to another buffer. * * @param buffer The buffer containing the compressed record data positioned at the first offset of the * @param magic The magic value of the record set * @param recordSize The size of the record (including record overhead) * @param timestamp The timestamp of the wrapper record * @param compressionType The compression type used * @param timestampType The timestamp type of the wrapper record */ public static void writeCompressedRecordHeader(ByteBuffer buffer, byte magic, int recordSize, long timestamp, CompressionType compressionType, TimestampType timestampType) { int recordPosition = buffer.position(); int valueSize = recordSize - recordOverhead(magic); // write the record header with a null value (the key is always null for the wrapper) write(buffer, magic, timestamp, null, null, compressionType, timestampType); buffer.position(recordPosition); // now fill in the value size buffer.putInt(recordPosition + keyOffset(magic), valueSize); // compute and fill the crc from the beginning of the message long crc = Crc32.crc32(buffer, MAGIC_OFFSET, recordSize - MAGIC_OFFSET); ByteUtils.writeUnsignedInt(buffer, recordPosition + CRC_OFFSET, crc); }
@Test public void testValue() { final byte[] bytes = "Some String".getBytes(); assertEquals(2021503672, Crc32.crc32(bytes)); }
/** * Compute the CRC32 of the byte array * * @param bytes The array to compute the checksum for * @return The CRC32 */ public static long crc32(byte[] bytes) { return crc32(bytes, 0, bytes.length); }