下面列出了java.util.zip.Checksum#update ( ) 实例代码,或者点击链接到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.
*/
private 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;
}
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;
}
public static void outputRecords(OutputStream out,
boolean useAscii,
Unsigned16 firstRecordNumber,
Unsigned16 recordsToGenerate,
Unsigned16 checksum
) throws IOException {
byte[] row = new byte[100];
Unsigned16 recordNumber = new Unsigned16(firstRecordNumber);
Unsigned16 lastRecordNumber = new Unsigned16(firstRecordNumber);
Checksum crc = new PureJavaCrc32();
Unsigned16 tmp = new Unsigned16();
lastRecordNumber.add(recordsToGenerate);
Unsigned16 ONE = new Unsigned16(1);
Unsigned16 rand = Random16.skipAhead(firstRecordNumber);
while (!recordNumber.equals(lastRecordNumber)) {
Random16.nextRand(rand);
if (useAscii) {
generateAsciiRecord(row, rand, recordNumber);
} else {
generateRecord(row, rand, recordNumber);
}
if (checksum != null) {
crc.reset();
crc.update(row, 0, row.length);
tmp.set(crc.getValue());
checksum.add(tmp);
}
recordNumber.add(ONE);
out.write(row);
}
}
private static void testDirectByteBuffer(Checksum checksum, long expected) {
checksum.reset();
ByteBuffer bb = ByteBuffer.allocateDirect(BYTES_123456789.length);
bb.put(BYTES_123456789);
bb.rewind();
checksum.update(bb);
checkChecksum(checksum, expected);
}
public static void updateLong(Checksum checksum, long input) {
checksum.update((byte) (input >> 56));
checksum.update((byte) (input >> 48));
checksum.update((byte) (input >> 40));
checksum.update((byte) (input >> 32));
checksum.update((byte) (input >> 24));
checksum.update((byte) (input >> 16));
checksum.update((byte) (input >> 8));
checksum.update((byte) input /* >> 0 */);
}
protected DurableBuffer<NonVolatileMemAllocator>
genuptBuffer(NonVolatileMemAllocator act, Checksum cs, int size) {
DurableBuffer<NonVolatileMemAllocator> ret = null;
ret = act.createBuffer(size, false);
if (null == ret) {
throw new OutOfHybridMemory("Create Durable Buffer Failed.");
}
ret.get().clear();
byte[] rdbytes = RandomUtils.nextBytes(size);
Assert.assertNotNull(rdbytes);
ret.get().put(rdbytes);
cs.update(rdbytes, 0, rdbytes.length);
ret.get().clear();
return ret;
}
public static void outputRecords(OutputStream out,
boolean useAscii,
Unsigned16 firstRecordNumber,
Unsigned16 recordsToGenerate,
Unsigned16 checksum
) throws IOException {
byte[] row = new byte[100];
Unsigned16 recordNumber = new Unsigned16(firstRecordNumber);
Unsigned16 lastRecordNumber = new Unsigned16(firstRecordNumber);
Checksum crc = new PureJavaCrc32();
Unsigned16 tmp = new Unsigned16();
lastRecordNumber.add(recordsToGenerate);
Unsigned16 ONE = new Unsigned16(1);
Unsigned16 rand = Random16.skipAhead(firstRecordNumber);
while (!recordNumber.equals(lastRecordNumber)) {
Random16.nextRand(rand);
if (useAscii) {
generateAsciiRecord(row, rand, recordNumber);
} else {
generateRecord(row, rand, recordNumber);
}
if (checksum != null) {
crc.reset();
crc.update(row, 0, row.length);
tmp.set(crc.getValue());
checksum.add(tmp);
}
recordNumber.add(ONE);
out.write(row);
}
}
/**
* Calculates the CRC32 checksum of the specified buffer.
*
* @param buffer
* The buffer.
* @return The CRC32 checksum.
*/
public static int getCrcChecksum(ByteBuffer buffer) {
Checksum crc = new CRC32();
for (int i = 0; i < buffer.limit(); i++) {
crc.update(buffer.get(i));
}
return (int) crc.getValue();
}
public static void outputRecords(OutputStream out,
boolean useAscii,
Unsigned16 firstRecordNumber,
Unsigned16 recordsToGenerate,
Unsigned16 checksum
) throws IOException {
byte[] row = new byte[100];
Unsigned16 recordNumber = new Unsigned16(firstRecordNumber);
Unsigned16 lastRecordNumber = new Unsigned16(firstRecordNumber);
Checksum crc = new PureJavaCrc32();
Unsigned16 tmp = new Unsigned16();
lastRecordNumber.add(recordsToGenerate);
Unsigned16 ONE = new Unsigned16(1);
Unsigned16 rand = Random16.skipAhead(firstRecordNumber);
while (!recordNumber.equals(lastRecordNumber)) {
Random16.nextRand(rand);
if (useAscii) {
generateAsciiRecord(row, rand, recordNumber);
} else {
generateRecord(row, rand, recordNumber);
}
if (checksum != null) {
crc.reset();
crc.update(row, 0, row.length);
tmp.set(crc.getValue());
checksum.add(tmp);
}
recordNumber.add(ONE);
out.write(row);
}
}
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;
}
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 long getChecksum(byte[] data) {
Checksum crc = new CRC32();
crc.update(data, 0, data.length);
return crc.getValue();
}
@Test(enabled = true, dependsOnMethods = {"testWriteChunkData"})
public void testReadChunkData() throws Exception {
List<String> partfns = new ArrayList<String>();
long reccnt = 0L;
long tsize = 0L;
Checksum cs = new CRC32();
cs.reset();
File folder = new File(m_workdir.toString());
File[] listfiles = folder.listFiles();
for (int idx = 0; idx < listfiles.length; ++idx) {
if (listfiles[idx].isFile()
&& listfiles[idx].getName().startsWith(MneConfigHelper.getBaseOutputName(m_conf, null))
&& listfiles[idx].getName().endsWith(MneConfigHelper.DEFAULT_FILE_EXTENSION)) {
partfns.add(listfiles[idx].getName());
}
}
Collections.sort(partfns); // keep the order for checksum
for (int idx = 0; idx < partfns.size(); ++idx) {
System.out.println(String.format("Verifying : %s", partfns.get(idx)));
FileSplit split = new FileSplit(
new Path(m_workdir, partfns.get(idx)), 0, 0L, new String[0]);
InputFormat<NullWritable, MneDurableInputValue<DurableChunk<?>>> inputFormat =
new MneInputFormat<MneDurableInputValue<DurableChunk<?>>, DurableChunk<?>>();
RecordReader<NullWritable, MneDurableInputValue<DurableChunk<?>>> reader =
inputFormat.getRecordReader(split, m_conf, null);
MneDurableInputValue<DurableChunk<?>> dchkval = null;
NullWritable dchkkey = reader.createKey();
while (true) {
dchkval = reader.createValue();
if (reader.next(dchkkey, dchkval)) {
byte b;
for (int j = 0; j < dchkval.getValue().getSize(); ++j) {
b = unsafe.getByte(dchkval.getValue().get() + j);
cs.update(b);
}
tsize += dchkval.getValue().getSize();
++reccnt;
} else {
break;
}
}
reader.close();
}
AssertJUnit.assertEquals(m_reccnt, reccnt);
AssertJUnit.assertEquals(m_totalsize, tsize);
AssertJUnit.assertEquals(m_checksum, cs.getValue());
System.out.println(String.format("The checksum of chunk is %d", m_checksum));
}
/**
* 读取多行日志
* @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 updateSerial(Checksum crc, byte[] b, int start, int length) {
for (int i = 0; i < length; i++)
crc.update(b[i+start]);
}
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 updateSerial(Checksum crc, byte[] b, int start, int length) {
for (int i = 0; i < length; i++)
crc.update(b[i+start]);
}
private static long crc32(String input) {
byte[] bytes = input.getBytes();
Checksum checksum = new CRC32();
checksum.update(bytes, 0, bytes.length);
return checksum.getValue();
}
public static long calcChecksum(byte[] data){
Checksum crc = new CRC32();
crc.update(data, 0, data.length);
return crc.getValue();
}