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

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

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

CRC32.<init>介绍

[英]Creates a new CRC32 object.
[中]创建新的CRC32对象。

代码示例

代码示例来源: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: 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: 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: org.apache.commons/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</code>
 * @return the checksum value
 * @throws NullPointerException if the file or checksum is <code>null</code>
 * @throws IllegalArgumentException if the file is a directory
 * @throws IOException if an IO error occurs reading the file
 * @since Commons IO 1.3
 */
public static long checksumCRC32(File file) throws IOException {
  CRC32 crc = new CRC32();
  checksum(file, crc);
  return crc.getValue();
}

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

@Test
  public void testHandleNeedsConnectionLeftOpen() throws Exception {
    ByteArrayInputStream bais = new ByteArrayInputStream(
        "{\"key\" :\"Content\"}".getBytes(StringUtils.UTF8));
    CRC32 crc32 = new CRC32();
    crc32.update("{\"key\" :\"Content\"}".getBytes(StringUtils.UTF8));
    HttpResponse response = new HttpResponse.Builder().statusText("testResponse")
        .statusCode(200).header("testKey", "testValue").content(bais).build();

    final List<InputStream> capture = new ArrayList<InputStream>();

    Unmarshaller<String, JsonUnmarshallerContext> unmarshaller = new Unmarshaller<String, JsonUnmarshallerContext>() {

      @Override
      public String unmarshall(JsonUnmarshallerContext in) throws Exception {
        capture.add(in.getHttpResponse().getContent());
        return "OpenConnection";
      }

    };

    JsonResponseHandler<String> toTest = new JsonResponseHandler<String>(unmarshaller);
    toTest.needsConnectionLeftOpen = true;
    assertTrue(toTest.needsConnectionLeftOpen());

    AmazonWebServiceResponse<String> awsResponse = toTest.handle(response);
    assertEquals(awsResponse.getResult(), "OpenConnection");
    assertSame(capture.get(0), bais);
  }
}

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

final CRC32 crc = new CRC32();
String calculatedCRC = String.valueOf(crc.getValue());
writeTransactionResponse(false, ResponseCode.CONFIRM_TRANSACTION, commsSession, calculatedCRC);

代码示例来源: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: com.google.gwt/gwt-servlet

public static String getSerializationSignature(Class<?> instanceType,
  SerializationPolicy policy) {
 String result;
 result = classCRC32Cache.get(instanceType);
 if (result == null) {
  CRC32 crc = new CRC32();
  try {
   generateSerializationSignature(instanceType, crc, policy);
  } catch (UnsupportedEncodingException e) {
   throw new RuntimeException("Could not compute the serialization signature", e);
  }
  result = Long.toString(crc.getValue());
  classCRC32Cache.put(instanceType, result);
 }
 return result;
}

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

private static int crc32(BytesReference data) {
  OutputStream dummy = new OutputStream() {
    @Override
    public void write(int b) throws IOException {
      // no-op
    }
    @Override
    public void write(byte[] b, int off, int len) throws IOException {
      // no-op
    }
  };
  CRC32 crc32 = new CRC32();
  try {
    data.writeTo(new CheckedOutputStream(dummy, crc32));
  } catch (IOException bogus) {
    // cannot happen
    throw new Error(bogus);
  }
  return (int) crc32.getValue();
}

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

/**
 * Create a {@link CompressedXContent} out of a {@link ToXContent} instance.
 */
public CompressedXContent(ToXContent xcontent, XContentType type, ToXContent.Params params) throws IOException {
  BytesStreamOutput bStream = new BytesStreamOutput();
  OutputStream compressedStream = CompressorFactory.COMPRESSOR.streamOutput(bStream);
  CRC32 crc32 = new CRC32();
  OutputStream checkedStream = new CheckedOutputStream(compressedStream, crc32);
  try (XContentBuilder builder = XContentFactory.contentBuilder(type, checkedStream)) {
    builder.startObject();
    xcontent.toXContent(builder, params);
    builder.endObject();
  }
  this.bytes = BytesReference.toBytes(bStream.bytes());
  this.crc32 = (int) crc32.getValue();
  assertConsistent();
}

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

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

代码示例来源: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();
}

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

/**
 * 对输入字符串进行crc32散列,与php兼容,在64bit系统下返回永远是正数的long
 * 
 * Guava也有crc32实现, 但返回值无法返回long,所以统一使用JDK默认实现
 */
public static long crc32AsLong(@NotNull byte[] input) {
  CRC32 crc32 = new CRC32();
  crc32.update(input);
  return crc32.getValue();
}

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

public static int crc32(ByteBuffer buf) {
  CRC32 checksum = new CRC32();
  while (buf.hasRemaining()) {
    checksum.update(buf.get());
  }
  return (int) checksum.getValue();
}

相关文章

微信公众号

最新文章

更多