This project has retired. For details please refer to its
Attic page.
ChecksumUtil xref
1 package org.apache.archiva.checksum;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
88
89
90
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 }