This project has retired. For details please refer to its
Attic page.
Checksum 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 org.apache.commons.io.IOUtils;
23 import org.apache.commons.io.output.NullOutputStream;
24
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.security.DigestInputStream;
28 import java.security.MessageDigest;
29 import java.security.NoSuchAlgorithmException;
30 import java.util.List;
31
32
33
34
35 public class Checksum
36 {
37 private static final int BUFFER_SIZE = 32768;
38
39 public static void update( List<Checksum> checksums, InputStream stream )
40 throws IOException
41 {
42 byte[] buffer = new byte[BUFFER_SIZE];
43 int size = stream.read( buffer, 0, BUFFER_SIZE );
44 while ( size >= 0 )
45 {
46 for ( Checksum checksum : checksums )
47 {
48 checksum.update( buffer, 0, size );
49 }
50 size = stream.read( buffer, 0, BUFFER_SIZE );
51 }
52 }
53
54 private final MessageDigest md;
55
56 private ChecksumAlgorithm checksumAlgorithm;
57
58 public Checksum( ChecksumAlgorithm checksumAlgorithm )
59 {
60 this.checksumAlgorithm = checksumAlgorithm;
61 try
62 {
63 md = MessageDigest.getInstance( checksumAlgorithm.getAlgorithm() );
64 }
65 catch ( NoSuchAlgorithmException e )
66 {
67
68 throw new IllegalStateException(
69 "Unable to initialize MessageDigest algorithm " + checksumAlgorithm.getAlgorithm() + " : "
70 + e.getMessage(), e );
71 }
72 }
73
74 public String getChecksum()
75 {
76 return Hex.encode( md.digest() );
77 }
78
79 public ChecksumAlgorithm getAlgorithm()
80 {
81 return this.checksumAlgorithm;
82 }
83
84 public void reset()
85 {
86 md.reset();
87 }
88
89 public Checksum update( byte[] buffer, int offset, int size )
90 {
91 md.update( buffer, 0, size );
92 return this;
93 }
94
95 public Checksum update( InputStream stream )
96 throws IOException
97 {
98 try (DigestInputStream dig = new DigestInputStream( stream, md ))
99 {
100 IOUtils.copy( dig, new NullOutputStream() );
101 }
102 return this;
103 }
104 }