Search in sources :

Example 6 with Adler32

use of java.util.zip.Adler32 in project atlas by alibaba.

the class DexFile method calcChecksum.

/**
     * Calculates the checksum for the {@code .dex} file in the
     * given array, and modify the array to contain it.
     *
     * @param bytes {@code non-null;} the bytes of the file
     */
private static void calcChecksum(byte[] bytes) {
    Adler32 a32 = new Adler32();
    a32.update(bytes, 12, bytes.length - 12);
    int sum = (int) a32.getValue();
    bytes[8] = (byte) sum;
    bytes[9] = (byte) (sum >> 8);
    bytes[10] = (byte) (sum >> 16);
    bytes[11] = (byte) (sum >> 24);
}
Also used : Adler32(java.util.zip.Adler32)

Example 7 with Adler32

use of java.util.zip.Adler32 in project atlas by alibaba.

the class Dex method computeChecksum.

/**
     * Returns the checksum of all but the first 12 bytes of {@code dex}.
     */
public int computeChecksum() throws IOException {
    Adler32 adler32 = new Adler32();
    byte[] buffer = new byte[8192];
    // positioned ByteBuffers aren't thread safe
    ByteBuffer data = this.data.duplicate();
    data.limit(data.capacity());
    data.position(CHECKSUM_OFFSET + SizeOf.CHECKSUM);
    while (data.hasRemaining()) {
        int count = Math.min(buffer.length, data.remaining());
        data.get(buffer, 0, count);
        adler32.update(buffer, 0, count);
    }
    return (int) adler32.getValue();
}
Also used : ByteBuffer(java.nio.ByteBuffer) Adler32(java.util.zip.Adler32)

Example 8 with Adler32

use of java.util.zip.Adler32 in project dex2jar by pxb1988.

the class DexFileWriter method updateChecksum.

public static void updateChecksum(ByteBuffer buffer, int size) {
    byte[] data = buffer.array();
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError();
    }
    digest.update(data, 32, size - 32);
    byte[] sha1 = digest.digest();
    System.arraycopy(sha1, 0, data, 12, sha1.length);
    Adler32 adler32 = new Adler32();
    adler32.update(data, 12, size - 12);
    int v = (int) adler32.getValue();
    buffer.position(8);
    buffer.putInt(v);
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) Adler32(java.util.zip.Adler32)

Example 9 with Adler32

use of java.util.zip.Adler32 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 10 with Adler32

use of java.util.zip.Adler32 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

Adler32 (java.util.zip.Adler32)41 IOException (java.io.IOException)11 Checksum (java.util.zip.Checksum)9 ByteBuffer (java.nio.ByteBuffer)8 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)6 CheckedInputStream (java.util.zip.CheckedInputStream)6 BufferedInputStream (java.io.BufferedInputStream)5 File (java.io.File)5 BinaryInputArchive (org.apache.jute.BinaryInputArchive)5 InputArchive (org.apache.jute.InputArchive)4 EOFException (java.io.EOFException)3 OutputStream (java.io.OutputStream)3 CheckedOutputStream (java.util.zip.CheckedOutputStream)3 BufferedOutputStream (java.io.BufferedOutputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileOutputStream (java.io.FileOutputStream)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2