Search in sources :

Example 66 with Checksum

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)));
}
Also used : CRC32(java.util.zip.CRC32) BValue(org.ballerinalang.model.values.BValue) Checksum(java.util.zip.Checksum) BType(org.ballerinalang.model.types.BType) BString(org.ballerinalang.model.values.BString) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BBlob(org.ballerinalang.model.values.BBlob)

Example 67 with Checksum

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();
}
Also used : Checksum(java.util.zip.Checksum)

Example 68 with Checksum

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());
}
Also used : Checksum(java.util.zip.Checksum)

Example 69 with Checksum

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());
}
Also used : Checksum(java.util.zip.Checksum) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 70 with Checksum

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());
}
Also used : Checksum(java.util.zip.Checksum) Test(org.junit.Test)

Aggregations

Checksum (java.util.zip.Checksum)84 CRC32 (java.util.zip.CRC32)29 IOException (java.io.IOException)16 ByteBuffer (java.nio.ByteBuffer)15 Adler32 (java.util.zip.Adler32)12 File (java.io.File)8 InputStream (java.io.InputStream)7 FileInputStream (java.io.FileInputStream)6 Path (java.nio.file.Path)6 EOFException (java.io.EOFException)5 Test (org.junit.Test)5 Test (org.junit.jupiter.api.Test)5 StoreChannel (org.neo4j.io.fs.StoreChannel)5 CheckedInputStream (java.util.zip.CheckedInputStream)4 BufferedOutputStream (java.io.BufferedOutputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ArrayList (java.util.ArrayList)3 PureJavaCrc32 (org.apache.hadoop.util.PureJavaCrc32)3 BinaryInputArchive (org.apache.jute.BinaryInputArchive)3