This project has retired. For details please refer to its Attic page.
ChecksumUtil xref
View Javadoc
1   package org.apache.archiva.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.FileNotFoundException;
23  import java.io.IOException;
24  import java.nio.MappedByteBuffer;
25  import java.nio.channels.FileChannel;
26  import java.nio.file.Path;
27  import java.nio.file.StandardOpenOption;
28  import java.util.List;
29  import java.util.stream.Collectors;
30  
31  /**
32   * Utility class that handles multiple checksums for a single file.
33   */
34  public class ChecksumUtil {
35  
36  
37      static final int BUFFER_SIZE = 32768;
38  
39      public static void update(List<Checksum> checksumList, Path file ) throws IOException {
40          long fileSize;
41          try (FileChannel channel = FileChannel.open(file, StandardOpenOption.READ )) {
42              fileSize = channel.size();
43              long pos = 0;
44              while (pos < fileSize) {
45                  long bufferSize = Math.min(BUFFER_SIZE, fileSize - pos);
46                  MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, pos, bufferSize);
47                  for (Checksum checksum : checksumList) {
48                      checksum.update(buffer);
49                      buffer.rewind();
50                  }
51                  fileSize = channel.size();
52                  pos += BUFFER_SIZE;
53              }
54              for (Checksum checksum : checksumList) {
55                  checksum.finish();
56              }
57          }
58      }
59  
60      public static void update(Checksum checksum, Path file)
61          throws IOException
62      {
63          long fileSize;
64          try (FileChannel channel = FileChannel.open(file, StandardOpenOption.READ )) {
65              fileSize = channel.size();
66              long pos = 0;
67              while (pos<fileSize)
68              {
69                  long bufferSize = Math.min(BUFFER_SIZE, fileSize-pos);
70                  MappedByteBuffer buffer = channel.map( FileChannel.MapMode.READ_ONLY, pos, bufferSize);
71                  checksum.update( buffer );
72                  buffer.rewind();
73                  fileSize = channel.size();
74                  pos += BUFFER_SIZE;
75              }
76              checksum.finish();
77          }
78      }
79  
80      public static List<Checksum> initializeChecksums(Path file, List<ChecksumAlgorithm> checksumAlgorithms) throws IOException {
81          final List<Checksum> checksums = newChecksums(checksumAlgorithms);
82          update(checksums, file);
83          return checksums;
84      }
85  
86      /**
87       * Returns the list of configured checksum types.
88       *
89       * @param checksumTypes The list of checksum strings
90       * @return The list of checksum objects
91       */
92      public static List<ChecksumAlgorithm> getAlgorithms(List<String> checksumTypes) {
93          return checksumTypes.stream().map(ca ->
94                  ChecksumAlgorithm.valueOf(ca.toUpperCase())).collect(Collectors.toList());
95      }
96  
97      public static List<Checksum> newChecksums(List<ChecksumAlgorithm> checksumAlgorithms) {
98          return checksumAlgorithms.stream().map( a -> new Checksum(a)).collect(Collectors.toList());
99      }
100 
101 }