View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.spi.connector.checksum;
20  
21  import java.io.BufferedInputStream;
22  import java.io.ByteArrayInputStream;
23  import java.io.File;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.nio.ByteBuffer;
27  import java.nio.file.Files;
28  import java.util.LinkedHashMap;
29  import java.util.List;
30  import java.util.Map;
31  
32  /**
33   * Helper for checksum operations.
34   *
35   * @since 1.8.0
36   */
37  public final class ChecksumAlgorithmHelper {
38      private ChecksumAlgorithmHelper() {
39          // nop
40      }
41  
42      /**
43       * Calculates checksums for specified data.
44       *
45       * @param data        The content for which to calculate checksums, must not be {@code null}.
46       * @param factories   The checksum algorithm factories to use, must not be {@code null}.
47       * @return The calculated checksums, indexed by algorithm name, or the exception that occurred while trying to
48       * calculate it, never {@code null}.
49       * @throws IOException In case of any problem.
50       */
51      public static Map<String, String> calculate(byte[] data, List<ChecksumAlgorithmFactory> factories)
52              throws IOException {
53          try (InputStream inputStream = new ByteArrayInputStream(data)) {
54              return calculate(inputStream, factories);
55          }
56      }
57  
58      /**
59       * Calculates checksums for specified file.
60       *
61       * @param file        The file for which to calculate checksums, must not be {@code null}.
62       * @param factories   The checksum algorithm factories to use, must not be {@code null}.
63       * @return The calculated checksums, indexed by algorithm name, or the exception that occurred while trying to
64       * calculate it, never {@code null}.
65       * @throws IOException In case of any problem.
66       */
67      public static Map<String, String> calculate(File file, List<ChecksumAlgorithmFactory> factories)
68              throws IOException {
69          try (InputStream inputStream = new BufferedInputStream(Files.newInputStream(file.toPath()))) {
70              return calculate(inputStream, factories);
71          }
72      }
73  
74      private static Map<String, String> calculate(InputStream inputStream, List<ChecksumAlgorithmFactory> factories)
75              throws IOException {
76          LinkedHashMap<String, ChecksumAlgorithm> algorithms = new LinkedHashMap<>();
77          factories.forEach(f -> algorithms.put(f.getName(), f.getAlgorithm()));
78          final byte[] buffer = new byte[1024 * 32];
79          for (; ; ) {
80              int read = inputStream.read(buffer);
81              if (read < 0) {
82                  break;
83              }
84              for (ChecksumAlgorithm checksumAlgorithm : algorithms.values()) {
85                  checksumAlgorithm.update(ByteBuffer.wrap(buffer, 0, read));
86              }
87          }
88          LinkedHashMap<String, String> result = new LinkedHashMap<>();
89          algorithms.forEach((k, v) -> result.put(k, v.checksum()));
90          return result;
91      }
92  }