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

x33g5p2x  于2022-01-17 转载在 其他  
字(7.2k)|赞(0)|评价(0)|浏览(310)

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

CRC32介绍

[英]The CRC32 class is used to compute a CRC32 checksum from data provided as input value. See also Adler32 which is almost as good, but cheaper.
[中]CRC32类用于根据作为输入值提供的数据计算CRC32校验和。另请参见Adler32,它几乎同样好,但更便宜。

代码示例

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

/** Returns a CRC of the remaining bytes in the stream. */
public String crc (InputStream input) {
  if (input == null) return "" + System.nanoTime(); // fallback
  CRC32 crc = new CRC32();
  byte[] buffer = new byte[4096];
  try {
    while (true) {
      int length = input.read(buffer);
      if (length == -1) break;
      crc.update(buffer, 0, length);
    }
  } catch (Exception ex) {
    try {
      input.close();
    } catch (Exception ignored) {
    }
  }
  return Long.toString(crc.getValue());
}

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

private static String hash(byte[] resource) {
  CRC_32.reset();
  CRC_32.update(resource);
  return Long.toHexString(CRC_32.getValue());
}

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

public static int crc32(byte[] array, int offset, int length) {
  CRC32 crc32 = new CRC32();
  crc32.update(array, offset, length);
  return (int) (crc32.getValue() & 0x7FFFFFFF);
}

代码示例来源:origin: iBotPeaches/Apktool

public static CRC32 calculateCrc(InputStream input) throws IOException {
  CRC32 crc = new CRC32();
  int bytesRead;
  byte[] buffer = new byte[8192];
  while((bytesRead = input.read(buffer)) != -1) {
    crc.update(buffer, 0, bytesRead);
  }
  return crc;
}

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

private void readZeroTerminated(boolean hcrc) throws IOException {
    int result;
    while ((result = in.read()) > 0) {
      if (hcrc) {
        crc.update(result);
      }
    }
    if (result == -1) {
      throw new EOFException();
    }
    // Add the zero
    if (hcrc) {
      crc.update(result);
    }
  }
}

代码示例来源:origin: org.apache.commons/commons-compress

if (inf.needsInput()) {
  in.mark(buf.length);
  bufUsed = in.read(buf);
  if (bufUsed == -1) {
    throw new EOFException();
  inf.setInput(buf, 0, bufUsed);
  ret = inf.inflate(b, off, len);
} catch (final DataFormatException e) {
  throw new IOException("Gzip-compressed data is corrupt");
crc.update(b, off, ret);
off += ret;
len -= ret;
  in.reset();
  final int skipAmount = bufUsed - inf.getRemaining();
  if (crcStored != crc.getValue()) {
    throw new IOException("Gzip-compressed data is corrupt "
               + "(CRC32 error)");

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

final InputStream markableInputStream = in.markSupported() ? in : new BufferedInputStream(in);
  final CRC32 cal = new CRC32();
  markableInputStream.mark(Integer.MAX_VALUE);
  final byte[] buffer = new byte[BUFFER_SIZE];
  int count = 0;
  do {
    size += count;
    cal.update(buffer, 0, count);
    count = markableInputStream.read(buffer, 0, buffer.length);
  } while (count != -1);
  markableInputStream.reset();
  ze.setSize(size);
  ze.setCrc(cal.getValue());

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

super(is, new Inflater(true), size);
byte[] header = new byte[10];
readFully(header, 0, header.length);
boolean hcrc = (flags & FHCRC) != 0;
if (hcrc) {
  crc.update(header, 0, header.length);
    crc.update(header, 0, 2);
    int result = in.read(buf, 0, max);
    if (result == -1) {
      throw new EOFException();
      crc.update(buf, 0, result);
  readFully(header, 0, 2);
  short crc16 = Memory.peekShort(header, 0, ByteOrder.LITTLE_ENDIAN);
  if ((short) crc.getValue() != crc16) {
    throw new IOException("CRC mismatch");
  crc.reset();

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

Arrays.checkOffsetAndCount(buffer.length, byteOffset, byteCount);
if (inf.finished() || currentEntry == null) {
  return -1;
    if ((len = in.read(buf)) == -1) {
      eof = true;
      return -1;
  lastRead += toRead;
  inRead += toRead;
  crc.update(buffer, byteOffset, toRead);
  return toRead;
if (inf.needsInput()) {
  fill();
  if (len > 0) {
  read = inf.inflate(buffer, byteOffset, byteCount);
} catch (DataFormatException e) {
  throw new ZipException(e.getMessage());
  return -1;
crc.update(buffer, byteOffset, read);
return read;

代码示例来源:origin: org.apache.commons/commons-compress

final int magic0 = in.read();
final int magic1 = in.read();
inf.reset();
crc.reset();

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

final int height = image.getHeight(null);
scrunch.finish();
bytePos = writeInt4(pngBytes, height, bytePos);
bytePos = writeBytes(pngBytes, new byte[]{8, 2, 0, 0, 0}, bytePos);
final CRC32 crc = new CRC32();
crc.reset();
crc.update(pngBytes, startPos, bytePos - startPos);
bytePos = writeInt4(pngBytes, (int) crc.getValue(), bytePos);
crc.reset();
bytePos = writeInt4(pngBytes, nCompressed, bytePos);
bytePos = writeBytes(pngBytes, IDAT, bytePos);
crc.update(IDAT);
System.arraycopy(outBytes.toByteArray(), 0, pngBytes, bytePos, outBytes.size());
outBytes.close();
outBytes = null;
crc.update(pngBytes, bytePos, nCompressed);
bytePos += nCompressed;
bytePos = writeInt4(pngBytes, (int) crc.getValue(), bytePos);
bytePos = writeInt4(pngBytes, 0, bytePos);
bytePos = writeBytes(pngBytes, IEND, bytePos);
crc.reset();
crc.update(IEND);
bytePos = writeInt4(pngBytes, (int) crc.getValue(), bytePos);
return pngBytes;

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

deflater.finish();
while (!deflater.finished()) {
  deflate(footer);
  if (!footer.isWritable()) {
  int crcValue = (int) crc.getValue();
  int uncBytes = deflater.getTotalIn();
  footer.writeByte(crcValue);
  footer.writeByte(crcValue >>> 8);

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

private JdkZlibDecoder(ZlibWrapper wrapper, byte[] dictionary, boolean decompressConcatenated) {
  if (wrapper == null) {
    throw new NullPointerException("wrapper");
  }
  this.decompressConcatenated = decompressConcatenated;
  switch (wrapper) {
    case GZIP:
      inflater = new Inflater(true);
      crc = ByteBufChecksum.wrapChecksum(new CRC32());
      break;
    case NONE:
      inflater = new Inflater(true);
      crc = null;
      break;
    case ZLIB:
      inflater = new Inflater();
      crc = null;
      break;
    case ZLIB_OR_NONE:
      // Postpone the decision until decode(...) is called.
      decideZlibOrNone = true;
      crc = null;
      break;
    default:
      throw new IllegalArgumentException("Only GZIP or ZLIB is supported, but you used " + wrapper);
  }
  this.dictionary = dictionary;
}

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

crc.update(inAry, offset, len);
deflater.setInput(inAry, offset, len);
for (;;) {
  deflate(out);
  if (deflater.needsInput()) {

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

/**
 * Resets the wrapped input stream and the CRC32 computation.
 *
 * @see java.io.InputStream#reset()
 */
@Override
public synchronized void reset() throws IOException {
  abortIfNeeded();
  crc32.reset();
  in.reset();
}

代码示例来源:origin: commons-io/commons-io

/**
 * Computes the checksum of a file using the CRC32 checksum routine.
 * The value of the checksum is returned.
 *
 * @param file the file to checksum, must not be {@code null}
 * @return the checksum value
 * @throws NullPointerException     if the file or checksum is {@code null}
 * @throws IllegalArgumentException if the file is a directory
 * @throws IOException              if an IO error occurs reading the file
 * @since 1.3
 */
public static long checksumCRC32(final File file) throws IOException {
  final CRC32 crc = new CRC32();
  checksum(file, crc);
  return crc.getValue();
}

代码示例来源:origin: google/guava

@Override
 public Checksum get() {
  return new CRC32();
 }
},

相关文章