本文整理汇总了Java中java.util.zip.Checksum.update方法的典型用法代码示例。如果您正苦于以下问题:Java Checksum.update方法的具体用法?Java Checksum.update怎么用?Java Checksum.update使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.zip.Checksum
的用法示例。
在下文中一共展示了Checksum.update方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: computeCrc32
import java.util.zip.Checksum; //导入方法依赖的package包/类
/**
* Computes CRC code of data from InputStream
*
* @param is InputStream to read data from
* @return CRC code
*/
public 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;
}
示例2: computeCrc32
import java.util.zip.Checksum; //导入方法依赖的package包/类
private static String computeCrc32(final FileObject fo) throws IOException {
final Checksum crc = new CRC32();
try (final InputStream in = new BufferedInputStream(fo.getInputStream())) {
int last = -1;
int curr;
while ((curr = in.read()) != -1) {
if (curr != '\n' && last == '\r') { //NOI18N
crc.update('\n'); //NOI18N
}
if (curr != '\r') { //NOI18N
crc.update(curr);
}
last = curr;
}
if (last == '\r') { //NOI18N
crc.update('\n'); //NOI18N
}
}
int val = (int)crc.getValue();
String hex = Integer.toHexString(val);
while (hex.length() < 8) {
hex = "0" + hex; // NOI18N
}
return hex;
}
示例3: testUpdate
import java.util.zip.Checksum; //导入方法依赖的package包/类
@Test
public void testUpdate() {
final byte[] bytes = "Any String you want".getBytes();
final int len = bytes.length;
Checksum crc1 = Crc32C.create();
Checksum crc2 = Crc32C.create();
Checksum crc3 = Crc32C.create();
crc1.update(bytes, 0, len);
for (int i = 0; i < len; i++)
crc2.update(bytes[i]);
crc3.update(bytes, 0, len / 2);
crc3.update(bytes, len / 2, len - len / 2);
assertEquals("Crc values should be the same", crc1.getValue(), crc2.getValue());
assertEquals("Crc values should be the same", crc1.getValue(), crc3.getValue());
}
示例4: testDirectByteBufferOffset
import java.util.zip.Checksum; //导入方法依赖的package包/类
private static void testDirectByteBufferOffset(Checksum checksum, long expected) {
byte[] unaligned_bytes_123456789 = new byte[BYTES_123456789.length + 64];
for (int i = 0; i < unaligned_bytes_123456789.length - BYTES_123456789.length; i++) {
checksum.reset();
ByteBuffer bb = ByteBuffer.allocateDirect(unaligned_bytes_123456789.length);
System.arraycopy(BYTES_123456789, 0, unaligned_bytes_123456789, i, BYTES_123456789.length);
bb.put(unaligned_bytes_123456789);
bb.position(i);
bb.limit(i + BYTES_123456789.length);
checksum.update(bb);
checkChecksumOffset(checksum, expected, i);
}
}
示例5: testUpdateLong
import java.util.zip.Checksum; //导入方法依赖的package包/类
@Test
public void testUpdateLong() {
final long value = Integer.MAX_VALUE + 1;
final ByteBuffer buffer = ByteBuffer.allocate(8);
buffer.putLong(value);
Checksum crc1 = new Crc32();
Checksum crc2 = new Crc32();
Checksums.updateLong(crc1, value);
crc2.update(buffer.array(), buffer.arrayOffset(), 8);
assertEquals("Crc values should be the same", crc1.getValue(), crc2.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: testWrappedByteBufferOffset
import java.util.zip.Checksum; //导入方法依赖的package包/类
private static void testWrappedByteBufferOffset(Checksum checksum, long expected) {
byte[] unaligned_bytes_123456789 = new byte[BYTES_123456789.length + 64];
for (int i = 0; i < unaligned_bytes_123456789.length - BYTES_123456789.length; i++) {
checksum.reset();
System.arraycopy(BYTES_123456789, 0, unaligned_bytes_123456789, i, BYTES_123456789.length);
ByteBuffer bb = ByteBuffer.wrap(unaligned_bytes_123456789);
bb.position(i);
bb.limit(i + BYTES_123456789.length);
checksum.update(bb);
checkChecksumOffset(checksum, expected, i);
}
}
示例8: testLittleEndianReadonlyByteBufferOffset
import java.util.zip.Checksum; //导入方法依赖的package包/类
private static void testLittleEndianReadonlyByteBufferOffset(Checksum checksum, long expected) {
byte[] unaligned_bytes_123456789 = new byte[BYTES_123456789.length + 64];
for (int i = 0; i < unaligned_bytes_123456789.length - BYTES_123456789.length; i++) {
checksum.reset();
System.arraycopy(BYTES_123456789, 0, unaligned_bytes_123456789, i, BYTES_123456789.length);
ByteBuffer bb = ByteBuffer.wrap(unaligned_bytes_123456789).asReadOnlyBuffer();
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.position(i);
bb.limit(i + BYTES_123456789.length);
checksum.update(bb);
checkChecksumOffset(checksum, expected, i);
}
}
示例9: checksumTest
import java.util.zip.Checksum; //导入方法依赖的package包/类
@Test
public void checksumTest() throws Exception {
Checksum crc32Checksum = new CRC32();
crc32Checksum.update(example2_bytes_seven, 0, example2_bytes_seven.length);
assertEquals(crc32Checksum.getValue(), Bytes.from(example2_bytes_seven).transform(checksumCrc32()).resize(8).toLong());
assertEquals(Bytes.from(example2_bytes_seven, Bytes.from(crc32Checksum.getValue()).resize(4).array()), Bytes.from(example2_bytes_seven).transform(checksumAppendCrc32()));
Checksum adlerChecksum = new Adler32();
adlerChecksum.update(example2_bytes_seven, 0, example2_bytes_seven.length);
assertEquals(Bytes.from(adlerChecksum.getValue()).resize(4),
Bytes.from(example2_bytes_seven).transform(checksum(new Adler32(), ChecksumTransformer.Mode.TRANSFORM, 4)));
}
示例10: updateLong
import java.util.zip.Checksum; //导入方法依赖的package包/类
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 */);
}
示例11: update
import java.util.zip.Checksum; //导入方法依赖的package包/类
public static void update(Checksum checksum, ByteBuffer buffer, int offset, int length) {
if (buffer.hasArray()) {
checksum.update(buffer.array(), buffer.position() + buffer.arrayOffset() + offset, length);
} else {
int start = buffer.position() + offset;
for (int i = start; i < start + length; i++)
checksum.update(buffer.get(i));
}
}
示例12: 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();
}
示例13: testByteArray
import java.util.zip.Checksum; //导入方法依赖的package包/类
private static void testByteArray(Checksum checksum, long expected) {
checksum.reset();
checksum.update(BYTES_123456789);
checkChecksum(checksum, expected);
}
示例14: 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++;
}
}
示例15: 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();
}
}