Search in sources :

Example 71 with Checksum

use of java.util.zip.Checksum in project helix by apache.

the class ZKLogFormatter method readTransactionLog.

private static void readTransactionLog(String logfilepath) throws FileNotFoundException, IOException, EOFException {
    FileInputStream fis = new FileInputStream(logfilepath);
    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 " + logfilepath);
        System.exit(2);
    }
    if (bw != null) {
        bw.write("ZooKeeper Transactional Log File with dbid " + fhdr.getDbid() + " txnlog format version " + fhdr.getVersion());
        bw.newLine();
    } else {
        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) {
            if (bw != null) {
                bw.write("EOF reached after " + count + " txns.");
                bw.newLine();
            } else {
                System.out.println("EOF reached after " + count + " txns.");
            }
            break;
        }
        if (bytes.length == 0) {
            // empty transaction
            if (bw != null) {
                bw.write("EOF reached after " + count + " txns.");
                bw.newLine();
            } else {
                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);
        if (bw != null) {
            bw.write(formatTransaction(hdr, txn));
            bw.newLine();
        } else {
            System.out.println(formatTransaction(hdr, txn));
        }
        if (logStream.readByte("EOR") != 'B') {
            LOG.error("Last transaction was partial.");
            throw new EOFException("Last transaction was partial.");
        }
        count++;
    }
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) Checksum(java.util.zip.Checksum) EOFException(java.io.EOFException) Record(org.apache.jute.Record) IOException(java.io.IOException) FileHeader(org.apache.zookeeper.server.persistence.FileHeader) FileInputStream(java.io.FileInputStream) Adler32(java.util.zip.Adler32) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 72 with Checksum

use of java.util.zip.Checksum in project Aeron by real-logic.

the class Crc32cTest method compute.

@Test
void compute() throws ReflectiveOperationException {
    final Random random = new Random(54893045794L);
    final int offset = 7;
    final ByteBuffer buffer = allocateDirectAligned(1024 + offset, CACHE_LINE_LENGTH);
    final long address = address(buffer);
    for (int i = 1; i <= 1024; i++) {
        final int length = i;
        final byte[] data = new byte[length];
        random.nextBytes(data);
        buffer.clear().position(offset);
        buffer.put(data);
        buffer.flip().position(offset);
        final Checksum crc32c = (Checksum) constructor.newInstance();
        method.invoke(crc32c, buffer);
        final int checksum = (int) crc32c.getValue();
        assertEquals(checksum, INSTANCE.compute(address, offset, length), () -> "Failed on length: " + length);
    }
}
Also used : Random(java.util.Random) Checksum(java.util.zip.Checksum) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 73 with Checksum

use of java.util.zip.Checksum in project openolat by klemens.

the class VersionsFileManager method isSameFile.

private boolean isSameFile(VFSLeaf currentFile, VersionsFileImpl versions) {
    boolean same = false;
    if (versions.getRevisions() != null && !versions.getRevisions().isEmpty()) {
        VFSRevision lastRevision = versions.getRevisions().get(versions.getRevisions().size() - 1);
        long lastSize = lastRevision.getSize();
        long currentSize = currentFile.getSize();
        if (currentSize == lastSize && currentSize > 0 && lastRevision instanceof RevisionFileImpl && currentFile instanceof LocalFileImpl) {
            RevisionFileImpl lastRev = ((RevisionFileImpl) lastRevision);
            LocalFileImpl current = (LocalFileImpl) currentFile;
            // can be the same file
            try {
                Checksum cm1 = FileUtils.checksum(((LocalFileImpl) lastRev.getFile()).getBasefile(), new Adler32());
                Checksum cm2 = FileUtils.checksum(current.getBasefile(), new Adler32());
                same = cm1.getValue() == cm2.getValue();
            } catch (IOException e) {
                log.debug("Error calculating the checksum of files");
            }
        }
    }
    return same;
}
Also used : Checksum(java.util.zip.Checksum) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) IOException(java.io.IOException) Adler32(java.util.zip.Adler32)

Example 74 with Checksum

use of java.util.zip.Checksum in project cassandra by apache.

the class CommitLogTest method testRecoveryWithBadSizeArgument.

protected void testRecoveryWithBadSizeArgument(int size, int dataSize) throws Exception {
    Checksum checksum = new CRC32();
    checksum.update(size);
    testRecoveryWithBadSizeArgument(size, dataSize, checksum.getValue());
}
Also used : CRC32(java.util.zip.CRC32) Checksum(java.util.zip.Checksum)

Example 75 with Checksum

use of java.util.zip.Checksum in project cassandra by apache.

the class CommitLogTest method testRecoveryWithBadSizeChecksum.

@Test
public void testRecoveryWithBadSizeChecksum() throws Exception {
    Checksum checksum = new CRC32();
    checksum.update(100);
    testRecoveryWithBadSizeArgument(100, 100, ~checksum.getValue());
}
Also used : CRC32(java.util.zip.CRC32) Checksum(java.util.zip.Checksum)

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