org.apache.commons.io.FileUtils.checksum()方法的使用及代码示例

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

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

FileUtils.checksum介绍

[英]Computes the checksum of a file using the specified checksum object. Multiple files may be checked using one Checksum instance if desired simply by reusing the same checksum object. For example:

long csum = FileUtils.checksum(file, new CRC32()).getValue();

[中]使用指定的校验和对象计算文件的校验和。如果需要,可以使用一个Checksum实例检查多个文件,只需重复使用同一个校验和对象即可。例如:

long csum = FileUtils.checksum(file, new CRC32()).getValue();

代码示例

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

@Test
public void testChecksumOnDirectory() throws Exception {
  try {
    FileUtils.checksum(new File("."), new CRC32());
    fail();
  } catch (final IllegalArgumentException ex) {
    // expected
  }
}

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

@Test
public void testChecksumOnNullFile() throws Exception {
  try {
    FileUtils.checksum(null, new CRC32());
    fail();
  } catch (final NullPointerException ex) {
    // expected
  }
}

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

@Test
public void testChecksumDouble() throws Exception {
  // create a test file
  final String text1 = "Imagination is more important than knowledge - Einstein";
  final File file1 = new File(getTestDirectory(), "checksum-test.txt");
  FileUtils.writeStringToFile(file1, text1, "US-ASCII");
  // create a second test file
  final String text2 = "To be or not to be - Shakespeare";
  final File file2 = new File(getTestDirectory(), "checksum-test2.txt");
  FileUtils.writeStringToFile(file2, text2, "US-ASCII");
  // compute the expected checksum
  final Checksum expectedChecksum = new CRC32();
  expectedChecksum.update(text1.getBytes("US-ASCII"), 0, text1.length());
  expectedChecksum.update(text2.getBytes("US-ASCII"), 0, text2.length());
  final long expectedValue = expectedChecksum.getValue();
  // compute the checksum of the file
  final Checksum testChecksum = new CRC32();
  FileUtils.checksum(file1, testChecksum);
  FileUtils.checksum(file2, testChecksum);
  final long resultValue = testChecksum.getValue();
  assertEquals(expectedValue, resultValue);
}

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

@Test
public void testChecksum() 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 Checksum testChecksum = new CRC32();
  final Checksum resultChecksum = FileUtils.checksum(file, testChecksum);
  final long resultValue = resultChecksum.getValue();
  assertSame(testChecksum, resultChecksum);
  assertEquals(expectedValue, resultValue);
}

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

@Test
public void testChecksumOnNullChecksum() 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");
  try {
    FileUtils.checksum(file, null);
    fail();
  } catch (final NullPointerException ex) {
    // expected
  }
}

代码示例来源:origin: org.onosproject/onlab-thirdparty

/**
 * 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(File file) throws IOException {
  CRC32 crc = new CRC32();
  checksum(file, crc);
  return crc.getValue();
}

代码示例来源:origin: Nextdoor/bender

/**
 * 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 1.3
 */
public static long checksumCRC32(File file) throws IOException {
  CRC32 crc = new CRC32();
  checksum(file, crc);
  return crc.getValue();
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.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: io.github.stephenc.docker/docker-client-shaded

/**
 * 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: org.apache.commons/com.springsource.org.apache.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: com.impetus.fabric/fabric-jdbc-driver-shaded

/**
 * 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: org.gradle/gradle-base-services

public static Checksum checksum(File file, Checksum checksum) {
  try {
    return FileUtils.checksum(file, checksum);
  } catch (IOException e) {
    throw new UncheckedIOException(e);
  }
}

代码示例来源:origin: org.deeplearning4j/deeplearning4j-datasets

private void validateFiles(String[] files, long[] checksums){
  //Validate files:
  try {
    for (int i = 0; i < files.length; i++) {
      File f = new File(files[i]);
      Checksum adler = new Adler32();
      long checksum = f.exists() ? FileUtils.checksum(f, adler).getValue() : -1;
      if (!f.exists() || checksum != checksums[i]) {
        throw new IllegalStateException("Failed checksum: expected " + checksums[i] +
            ", got " + checksum + " for file: " + f);
      }
    }
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: org.deeplearning4j/deeplearning4j-zoo

log.info("Verifying download...");
Checksum adler = new Adler32();
FileUtils.checksum(cachedFile, adler);
long localChecksum = adler.getValue();
log.info("Checksum local is " + localChecksum + ", expecting " + expectedChecksum);

代码示例来源:origin: org.deeplearning4j/deeplearning4j-datasets

log.info("Verifying download...");
Checksum adler = new Adler32();
FileUtils.checksum(tmpFile, adler);
long localChecksum = adler.getValue();
log.info("Checksum local is " + localChecksum + ", expecting "+expectedChecksum(set));

代码示例来源:origin: net.sf.sf3jswing/kernel-core

sum = checksum.getValue();
} finally {
    install = sum != FileUtils.checksum(f, (Checksum) new MD5Checksum()).getValue();

相关文章

FileUtils类方法