Search in sources :

Example 6 with Checksum

use of java.util.zip.Checksum in project netty by netty.

the class FastLzFrameEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception {
    final Checksum checksum = this.checksum;
    for (; ; ) {
        if (!in.isReadable()) {
            return;
        }
        final int idx = in.readerIndex();
        final int length = Math.min(in.readableBytes(), MAX_CHUNK_LENGTH);
        final int outputIdx = out.writerIndex();
        out.setMedium(outputIdx, MAGIC_NUMBER);
        int outputOffset = outputIdx + CHECKSUM_OFFSET + (checksum != null ? 4 : 0);
        final byte blockType;
        final int chunkLength;
        if (length < MIN_LENGTH_TO_COMPRESSION) {
            blockType = BLOCK_TYPE_NON_COMPRESSED;
            out.ensureWritable(outputOffset + 2 + length);
            final byte[] output = out.array();
            final int outputPtr = out.arrayOffset() + outputOffset + 2;
            if (checksum != null) {
                final byte[] input;
                final int inputPtr;
                if (in.hasArray()) {
                    input = in.array();
                    inputPtr = in.arrayOffset() + idx;
                } else {
                    input = new byte[length];
                    in.getBytes(idx, input);
                    inputPtr = 0;
                }
                checksum.reset();
                checksum.update(input, inputPtr, length);
                out.setInt(outputIdx + CHECKSUM_OFFSET, (int) checksum.getValue());
                System.arraycopy(input, inputPtr, output, outputPtr, length);
            } else {
                in.getBytes(idx, output, outputPtr, length);
            }
            chunkLength = length;
        } else {
            // try to compress
            final byte[] input;
            final int inputPtr;
            if (in.hasArray()) {
                input = in.array();
                inputPtr = in.arrayOffset() + idx;
            } else {
                input = new byte[length];
                in.getBytes(idx, input);
                inputPtr = 0;
            }
            if (checksum != null) {
                checksum.reset();
                checksum.update(input, inputPtr, length);
                out.setInt(outputIdx + CHECKSUM_OFFSET, (int) checksum.getValue());
            }
            final int maxOutputLength = calculateOutputBufferLength(length);
            out.ensureWritable(outputOffset + 4 + maxOutputLength);
            final byte[] output = out.array();
            final int outputPtr = out.arrayOffset() + outputOffset + 4;
            final int compressedLength = compress(input, inputPtr, length, output, outputPtr, level);
            if (compressedLength < length) {
                blockType = BLOCK_TYPE_COMPRESSED;
                chunkLength = compressedLength;
                out.setShort(outputOffset, chunkLength);
                outputOffset += 2;
            } else {
                blockType = BLOCK_TYPE_NON_COMPRESSED;
                System.arraycopy(input, inputPtr, output, outputPtr - 2, length);
                chunkLength = length;
            }
        }
        out.setShort(outputOffset, length);
        out.setByte(outputIdx + OPTIONS_OFFSET, blockType | (checksum != null ? BLOCK_WITH_CHECKSUM : BLOCK_WITHOUT_CHECKSUM));
        out.writerIndex(outputOffset + 2 + chunkLength);
        in.skipBytes(length);
    }
}
Also used : Checksum(java.util.zip.Checksum)

Example 7 with Checksum

use of java.util.zip.Checksum in project DirectMemory by raffaeleguidi.

the class BaseTests method crc.

public long crc(String str) {
    final Checksum checksum = new CRC32();
    final byte[] bytes = str.getBytes();
    checksum.update(bytes, 0, bytes.length);
    return checksum.getValue();
}
Also used : CRC32(java.util.zip.CRC32) Checksum(java.util.zip.Checksum)

Example 8 with Checksum

use of java.util.zip.Checksum in project ratpack by ratpack.

the class Adler32Checksummer method apply.

/**
   * Algorythm implementation.
   *
   * @param is {@code InputStream} of file to calculate checksum
   * @return checksum string
   */
@Override
public String apply(InputStream is) throws Exception {
    byte[] buffer = new byte[BUFFER_SIZE];
    Checksum checksum = new Adler32();
    int read = is.read(buffer, 0, BUFFER_SIZE);
    while (read != -1) {
        checksum.update(buffer, 0, read);
        read = is.read(buffer, 0, BUFFER_SIZE);
    }
    return Long.toHexString(checksum.getValue());
}
Also used : Checksum(java.util.zip.Checksum) Adler32(java.util.zip.Adler32)

Example 9 with Checksum

use of java.util.zip.Checksum in project robolectric by robolectric.

the class ShadowBitmapFactory method decodeByteArray.

@Implementation
public static Bitmap decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts) {
    String desc = new String(data);
    if (!Charset.forName("US-ASCII").newEncoder().canEncode(desc)) {
        Checksum checksumEngine = new CRC32();
        checksumEngine.update(data, 0, data.length);
        desc = "byte array, checksum: " + checksumEngine.getValue();
    }
    if (offset != 0 || length != data.length) {
        desc += " bytes " + offset + ".." + length;
    }
    return create(desc, opts);
}
Also used : CRC32(java.util.zip.CRC32) Checksum(java.util.zip.Checksum) Implementation(org.robolectric.annotation.Implementation)

Example 10 with Checksum

use of java.util.zip.Checksum in project exhibitor by soabase.

the class ZooKeeperLogParser method parse.

public void parse(LogEntryReceiver receiver) throws Exception {
    if (!validHeader) {
        throw new Exception("Invalid magic number for");
    }
    while (true) {
        long crcValue;
        byte[] bytes;
        try {
            crcValue = logStream.readLong("crcvalue");
            bytes = logStream.readBuffer("txnEntry");
        } catch (EOFException e) {
            break;
        }
        if (bytes.length == 0) {
            // empty transaction
            break;
        }
        Checksum crc = new Adler32();
        crc.update(bytes, 0, bytes.length);
        if (crcValue != crc.getValue()) {
            throw new IOException("CRC doesn't match " + crcValue + " vs " + crc.getValue());
        }
        InputArchive iab = BinaryInputArchive.getArchive(new ByteArrayInputStream(bytes));
        TxnHeader hdr = new TxnHeader();
        Record record = useOldDeserializeMethod ? (Record) deserializeTxnMethod.invoke(null, iab, hdr) : (Record) deserializeTxnMethod.invoke(null, bytes, hdr);
        if (logStream.readByte("EOR") != 'B') {
            // partial transaction
            break;
        }
        receiver.receiveEntry(hdr, record);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Checksum(java.util.zip.Checksum) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) EOFException(java.io.EOFException) Record(org.apache.jute.Record) IOException(java.io.IOException) IOException(java.io.IOException) EOFException(java.io.EOFException) Adler32(java.util.zip.Adler32) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Aggregations

Checksum (java.util.zip.Checksum)35 CRC32 (java.util.zip.CRC32)14 IOException (java.io.IOException)10 Adler32 (java.util.zip.Adler32)9 File (java.io.File)6 EOFException (java.io.EOFException)3 FileInputStream (java.io.FileInputStream)3 ByteBuffer (java.nio.ByteBuffer)3 ArrayList (java.util.ArrayList)3 BufferedOutputStream (java.io.BufferedOutputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileOutputStream (java.io.FileOutputStream)2 RandomAccessFile (java.io.RandomAccessFile)2 Date (java.util.Date)2 DataChecksum (org.apache.hadoop.util.DataChecksum)2 PureJavaCrc32 (org.apache.hadoop.util.PureJavaCrc32)2 BinaryInputArchive (org.apache.jute.BinaryInputArchive)2 Record (org.apache.jute.Record)2 FileHeader (org.apache.zookeeper_voltpatches.server.persistence.FileHeader)2 ByteBuf (io.netty.buffer.ByteBuf)1