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++;
}
}
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);
}
}
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;
}
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());
}
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());
}
Aggregations