1package org.apache.archiva.checksum;
23/*4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122import java.io.IOException;
23import java.io.InputStream;
24import java.io.OutputStream;
25import java.util.List;
26import java.util.stream.Collectors;
2728/**29 *30 * Class that handles checksums with streams.31 *32 * @author Martin Stockhammer <martin_s@apache.org>33 */34publicclassStreamingChecksum35 {
36staticfinalint BUFFER_SIZE=4096;
3738publicstaticvoid updateChecksums( InputStream input, List<ChecksumAlgorithm> algorithms, List<OutputStream> checksumOutput) {
39 List<Checksum> checksums = algorithms.stream().map(a -> newChecksum( a )).collect( Collectors.toList());
40 byte[] buffer = new byte[BUFFER_SIZE];
41int read;
42try43 {
44while ( ( read = input.read( buffer ) ) >= 0 )
45 {
46for (Checksum cs : checksums ) {
47 cs.update( buffer, 0, read );
48 }
49 }
50int minIndex = Math.min(algorithms.size(), checksums.size());
51for (int csIndex = 0; csIndex<minIndex; csIndex++) {
52Checksum cs = checksums.get(csIndex);
53 cs.finish();
54 OutputStream os =checksumOutput.get(csIndex);
55if (os!=null)
56 {
57 os.write( cs.getChecksum( ).getBytes( ) );
58 }
59 }
6061 } catch ( IOException e ) {
6263 }
64 }
65 }