本文整理汇总了Java中java.util.zip.Checksum.getValue方法的典型用法代码示例。如果您正苦于以下问题:Java Checksum.getValue方法的具体用法?Java Checksum.getValue怎么用?Java Checksum.getValue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.zip.Checksum
的用法示例。
在下文中一共展示了Checksum.getValue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: calCrc32
import java.util.zip.Checksum; //导入方法依赖的package包/类
public static byte[] calCrc32(byte[] data) {
Checksum checksum = new CRC32();
// update the current checksum with the specified array of bytes
checksum.update(data, 0, data.length);
// get the current checksum value
long checksumValue = checksum.getValue();
String hex = Long.toHexString(checksumValue).toUpperCase();
while (hex.length() < 8) {
hex = "0" + hex;
}
byte[] crc32 = Hex.decode(hex);
inverseBytes(crc32);
reverseArray(crc32);
return crc32;
}
示例2: computeCrc32
import java.util.zip.Checksum; //导入方法依赖的package包/类
/**
* Compute the CRC-32 of the contents of a stream.
* \r\n and \r are both normalized to \n for purposes of the calculation.
*/
static String computeCrc32(InputStream is) throws IOException {
Checksum crc = new CRC32();
int last = -1;
int curr;
while ((curr = is.read()) != -1) {
if (curr != '\n' && last == '\r') {
crc.update('\n');
}
if (curr != '\r') {
crc.update(curr);
}
last = curr;
}
if (last == '\r') {
crc.update('\n');
}
int val = (int)crc.getValue();
String hex = Integer.toHexString(val);
while (hex.length() < 8) {
hex = "0" + hex; // NOI18N
}
return hex;
}
示例3: crc32
import java.util.zip.Checksum; //导入方法依赖的package包/类
/**
* Generates a CRC 32 Value of String passed
*
* @param data
* @return long crc32 value of input. -1 if string is null
* @throws RuntimeException if UTF-8 is not a supported character set
*/
public static long crc32(String data) {
if (data == null) {
return -1;
}
try {
// get bytes from string
byte bytes[] = data.getBytes("UTF-8");
Checksum checksum = new CRC32();
// update the current checksum with the specified array of bytes
checksum.update(bytes, 0, bytes.length);
// get the current checksum value
return checksum.getValue();
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
示例4: checkChecksum
import java.util.zip.Checksum; //导入方法依赖的package包/类
private static void checkChecksum(Checksum checksum, long expected) {
if (checksum.getValue() != expected) {
throw new RuntimeException("Calculated checksum result was invalid."
+ " Expected " + Long.toHexString(expected)
+ ", but got " + Long.toHexString(checksum.getValue()) + ".");
}
}
示例5: checkChecksumOffset
import java.util.zip.Checksum; //导入方法依赖的package包/类
private static void checkChecksumOffset(Checksum checksum, long expected, int offset) {
if (checksum.getValue() != expected) {
throw new RuntimeException("Calculated CRC32C result was invalid. Array offset "
+ offset + ". Expected: " + Long.toHexString(expected) + ", Got: "
+ Long.toHexString(checksum.getValue()));
}
}
示例6: crc32
import java.util.zip.Checksum; //导入方法依赖的package包/类
public Encoder crc32() {
Checksum checksum = new CRC32();
checksum.update(data, 0, data.length);
long checksumValue = checksum.getValue();
data = Long.toHexString(checksumValue).getBytes();
return unhex();
}
示例7: assertChecksum
import java.util.zip.Checksum; //导入方法依赖的package包/类
private static void assertChecksum(Supplier<Checksum> supplier, String input) {
byte[] bytes = HashTestUtils.ascii(input);
Checksum checksum = supplier.get();
checksum.update(bytes, 0, bytes.length);
long value = checksum.getValue();
String toString = "name";
HashFunction func = new ChecksumHashFunction(supplier, 32, toString);
assertEquals(toString, func.toString());
assertEquals(value, func.hashBytes(bytes).padToLong());
}
示例8: check
import java.util.zip.Checksum; //导入方法依赖的package包/类
private static void check(Checksum crc1, Checksum crc2) throws Exception {
if (crc1.getValue() != crc2.getValue()) {
String s = "value 1 = " + crc1.getValue() + ", value 2 = " + crc2.getValue();
System.err.println(s);
throw new Exception(s);
}
}
示例9: validateChecksum
import java.util.zip.Checksum; //导入方法依赖的package包/类
/**
* Validate a transaction's checksum
*/
private void validateChecksum(DataInputStream in,
Checksum checksum,
long txid)
throws IOException {
if (checksum != null) {
int calculatedChecksum = (int)checksum.getValue();
int readChecksum = in.readInt(); // read in checksum
if (readChecksum != calculatedChecksum) {
throw new ChecksumException(
"Transaction is corrupt. Calculated checksum is " +
calculatedChecksum + " but read checksum " + readChecksum, txid);
}
}
}
示例10: computePartialChecksum
import java.util.zip.Checksum; //导入方法依赖的package包/类
public static long computePartialChecksum(long timestamp, int serializedKeySize, int serializedValueSize) {
Checksum checksum = Crc32C.create();
Checksums.updateLong(checksum, timestamp);
Checksums.updateInt(checksum, serializedKeySize);
Checksums.updateInt(checksum, serializedValueSize);
return checksum.getValue();
}
示例11: main
import java.util.zip.Checksum; //导入方法依赖的package包/类
/**
* @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++;
}
}
示例12: TxnLogSource
import java.util.zip.Checksum; //导入方法依赖的package包/类
public TxnLogSource(String file) throws IOException {
this.file = file;
skiplist = new LogSkipList();
RandomAccessFileReader reader = new RandomAccessFileReader(new File(file));
try {
BinaryInputArchive logStream = new BinaryInputArchive(reader);
FileHeader fhdr = new FileHeader();
fhdr.deserialize(logStream, "fileheader");
byte[] bytes = null;
while (true) {
long lastFp = reader.getPosition();
long crcValue;
try {
crcValue = logStream.readLong("crcvalue");
bytes = logStream.readBuffer("txnEntry");
} catch (EOFException e) {
break;
}
if (bytes.length == 0) {
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());
}
if (logStream.readByte("EOR") != 'B') {
throw new EOFException("Last transaction was partial.");
}
TxnHeader hdr = new TxnHeader();
Record r = SerializeUtils.deserializeTxn(bytes, hdr);
if (starttime == 0) {
starttime = hdr.getTime();
}
endtime = hdr.getTime();
if (size % skipN == 0) {
skiplist.addMark(hdr.getTime(), lastFp, size);
}
size++;
}
if (bytes == null) {
throw new IOException("Nothing read from ("+file+")");
}
} finally {
reader.close();
}
}
示例13: updateSerialSlow
import java.util.zip.Checksum; //导入方法依赖的package包/类
private static void updateSerialSlow(Checksum crc, byte[] b, int start, int length) {
for (int i = 0; i < length; i++)
crc.update(b[i+start]);
crc.getValue();
}
示例14: computePartialChunkCrc
import java.util.zip.Checksum; //导入方法依赖的package包/类
/**
* reads in the partial crc chunk and computes checksum
* of pre-existing data in partial chunk.
*/
private Checksum computePartialChunkCrc(long blkoff, long ckoff)
throws IOException {
// find offset of the beginning of partial chunk.
//
int sizePartialChunk = (int) (blkoff % bytesPerChecksum);
blkoff = blkoff - sizePartialChunk;
if (LOG.isDebugEnabled()) {
LOG.debug("computePartialChunkCrc for " + block
+ ": sizePartialChunk=" + sizePartialChunk
+ ", block offset=" + blkoff
+ ", metafile offset=" + ckoff);
}
// create an input stream from the block file
// and read in partial crc chunk into temporary buffer
//
byte[] buf = new byte[sizePartialChunk];
byte[] crcbuf = new byte[checksumSize];
try (ReplicaInputStreams instr =
datanode.data.getTmpInputStreams(block, blkoff, ckoff)) {
IOUtils.readFully(instr.getDataIn(), buf, 0, sizePartialChunk);
// open meta file and read in crc value computer earlier
IOUtils.readFully(instr.getChecksumIn(), crcbuf, 0, crcbuf.length);
}
// compute crc of partial chunk from data read in the block file.
final Checksum partialCrc = DataChecksum.newDataChecksum(
diskChecksum.getChecksumType(), diskChecksum.getBytesPerChecksum());
partialCrc.update(buf, 0, sizePartialChunk);
if (LOG.isDebugEnabled()) {
LOG.debug("Read in partial CRC chunk from disk for " + block);
}
// paranoia! verify that the pre-computed crc matches what we
// recalculated just now
if (partialCrc.getValue() != checksum2long(crcbuf)) {
String msg = "Partial CRC " + partialCrc.getValue() +
" does not match value computed the " +
" last time file was closed " +
checksum2long(crcbuf);
throw new IOException(msg);
}
return partialCrc;
}
示例15: compute
import java.util.zip.Checksum; //导入方法依赖的package包/类
/**
* Compute the CRC32C (Castagnoli) of the segment of the byte array given by the specified size and offset
*
* @param bytes The bytes to checksum
* @param offset the offset at which to begin the checksum computation
* @param size the number of bytes to checksum
* @return The CRC32C
*/
public static long compute(byte[] bytes, int offset, int size) {
Checksum crc = create();
crc.update(bytes, offset, size);
return crc.getValue();
}