This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.checksum;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.commons.io.IOUtils;
023import org.apache.commons.io.output.NullOutputStream;
024
025import java.io.IOException;
026import java.io.InputStream;
027import java.security.DigestInputStream;
028import java.security.MessageDigest;
029import java.security.NoSuchAlgorithmException;
030import java.util.List;
031
032/**
033 * Checksum - simple checksum hashing routines.
034 */
035public class Checksum
036{
037    private static final int BUFFER_SIZE = 32768;
038
039    public static void update( List<Checksum> checksums, InputStream stream )
040        throws IOException
041    {
042        byte[] buffer = new byte[BUFFER_SIZE];
043        int size = stream.read( buffer, 0, BUFFER_SIZE );
044        while ( size >= 0 )
045        {
046            for ( Checksum checksum : checksums )
047            {
048                checksum.update( buffer, 0, size );
049            }
050            size = stream.read( buffer, 0, BUFFER_SIZE );
051        }
052    }
053
054    private final MessageDigest md;
055
056    private ChecksumAlgorithm checksumAlgorithm;
057
058    public Checksum( ChecksumAlgorithm checksumAlgorithm )
059    {
060        this.checksumAlgorithm = checksumAlgorithm;
061        try
062        {
063            md = MessageDigest.getInstance( checksumAlgorithm.getAlgorithm() );
064        }
065        catch ( NoSuchAlgorithmException e )
066        {
067            // Not really possible, but here none-the-less
068            throw new IllegalStateException(
069                "Unable to initialize MessageDigest algorithm " + checksumAlgorithm.getAlgorithm() + " : "
070                    + e.getMessage(), e );
071        }
072    }
073
074    public String getChecksum()
075    {
076        return Hex.encode( md.digest() );
077    }
078
079    public ChecksumAlgorithm getAlgorithm()
080    {
081        return this.checksumAlgorithm;
082    }
083
084    public void reset()
085    {
086        md.reset();
087    }
088
089    public Checksum update( byte[] buffer, int offset, int size )
090    {
091        md.update( buffer, 0, size );
092        return this;
093    }
094
095    public Checksum update( InputStream stream )
096        throws IOException
097    {
098        try (DigestInputStream dig = new DigestInputStream( stream, md ))
099        {
100            IOUtils.copy( dig, new NullOutputStream() );
101        }
102        return this;
103    }
104}