java.util.zip.Checksum.update()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(136)

本文整理了Java中java.util.zip.Checksum.update()方法的一些代码示例,展示了Checksum.update()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Checksum.update()方法的具体详情如下:
包路径:java.util.zip.Checksum
类名称:Checksum
方法名:update

Checksum.update介绍

[英]Updates the checksum value with the given byte.
[中]使用给定字节更新校验和值。

代码示例

代码示例来源: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: greenrobot/essentials

@Test
public void testMixedUnaligned() {
  checksum.update(INPUT16, 0, INPUT16.length);
  long hash = checksum.getValue();
  checksum.reset();
  checksum.update(INPUT16, 0, 2);
  checksum.update(INPUT16[2]);
  checksum.update(INPUT16, 3, 11);
  checksum.update(INPUT16[14]);
  checksum.update(INPUT16[15]);
  Assert.assertEquals(hash, checksum.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 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: 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: 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: 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

public void testExpectedHash(long expectedFor0, long expectedForInput4, long expectedForInput16) {
  checksum.update(0);
  checksum.update(0);
  checksum.update(0);
  checksum.update(0);
  Assert.assertEquals("0 (int)", expectedFor0, checksum.getValue());
  checksum.reset();
  checksum.update(INPUT4, 0, INPUT4.length);
  Assert.assertEquals("I4", expectedForInput4, checksum.getValue());
  checksum.reset();
  checksum.update(INPUT16, 0, INPUT16.length);
  Assert.assertEquals("I16", expectedForInput16, 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: 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());
}

相关文章