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.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.util.List;
26 import java.util.stream.Collectors;
27
28
29
30
31
32
33
34 public class StreamingChecksum
35 {
36 static final int BUFFER_SIZE=4096;
37
38 public static void updateChecksums( InputStream input, List<ChecksumAlgorithm> algorithms, List<OutputStream> checksumOutput) {
39 List<Checksum> checksums = algorithms.stream().map(a -> new Checksum( a )).collect( Collectors.toList());
40 byte[] buffer = new byte[BUFFER_SIZE];
41 int read;
42 try
43 {
44 while ( ( read = input.read( buffer ) ) >= 0 )
45 {
46 for (Checksum cs : checksums ) {
47 cs.update( buffer, 0, read );
48 }
49 }
50 int minIndex = Math.min(algorithms.size(), checksums.size());
51 for (int csIndex = 0; csIndex<minIndex; csIndex++) {
52 Checksum cs = checksums.get(csIndex);
53 cs.finish();
54 OutputStream os =checksumOutput.get(csIndex);
55 if (os!=null)
56 {
57 os.write( cs.getChecksum( ).getBytes( ) );
58 }
59 }
60
61 } catch ( IOException e ) {
62
63 }
64 }
65 }