java.util.zip.Checksum类的使用及代码示例

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

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

Checksum介绍

[英]The interface common to checksum classes such as Adler32 and CRC32.
[中]校验和类(如Adler32和CRC32)通用的接口。

代码示例

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

protected long calculateChecksum(byte[] body) {
 checksum.reset();
 checksum.update(body, 0, body.length);
 return checksum.getValue();
}

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

public static long calculateChecksum(byte[] data) {
  Checksum sum = new CRC32();
  sum.update(data, 0, data.length);
  return sum.getValue();
}

代码示例来源:origin: greenrobot/essentials

public static void updateChecksum(InputStream in, Checksum checksum) throws IOException {
  byte[] buffer = new byte[BUFFER_SIZE];
  while (true) {
    int read = in.read(buffer);
    if (read == -1) {
      break;
    }
    checksum.update(buffer, 0, read);
  }
}

代码示例来源:origin: robovm/robovm

/**
 * Writes the specified byte to the underlying stream. The checksum is
 * updated with {@code val}.
 *
 * @param val
 *            the data value to written to the output stream.
 * @throws IOException
 *             if an IO error has occurred.
 */
@Override
public void write(int val) throws IOException {
  out.write(val);
  check.update(val);
}

代码示例来源:origin: org.apache.zookeeper/zookeeper

LOG.info("Reading snapshot " + snap);
  snapIS = new BufferedInputStream(new FileInputStream(snap));
  crcIn = new CheckedInputStream(snapIS, new Adler32());
  InputArchive ia = BinaryInputArchive.getArchive(crcIn);
  deserialize(dt,sessions, ia);
  long checkSum = crcIn.getChecksum().getValue();
  long val = ia.readLong("val");
  if (val != checkSum) {
} finally {
  if (snapIS != null) 
    snapIS.close();
  if (crcIn != null) 
    crcIn.close();

代码示例来源:origin: aws/aws-sdk-java

public void encode(OutputStream os) {
  try {
    CheckedOutputStream checkedOutputStream = new CheckedOutputStream(os, new CRC32());
    encodeOrThrow(checkedOutputStream);
    long messageCrc = checkedOutputStream.getChecksum().getValue();
    os.write((int) (0xFF & messageCrc >> 24));
    os.write((int) (0xFF & messageCrc >> 16));
    os.write((int) (0xFF & messageCrc >> 8));
    os.write((int) (0xFF & messageCrc));
    os.flush();
  } catch (IOException ex) {
    throw new RuntimeException(ex);
  }
}

代码示例来源:origin: apache/nifi

private void verifyChecksum(final Checksum checksum, final InputStream in, final OutputStream out, final String peerDescription, final int flowFileCount) throws IOException {
  final long expectedChecksum = readChecksum(in);
  if (checksum.getValue() == expectedChecksum) {
    logger.debug("Checksum from Peer {} matched the checksum that was calculated. Writing confirmation.", peerDescription);
    out.write(CONFIRM_CHECKSUM);
    out.flush();
  } else {
    logger.error("Received {} FlowFiles from peer {} but the Checksum reported by the peer ({}) did not match the checksum that was calculated ({}). Will reject the transaction.",
        flowFileCount, peerDescription, expectedChecksum, checksum.getValue());
    out.write(REJECT_CHECKSUM);
    out.flush();
    throw new TransactionAbortedException("Transaction with Peer " + peerDescription + " was aborted because the calculated checksum did not match the checksum provided by peer.");
  }
}

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

if (bytesRead < 1) {
  flowFileInputStream.close();
  flowFileInputStream = null;
  phase = TransactionPhase.GET_NEXT_FLOWFILE;
  buffer.rewind();
  checksum.update(LoadBalanceProtocolConstants.NO_DATA_FRAME);
checksum.update(frameArray, 0, frameArray.length);

代码示例来源:origin: org.apache.zookeeper/zookeeper

/**
 * serialize the datatree and session into the file snapshot
 * @param dt the datatree to be serialized
 * @param sessions the sessions to be serialized
 * @param snapShot the file to store snapshot into
 */
public synchronized void serialize(DataTree dt, Map<Long, Integer> sessions, File snapShot)
    throws IOException {
  if (!close) {
    OutputStream sessOS = new BufferedOutputStream(new FileOutputStream(snapShot));
    CheckedOutputStream crcOut = new CheckedOutputStream(sessOS, new Adler32());
    //CheckedOutputStream cout = new CheckedOutputStream()
    OutputArchive oa = BinaryOutputArchive.getArchive(crcOut);
    FileHeader header = new FileHeader(SNAP_MAGIC, VERSION, dbId);
    serialize(dt,sessions,oa, header);
    long val = crcOut.getChecksum().getValue();
    oa.writeLong(val, "val");
    oa.writeString("/", "path");
    sessOS.flush();
    crcOut.close();
    sessOS.close();
  }
}

代码示例来源:origin: freenet/fred

@Override
public void close() throws IOException {
  if(writeChecksum) {
    synchronized(this) {
      if(closed) throw new IOException("Already closed");
      closed = true;
    }
    out.write(Fields.intToBytes((int)crc.getValue()));
  }
}

代码示例来源:origin: org.apache.portals.pluto/pluto-util

public synchronized void write(int b) throws IOException {
  try {
    out.write(b);
    count++;
    CRC.update(b);
  } catch (IOException e) {
    count = 0;
    CRC.reset();
    throw e;
  }
}

代码示例来源:origin: org.apache.portals.pluto/pluto-util

/**
 * Closes the underlying output stream, resets the
 * checksum calculator, and clears the byte count.
 */
public void close() throws IOException {
  try {
    out.close();
  } finally {
    CRC.reset();
    count = 0;
  }
}

代码示例来源:origin: pentaho/pentaho-kettle

cis = new CheckedInputStream( KettleVFS.getInputStream( file ), new CRC32() );
byte[] buf = new byte[128];
int readSize = 0;
do {
 readSize = cis.read( buf );
} while ( readSize >= 0 );
checksum = cis.getChecksum().getValue();

代码示例来源:origin: looly/hutool

/**
 * 计算文件CRC32校验码
 * 
 * @param file 文件,不能为目录
 * @return CRC32值
 * @throws IORuntimeException IO异常
 * @since 4.0.6
 */
public static long checksumCRC32(File file) throws IORuntimeException {
  return checksum(file, new CRC32()).getValue();
}

代码示例来源:origin: soabase/exhibitor

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());

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

public static long getChecksum(String test) {
  try {
    byte buffer[] = test.getBytes();
    ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
    CheckedInputStream cis = new CheckedInputStream(bais, new Adler32());
    byte readBuffer[] = new byte[buffer.length];
    cis.read(readBuffer);
    return cis.getChecksum().getValue();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

相关文章