Search in sources :

Example 21 with Adler32

use of java.util.zip.Adler32 in project ddf by codice.

the class Adler32ChecksumProvider method calculateChecksum.

@Override
public String calculateChecksum(InputStream inputStream) throws IOException, NoSuchAlgorithmException {
    if (inputStream == null) {
        throw new IllegalArgumentException("InputStream cannot be null");
    }
    long checksumValue = 0L;
    try (CheckedInputStream cis = new CheckedInputStream(inputStream, new Adler32())) {
        byte[] buf = new byte[4096];
        while (cis.read(buf, 0, buf.length - 1) != -1) {
        }
        checksumValue = cis.getChecksum().getValue();
    }
    return Long.toHexString(checksumValue);
}
Also used : CheckedInputStream(java.util.zip.CheckedInputStream) Adler32(java.util.zip.Adler32)

Example 22 with Adler32

use of java.util.zip.Adler32 in project processdash by dtuma.

the class ListingHashcodeCalculator method getListingHashcode.

/**
     * Computes a hashcode for the items in a resource collection.
     * 
     * The hashcode is sensitive to:
     * <ul>
     * <li>The names of the resources that appear both in the collection and in
     * the <code>resourceNames</code> parameter</li>
     * <li>The checksums of those files in the resource collection</li>
     * </ul>
     * 
     * Thus, if the list of resources in the collection changes, or if the
     * contents of a resource change, this hashcode is likely to change as well.
     * 
     * @param collection
     *                the resource collection to examine
     * @param resourceNames
     *                the names of resources to include in the hash
     * @return a hashcode for the names and checksums of the listed files
     */
public static long getListingHashcode(ResourceCollectionInfo collection, List<String> resourceNames) {
    String[] sortedNames = new String[resourceNames.size()];
    for (int i = 0; i < sortedNames.length; i++) {
        sortedNames[i] = resourceNames.get(i).toLowerCase();
    }
    Arrays.sort(sortedNames);
    Adler32 cksum = new Adler32();
    try {
        DataOutputStream out = new DataOutputStream(new CheckedOutputStream(NULL_OUT, cksum));
        for (String resourceName : sortedNames) {
            long lastMod = collection.getLastModified(resourceName);
            if (lastMod < 1)
                continue;
            Long checksum = collection.getChecksum(resourceName);
            if (checksum == null)
                continue;
            out.writeUTF(resourceName);
            out.writeLong(checksum);
        }
    } catch (IOException e) {
    // can't happen
    }
    return cksum.getValue();
}
Also used : DataOutputStream(java.io.DataOutputStream) CheckedOutputStream(java.util.zip.CheckedOutputStream) IOException(java.io.IOException) Adler32(java.util.zip.Adler32)

Example 23 with Adler32

use of java.util.zip.Adler32 in project zookeeper by apache.

the class CRCTest method getCheckSum.

/** return if checksum matches for a snapshot **/
private boolean getCheckSum(FileSnap snap, File snapFile) throws IOException {
    DataTree dt = new DataTree();
    Map<Long, Integer> sessions = new ConcurrentHashMap<Long, Integer>();
    InputStream snapIS = new BufferedInputStream(new FileInputStream(snapFile));
    CheckedInputStream crcIn = new CheckedInputStream(snapIS, new Adler32());
    InputArchive ia = BinaryInputArchive.getArchive(crcIn);
    try {
        snap.deserialize(dt, sessions, ia);
    } catch (IOException ie) {
        // we failed on the most recent snapshot
        // must be incomplete
        // try reading the next one
        // after corrupting
        snapIS.close();
        crcIn.close();
        throw ie;
    }
    long checksum = crcIn.getChecksum().getValue();
    long val = ia.readLong("val");
    snapIS.close();
    crcIn.close();
    return (val != checksum);
}
Also used : CheckedInputStream(java.util.zip.CheckedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) CheckedInputStream(java.util.zip.CheckedInputStream) Adler32(java.util.zip.Adler32) BufferedInputStream(java.io.BufferedInputStream) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 24 with Adler32

use of java.util.zip.Adler32 in project zookeeper by apache.

the class LogFormatter method main.

/**
     * @param args
     */
public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("USAGE: LogFormatter log_file");
        System.exit(2);
    }
    FileInputStream fis = new FileInputStream(args[0]);
    BinaryInputArchive logStream = BinaryInputArchive.getArchive(fis);
    FileHeader fhdr = new FileHeader();
    fhdr.deserialize(logStream, "fileheader");
    if (fhdr.getMagic() != FileTxnLog.TXNLOG_MAGIC) {
        System.err.println("Invalid magic number for " + args[0]);
        System.exit(2);
    }
    System.out.println("ZooKeeper Transactional Log File with dbid " + fhdr.getDbid() + " txnlog format version " + fhdr.getVersion());
    int count = 0;
    while (true) {
        long crcValue;
        byte[] bytes;
        try {
            crcValue = logStream.readLong("crcvalue");
            bytes = logStream.readBuffer("txnEntry");
        } catch (EOFException e) {
            System.out.println("EOF reached after " + count + " txns.");
            return;
        }
        if (bytes.length == 0) {
            // Since we preallocate, we define EOF to be an
            // empty transaction
            System.out.println("EOF reached after " + count + " txns.");
            return;
        }
        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());
        }
        TxnHeader hdr = new TxnHeader();
        Record txn = SerializeUtils.deserializeTxn(bytes, hdr);
        System.out.println(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG).format(new Date(hdr.getTime())) + " session 0x" + Long.toHexString(hdr.getClientId()) + " cxid 0x" + Long.toHexString(hdr.getCxid()) + " zxid 0x" + Long.toHexString(hdr.getZxid()) + " " + TraceFormatter.op2String(hdr.getType()) + " " + txn);
        if (logStream.readByte("EOR") != 'B') {
            LOG.error("Last transaction was partial.");
            throw new EOFException("Last transaction was partial.");
        }
        count++;
    }
}
Also used : IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Adler32(java.util.zip.Adler32) Date(java.util.Date) BinaryInputArchive(org.apache.jute.BinaryInputArchive) Checksum(java.util.zip.Checksum) EOFException(java.io.EOFException) Record(org.apache.jute.Record) FileHeader(org.apache.zookeeper.server.persistence.FileHeader) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 25 with Adler32

use of java.util.zip.Adler32 in project zookeeper by apache.

the class FileSnap method deserialize.

/**
     * deserialize a data tree from the most recent snapshot
     * @return the zxid of the snapshot
     */
public long deserialize(DataTree dt, Map<Long, Integer> sessions) throws IOException {
    // we run through 100 snapshots (not all of them)
    // if we cannot get it running within 100 snapshots
    // we should  give up
    List<File> snapList = findNValidSnapshots(100);
    if (snapList.size() == 0) {
        return -1L;
    }
    File snap = null;
    boolean foundValid = false;
    for (int i = 0; i < snapList.size(); i++) {
        snap = snapList.get(i);
        InputStream snapIS = null;
        CheckedInputStream crcIn = null;
        try {
            LOG.info("Reading snapshot " + snap);
            snapIS = new BufferedInputStream(new FileInputStream(snap));
            crcIn = new CheckedInputStream(snapIS, new Adler32());
            InputArchive ia = BinaryInputArchive.getArchive(crcIn);
            deserialize(dt, sessions, ia);
            long checkSum = crcIn.getChecksum().getValue();
            long val = ia.readLong("val");
            if (val != checkSum) {
                throw new IOException("CRC corruption in snapshot :  " + snap);
            }
            foundValid = true;
            break;
        } catch (IOException e) {
            LOG.warn("problem reading snap file " + snap, e);
        } finally {
            if (snapIS != null)
                snapIS.close();
            if (crcIn != null)
                crcIn.close();
        }
    }
    if (!foundValid) {
        throw new IOException("Not able to find valid snapshots in " + snapDir);
    }
    dt.lastProcessedZxid = Util.getZxidFromName(snap.getName(), "snapshot");
    return dt.lastProcessedZxid;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) CheckedInputStream(java.util.zip.CheckedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) IOException(java.io.IOException) File(java.io.File) CheckedInputStream(java.util.zip.CheckedInputStream) FileInputStream(java.io.FileInputStream) Adler32(java.util.zip.Adler32)

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