001    /*
002     * This file is part of muCommander, http://www.mucommander.com
003     * Copyright (C) 2002-2008 Maxence Bernard
004     *
005     * muCommander is free software; you can redistribute it and/or modify
006     * it under the terms of the GNU General Public License as published by
007     * the Free Software Foundation; either version 3 of the License, or
008     * (at your option) any later version.
009     *
010     * muCommander is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    
019    package com.mucommander.io;
020    
021    import java.io.OutputStream;
022    import java.security.DigestOutputStream;
023    import java.security.MessageDigest;
024    
025    /**
026     * This class extends <code>java.security.DigestOutputStream</code> and adds convenience methods that return the
027     * digest/checksum expressed in various forms.
028     *
029     * @see com.mucommander.io.ChecksumInputStream
030     * @author Maxence Bernard
031     */
032    public class ChecksumOutputStream extends DigestOutputStream {
033    
034        public ChecksumOutputStream(OutputStream stream, MessageDigest digest) {
035            super(stream, digest);
036        }
037    
038    
039        /**
040         * Returns this stream's digest, expressed as a byte array.
041         *
042         * @return this stream's digest, expressed as a byte array
043         */
044        public byte[] getChecksumBytes() {
045            return getMessageDigest().digest();
046        }
047    
048        /**
049         * Returns this stream's digest, expressed as an hexadecimal string.
050         *
051         * @return this stream's digest, expressed as an hexadecimal string
052         */
053        public String getChecksumString() {
054            return ByteUtils.toHexString(getChecksumBytes());
055        }
056    }