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

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

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

CRC32.update介绍

[英]Updates this checksum with the byte value provided as integer.
[中]使用作为整数提供的字节值更新此校验和。

代码示例

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

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

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

public static long getCrc32(byte[] b) {
  CRC32 crc = crc32Provider.get();
  crc.reset();
  crc.update(b);
  return crc.getValue();
}

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

/**
 * Generate crc32 for WriteOp.
 */
public static Long writeOpCRC32(String stream, byte[] payload) {
  CRC32 crc = requestCRC.get();
  try {
    crc.update(stream.getBytes(UTF_8));
    crc.update(payload);
    return crc.getValue();
  } finally {
    crc.reset();
  }
}

代码示例来源:origin: Tencent/tinker

public static long getFileCrc32(File file) throws IOException {
  InputStream inputStream = null;
  try {
    inputStream = new BufferedInputStream(new FileInputStream(file));
    CRC32 crc = new CRC32();
    int cnt;
    while ((cnt = inputStream.read()) != -1) {
      crc.update(cnt);
    }
    return crc.getValue();
  } finally {
    StreamUtil.closeQuietly(inputStream);
  }
}

代码示例来源:origin: twitter/distributedlog

/**
   * Generate crc32 for any op which only passes a stream name.
   */
  public static Long streamOpCRC32(String stream) {
    CRC32 crc = requestCRC.get();
    try {
      crc.update(stream.getBytes(UTF_8));
      return crc.getValue();
    } finally {
      crc.reset();
    }
  }
}

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

/** Returns a CRC of the remaining bytes in the stream. */
public String crc (InputStream input) {
  if (input == null) throw new IllegalArgumentException("input cannot be null.");
  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) {
  } finally {
    StreamUtils.closeQuietly(input);
  }
  return Long.toString(crc.getValue(), 16);
}

代码示例来源:origin: twitter/distributedlog

/**
 * Generate crc32 for TruncateOp.
 */
public static Long truncateOpCRC32(String stream, DLSN dlsn) {
  CRC32 crc = requestCRC.get();
  try {
    crc.update(stream.getBytes(UTF_8));
    crc.update(dlsn.serializeBytes());
    return crc.getValue();
  } finally {
    crc.reset();
  }
}

代码示例来源:origin: killme2008/Metamorphosis

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

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

/**
   * @param crcAlgo CRC algorithm.
   * @param buf Input buffer.
   * @param len Buffer length.
   *
   * @return Crc checksum.
   */
  private static int calcCrc(CRC32 crcAlgo, ByteBuffer buf, int len) {
    int initLimit = buf.limit();

    buf.limit(buf.position() + len);

    crcAlgo.update(buf);

    buf.limit(initLimit);

    return (int)crcAlgo.getValue() ^ 0xFFFFFFFF;
  }
}

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

/** Returns a CRC of the remaining bytes in the stream. */
public String crc (InputStream input) {
  if (input == null) throw new IllegalArgumentException("input cannot be null.");
  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) {
  } finally {
    StreamUtils.closeQuietly(input);
  }
  return Long.toString(crc.getValue(), 16);
}

代码示例来源:origin: alipay/sofa-bolt

/**
 * Compute CRC32 code for byte[].
 *
 * @param array
 * @param offset
 * @param length
 * @return
 */
public static final int crc32(byte[] array, int offset, int length) {
  CRC32 crc32 = CRC_32_THREAD_LOCAL.get();
  crc32.update(array, offset, length);
  int ret = (int) crc32.getValue();
  crc32.reset();
  return ret;
}

代码示例来源:origin: spring-projects/spring-loaded

private void dumpinfo(byte[] bytes) {
  CRC32 crc = new CRC32();
  crc.update(bytes,0,bytes.length);
  System.out.println("byteinfo:len="+bytes.length+":crc="+Long.toHexString(crc.getValue()));
}

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

/**
 * CRC32 for string
 */
public LongWritable evaluate(Text n) {
 if (n == null) {
  return null;
 }
 crc32.reset();
 crc32.update(n.getBytes(), 0, n.getLength());
 result.set(crc32.getValue());
 return result;
}

代码示例来源:origin: goldmansachs/gs-collections

private static long calculateChecksum(String string)
{
  CRC32 checksum = new CRC32();
  checksum.update(string.getBytes(StandardCharsets.UTF_8));
  return checksum.getValue();
}

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

/**
 * CRC32 for string
 */
public LongWritable evaluate(Text n) {
 if (n == null) {
  return null;
 }
 crc32.reset();
 crc32.update(n.getBytes(), 0, n.getLength());
 result.set(crc32.getValue());
 return result;
}

代码示例来源:origin: springside/springside4

/**
 * 对输入字符串进行crc32散列返回int, 返回值有可能是负数.
 * 
 * Guava也有crc32实现, 但返回值无法返回long,所以统一使用JDK默认实现
 */
public static int crc32AsInt(@NotNull byte[] input) {
  CRC32 crc32 = new CRC32();
  crc32.update(input);
  // CRC32 只是 32bit int,为了CheckSum接口强转成long,此处再次转回来
  return (int) crc32.getValue();
}

相关文章

微信公众号

最新文章

更多