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