本文整理了Java中java.util.zip.Checksum.getValue()
方法的一些代码示例,展示了Checksum.getValue()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Checksum.getValue()
方法的具体详情如下:
包路径:java.util.zip.Checksum
类名称:Checksum
方法名:getValue
[英]Returns the current calculated checksum value.
[中]返回当前计算的校验和值。
代码示例来源:origin: apache/kafka
/**
* 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();
}
代码示例来源:origin: aws/aws-sdk-java
private static long computePreludeCrc(ByteBuffer buf) {
byte[] prelude = new byte[Prelude.LENGTH];
buf.duplicate().get(prelude);
Checksum crc = new CRC32();
crc.update(prelude, 0, prelude.length);
return crc.getValue();
}
代码示例来源:origin: org.apache.poi/poi
/**
* Calculate checksum on input data
*/
public static long calculateChecksum(byte[] data) {
Checksum sum = new CRC32();
sum.update(data, 0, data.length);
return sum.getValue();
}
代码示例来源:origin: plutext/docx4j
public static long calculateChecksum(byte[] data) {
Checksum sum = new CRC32();
sum.update(data, 0, data.length);
return sum.getValue();
}
代码示例来源:origin: apache/incubator-pinot
public long computeCrc()
throws IOException {
byte[] buffer = new byte[BUFFER_SIZE];
Checksum checksum = new Adler32();
for (File file : _files) {
try (InputStream input = new FileInputStream(file)) {
int len;
while ((len = input.read(buffer)) > 0) {
checksum.update(buffer, 0, len);
}
}
}
return checksum.getValue();
}
代码示例来源:origin: org.apache.commons/commons-compress
/**
* Reads from the stream into a byte array.
* @throws IOException if the underlying stream throws or the
* stream is exhausted and the Checksum doesn't match the expected
* value
*/
@Override
public int read(final byte[] b, final int off, final int len) throws IOException {
final int ret = in.read(b, off, len);
if (ret >= 0) {
checksum.update(b, off, ret);
bytesRemaining -= ret;
}
if (bytesRemaining <= 0 && expectedChecksum != checksum.getValue()) {
throw new IOException("Checksum verification failed");
}
return ret;
}
代码示例来源:origin: org.apache.poi/poi
/**
* Calculate checksum on all the data read from input stream.
*
* This should be more efficient than the equivalent code
* {@code IOUtils.calculateChecksum(IOUtils.toByteArray(stream))}
*/
public static long calculateChecksum(InputStream stream) throws IOException {
Checksum sum = new CRC32();
byte[] buf = new byte[4096];
int count;
while ((count = stream.read(buf)) != -1) {
if (count > 0) {
sum.update(buf, 0, count);
}
}
return sum.getValue();
}
代码示例来源:origin: apache/flume
protected long calculateChecksum(byte[] body) {
checksum.reset();
checksum.update(body, 0, body.length);
return checksum.getValue();
}
代码示例来源:origin: org.apache.commons/commons-compress
/**
* Reads a single byte from the stream
* @throws IOException if the underlying stream throws or the
* stream is exhausted and the Checksum doesn't match the expected
* value
*/
@Override
public int read() throws IOException {
if (bytesRemaining <= 0) {
return -1;
}
final int ret = in.read();
if (ret >= 0) {
checksum.update(ret);
--bytesRemaining;
}
if (bytesRemaining == 0 && expectedChecksum != checksum.getValue()) {
throw new IOException("Checksum verification failed");
}
return ret;
}
代码示例来源:origin: aws/aws-sdk-java
private void encodeOrThrow(OutputStream os) throws IOException {
ByteArrayOutputStream headersAndPayload = new ByteArrayOutputStream();
{
DataOutputStream dos = new DataOutputStream(headersAndPayload);
for (Entry<String, HeaderValue> entry : headers.entrySet()) {
Header.encode(entry, dos);
}
dos.write(payload);
dos.flush();
}
int totalLength = Prelude.LENGTH_WITH_CRC + headersAndPayload.size() + 4;
{
byte[] preludeBytes = getPrelude(totalLength);
Checksum crc = new CRC32();
crc.update(preludeBytes, 0, preludeBytes.length);
DataOutputStream dos = new DataOutputStream(os);
dos.write(preludeBytes);
long value = crc.getValue();
int value1 = (int) value;
dos.writeInt(value1);
dos.flush();
}
headersAndPayload.writeTo(os);
}
代码示例来源:origin: apache/kafka
@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());
}
代码示例来源:origin: apache/kafka
@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());
}
代码示例来源:origin: apache/kafka
@Test
public void testUpdateInt() {
final int value = 1000;
final ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.putInt(value);
Checksum crc1 = Crc32C.create();
Checksum crc2 = Crc32C.create();
Checksums.updateInt(crc1, value);
crc2.update(buffer.array(), buffer.arrayOffset(), 4);
assertEquals("Crc values should be the same", crc1.getValue(), crc2.getValue());
}
代码示例来源:origin: apache/kafka
@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());
}
代码示例来源:origin: Netflix/EVCache
private boolean checkCRCChecksum(byte[] data, final ChunkInfo ci, boolean hasZF) {
if (data == null || data.length == 0) return false;
final Checksum checksum = new CRC32();
checksum.update(data, 0, data.length);
final long currentChecksum = checksum.getValue();
final long expectedChecksum = ci.getChecksum();
if (log.isDebugEnabled()) log.debug("CurrentChecksum : " + currentChecksum + "; ExpectedChecksum : "
+ expectedChecksum + " for key : " + ci.getKey());
if (currentChecksum != expectedChecksum) {
if (!hasZF) {
if (log.isWarnEnabled()) log.warn("CHECKSUM_ERROR : Chunks : " + ci.getChunks() + " ; "
+ "currentChecksum : " + currentChecksum + "; expectedChecksum : " + expectedChecksum
+ " for key : " + ci.getKey());
EVCacheMetricsFactory.increment(appName + "-CHECK_SUM_ERROR");
}
return false;
}
return true;
}
代码示例来源:origin: commons-codec/commons-codec
@Override
public void run() {
final long st = System.nanoTime();
crc.reset();
for (int i = 0; i < trials; i++) {
crc.update(bytes, 0, size);
}
final long et = System.nanoTime();
final double secsElapsed = (et - st) / 1000000000.0d;
results[index] = new BenchResult(crc.getValue(), mbProcessed/secsElapsed);
}
};
代码示例来源:origin: netty/netty
if (hasChecksum && checksum != null) {
checksum.reset();
checksum.update(output, outputPtr, originalLength);
final int checksumResult = (int) checksum.getValue();
if (checksumResult != currentChecksum) {
throw new DecompressionException(String.format(
代码示例来源:origin: greenrobot/essentials
@Test
public void testGetValueStable() {
checksum.update(INPUT16, 0, INPUT16.length);
long hash = checksum.getValue();
// Calling checksum.getValue() twice should not change hash
Assert.assertEquals(hash, checksum.getValue());
}
代码示例来源:origin: commons-io/commons-io
@Test
public void testChecksumCRC32() throws Exception {
// create a test file
final String text = "Imagination is more important than knowledge - Einstein";
final File file = new File(getTestDirectory(), "checksum-test.txt");
FileUtils.writeStringToFile(file, text, "US-ASCII");
// compute the expected checksum
final Checksum expectedChecksum = new CRC32();
expectedChecksum.update(text.getBytes("US-ASCII"), 0, text.length());
final long expectedValue = expectedChecksum.getValue();
// compute the checksum of the file
final long resultValue = FileUtils.checksumCRC32(file);
assertEquals(expectedValue, resultValue);
}
代码示例来源:origin: commons-io/commons-io
@Test
public void testChecksum() throws Exception {
// create a test file
final String text = "Imagination is more important than knowledge - Einstein";
final File file = new File(getTestDirectory(), "checksum-test.txt");
FileUtils.writeStringToFile(file, text, "US-ASCII");
// compute the expected checksum
final Checksum expectedChecksum = new CRC32();
expectedChecksum.update(text.getBytes("US-ASCII"), 0, text.length());
final long expectedValue = expectedChecksum.getValue();
// compute the checksum of the file
final Checksum testChecksum = new CRC32();
final Checksum resultChecksum = FileUtils.checksum(file, testChecksum);
final long resultValue = resultChecksum.getValue();
assertSame(testChecksum, resultChecksum);
assertEquals(expectedValue, resultValue);
}
内容来源于网络,如有侵权,请联系作者删除!