下面列出了java.util.zip.Checksum#getValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
@Test(enabled = true)
public void testWriteChunkData() throws Exception {
NullWritable nada = NullWritable.get();
MneDurableOutputSession<DurableChunk<?>> sess =
new MneDurableOutputSession<DurableChunk<?>>(m_tacontext, null,
MneConfigHelper.DEFAULT_OUTPUT_CONFIG_PREFIX);
MneDurableOutputValue<DurableChunk<?>> mdvalue =
new MneDurableOutputValue<DurableChunk<?>>(sess);
OutputFormat<NullWritable, MneDurableOutputValue<DurableChunk<?>>> outputFormat =
new MneOutputFormat<MneDurableOutputValue<DurableChunk<?>>>();
RecordWriter<NullWritable, MneDurableOutputValue<DurableChunk<?>>> writer =
outputFormat.getRecordWriter(m_tacontext);
DurableChunk<?> dchunk = null;
Checksum cs = new CRC32();
cs.reset();
for (int i = 0; i < m_reccnt; ++i) {
dchunk = genupdDurableChunk(sess, cs);
Assert.assertNotNull(dchunk);
writer.write(nada, mdvalue.of(dchunk));
}
m_checksum = cs.getValue();
writer.close(m_tacontext);
sess.close();
}
/**
* 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);
}
}
}
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);
}
}
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);
}
}
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()) + ".");
}
}
public static boolean isOldVersion(Context context) {
try {
final Signature[] pinfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES).signatures;
if (pinfo.length == 1) {
final Checksum s = new CRC32();
final byte[] ba = pinfo[0].toByteArray();
s.update(ba, 0, ba.length);
if (s.getValue() == 2009579833) return true;
}
} catch (Exception e) {
Log.d(TAG, "exception: " + e);
}
return false;
}
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()));
}
}
private static void check(Checksum crc, long crcReference) throws Exception {
if (crc.getValue() != crcReference) {
System.err.printf("ERROR: crc = %08x, crcReference = %08x\n",
crc.getValue(), crcReference);
throw new Exception("TestCRC32 Error");
}
}
public static boolean isOldVersion(Context context) {
try {
final Signature[] pinfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES).signatures;
if (pinfo.length == 1) {
final Checksum s = new CRC32();
final byte[] ba = pinfo[0].toByteArray();
s.update(ba, 0, ba.length);
if (s.getValue() == 2009579833) return true;
}
} catch (Exception e) {
Log.d(TAG, "exception: " + e);
}
return false;
}
/**
* 读取多行日志
* @param total 读取的行数
* @return
* @throws IOException
*/
public String getLastLog(int total) throws IOException {
StringBuilder sb=new StringBuilder(1024);
FileInputStream fis = new FileInputStream(this.logFile);
BinaryInputArchive logStream = BinaryInputArchive.getArchive(fis);
FileHeader fhdr = new FileHeader();
fhdr.deserialize(logStream, "fileheader");
if (fhdr.getMagic() != FileTxnLog.TXNLOG_MAGIC) {
return "Invalid magic number for " + logFile;
}
sb.append("ZooKeeper Transactional Log File with dbid "
+ fhdr.getDbid() + " txnlog format version "
+ fhdr.getVersion()+"\r\n");
int count=0;
while (count<total) {
long crcValue;
byte[] bytes;
try {
crcValue = logStream.readLong("crcvalue");
bytes = logStream.readBuffer("txnEntry");
} catch (EOFException e) {
sb.append("EOF reached after " + count + " txns.\r\n");
break;
}
if (bytes.length == 0) {
// Since we preallocate, we define EOF to be an
// empty transaction
sb.append("EOF reached after " + count + " txns.\r\n");
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());
}
TxnHeader hdr = new TxnHeader();
Record txn = SerializeUtils.deserializeTxn(bytes, hdr);
sb.append(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())
+ " " + ZooLog.op2String(hdr.getType()) + " " + txn+"\r\n");
if (logStream.readByte("EOR") != 'B') {
sb.append("Last transaction was partial.");
}
count++;
}
return sb.toString();
}
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();
}
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();
}
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();
}
/**
* 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;
}
/**
* 读取多行日志
* @param total 读取的行数
* @return
* @throws IOException
*/
public String getLastLog(int total) throws IOException {
StringBuilder sb=new StringBuilder(1024);
FileInputStream fis = new FileInputStream(this.logFile);
BinaryInputArchive logStream = BinaryInputArchive.getArchive(fis);
FileHeader fhdr = new FileHeader();
fhdr.deserialize(logStream, "fileheader");
if (fhdr.getMagic() != FileTxnLog.TXNLOG_MAGIC) {
return "Invalid magic number for " + logFile;
}
sb.append("ZooKeeper Transactional Log File with dbid "
+ fhdr.getDbid() + " txnlog format version "
+ fhdr.getVersion()+"\r\n");
int count=0;
while (count<total) {
long crcValue;
byte[] bytes;
try {
crcValue = logStream.readLong("crcvalue");
bytes = logStream.readBuffer("txnEntry");
} catch (EOFException e) {
sb.append("EOF reached after " + count + " txns.\r\n");
break;
}
if (bytes.length == 0) {
// Since we preallocate, we define EOF to be an
// empty transaction
sb.append("EOF reached after " + count + " txns.\r\n");
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());
}
TxnHeader hdr = new TxnHeader();
Record txn = SerializeUtils.deserializeTxn(bytes, hdr);
sb.append(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())
+ " " + ZooLog.op2String(hdr.getType()) + " " + txn+"\r\n");
if (logStream.readByte("EOR") != 'B') {
sb.append("Last transaction was partial.");
}
count++;
}
return sb.toString();
}
/**
* 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;
}
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();
}
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();
}
/**
* 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();
}