use of java.util.zip.Checksum in project ballerina by ballerina-lang.
the class GetCRC32 method execute.
@Override
public void execute(Context context) {
BValue entityBody = context.getRefArgument(0);
Checksum checksum = new CRC32();
byte[] bytes;
long checksumVal;
BType argType = entityBody.getType();
if (argType == BTypes.typeJSON || argType == BTypes.typeXML || argType == BTypes.typeString) {
// TODO: Look at the possibility of making the encoding configurable
bytes = entityBody.stringValue().getBytes(StandardCharsets.UTF_8);
} else if (argType == BTypes.typeBlob) {
bytes = ((BBlob) entityBody).blobValue();
} else {
throw new BallerinaException("failed to generate hash: unsupported data type: " + entityBody.getType().getName());
}
checksum.update(bytes, 0, bytes.length);
checksumVal = checksum.getValue();
context.setReturnValues(new BString(Long.toHexString(checksumVal)));
}
use of java.util.zip.Checksum in project apache-kafka-on-k8s by banzaicloud.
the class Crc32C method compute.
/**
* Compute the CRC32C (Castagnoli) 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 CRC32C
*/
public static long compute(ByteBuffer buffer, int offset, int size) {
Checksum crc = create();
Checksums.update(crc, buffer, offset, size);
return crc.getValue();
}
use of java.util.zip.Checksum in project apache-kafka-on-k8s by banzaicloud.
the class ChecksumsTest method doTestUpdateByteBuffer.
private void doTestUpdateByteBuffer(byte[] bytes, ByteBuffer buffer) {
buffer.put(bytes);
buffer.flip();
Checksum bufferCrc = new Crc32();
Checksums.update(bufferCrc, buffer, buffer.remaining());
assertEquals(Crc32.crc32(bytes), bufferCrc.getValue());
assertEquals(0, buffer.position());
}
use of java.util.zip.Checksum in project apache-kafka-on-k8s by banzaicloud.
the class ChecksumsTest method testUpdateInt.
@Test
public void testUpdateInt() {
final int value = 1000;
final ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.putInt(value);
Checksum crc1 = Crc32C.create();
Checksum crc2 = Crc32C.create();
Checksums.updateInt(crc1, value);
crc2.update(buffer.array(), buffer.arrayOffset(), 4);
assertEquals("Crc values should be the same", crc1.getValue(), crc2.getValue());
}
use of java.util.zip.Checksum in project apache-kafka-on-k8s by banzaicloud.
the class Crc32CTest method testUpdate.
@Test
public void testUpdate() {
final byte[] bytes = "Any String you want".getBytes();
final int len = bytes.length;
Checksum crc1 = Crc32C.create();
Checksum crc2 = Crc32C.create();
Checksum crc3 = Crc32C.create();
crc1.update(bytes, 0, len);
for (int i = 0; i < len; i++) crc2.update(bytes[i]);
crc3.update(bytes, 0, len / 2);
crc3.update(bytes, len / 2, len - len / 2);
assertEquals("Crc values should be the same", crc1.getValue(), crc2.getValue());
assertEquals("Crc values should be the same", crc1.getValue(), crc3.getValue());
}
Aggregations