001package com.hfg.util.io;
002
003import java.io.FilterInputStream;
004import java.io.IOException;
005import java.io.InputStream;
006import java.security.MessageDigest;
007import java.security.NoSuchAlgorithmException;
008
009
010//------------------------------------------------------------------------------
011/**
012 An InputStream variant for calculating the MD5 checksum for the bytes that pass through.
013 <div>
014 @author J. Alex Taylor, hairyfatguy.com
015 </div>
016 */
017//------------------------------------------------------------------------------
018// com.hfg XML/HTML Coding Library
019//
020// This library is free software; you can redistribute it and/or
021// modify it under the terms of the GNU Lesser General Public
022// License as published by the Free Software Foundation; either
023// version 2.1 of the License, or (at your option) any later version.
024//
025// This library is distributed in the hope that it will be useful,
026// but WITHOUT ANY WARRANTY; without even the implied warranty of
027// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
028// Lesser General Public License for more details.
029//
030// You should have received a copy of the GNU Lesser General Public
031// License along with this library; if not, write to the Free Software
032// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
033//
034// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
035// jataylor@hairyfatguy.com
036//------------------------------------------------------------------------------
037
038public class MD5InputStream extends FilterInputStream
039{
040   private MessageDigest mChecksum;
041
042   //##########################################################################
043   // CONSTRUCTORS
044   //##########################################################################
045
046   //--------------------------------------------------------------------------
047   /**
048    An InputStream that measures the number of bytes that pass through.
049    */
050   public MD5InputStream(InputStream in)
051   {
052      super(in);
053
054      try
055      {
056         mChecksum = MessageDigest.getInstance("MD5");
057      }
058      catch (NoSuchAlgorithmException e)
059      {
060         throw new RuntimeException(e);
061      }
062   }
063
064
065   //##########################################################################
066   // PUBLIC METHODS
067   //##########################################################################
068
069   //--------------------------------------------------------------------------
070   @Override
071   public int read()
072         throws IOException
073   {
074      int theByte = super.read();
075      if (theByte > 0)
076      {
077         mChecksum.update((byte) theByte);
078      }
079
080      return theByte;
081   }
082
083   //--------------------------------------------------------------------------
084   @Override
085   public int read(byte[] inBytes, int inOffset, int inLength)
086         throws IOException
087   {
088      int numBytesRead = super.read(inBytes, inOffset, inLength);
089      if (numBytesRead > 0)
090      {
091         mChecksum.update(inBytes, inOffset, numBytesRead);
092      }
093
094      return numBytesRead;
095   }
096
097   //--------------------------------------------------------------------------
098   public void reset()
099   {
100      mChecksum.reset();
101   }
102
103   //--------------------------------------------------------------------------
104   public byte[] getChecksum()
105   {
106      return mChecksum.digest();
107   }
108}