001    /* DigestInputStream.java --- An Input stream tied to a message digest
002       Copyright (C) 1999, 2003, 2004, 2005  Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package java.security;
040    
041    import java.io.FilterInputStream;
042    import java.io.IOException;
043    import java.io.InputStream;
044    
045    /**
046     * DigestInputStream is a class that ties an InputStream with a
047     * MessageDigest. The Message Digest is used by the class to
048     * update it self as bytes are read from the InputStream.
049     *
050     * The updating to the digest depends on the on flag which is set
051     * to true by default to tell the class to update the data
052     * in the message digest.
053     *
054     * @version 0.0
055     * @author Mark Benvenuto (ivymccough@worldnet.att.net)
056     */
057    public class DigestInputStream extends FilterInputStream
058    {
059      /**
060       * The message digest for the DigestInputStream
061       */
062      protected MessageDigest digest;
063    
064      //Manages the on flag
065      private boolean state = true;
066    
067      /**
068       * Constructs a new DigestInputStream.
069       * It associates a MessageDigest with the stream to
070       * compute the stream as data is written.
071       *
072       * @param stream An InputStream to associate this stream with
073       * @param digest A MessageDigest to hash the stream with
074       */
075      public DigestInputStream(InputStream stream, MessageDigest digest)
076      {
077        super(stream);
078        //this.in = stream;
079        this.digest = digest;
080      }
081    
082      /**
083       * Returns the MessageDigest associated with this DigestInputStream
084       *
085       * @return The MessageDigest used to hash this stream
086       */
087      public MessageDigest getMessageDigest()
088      {
089        return digest;
090      }
091    
092      /**
093       * Sets the current MessageDigest to current parameter
094       *
095       * @param digest A MessageDigest to associate with this stream
096       */
097      public void setMessageDigest(MessageDigest digest)
098      {
099        this.digest = digest;
100      }
101    
102      /**
103       * Reads a byte from the input stream and updates the digest.
104       * This method reads the underlying input stream and if the
105       * on flag is true then updates the message digest.
106       *
107       * @return Returns a byte from the input stream, -1 is returned to indicate that
108       * the end of stream was reached before this read call
109       *
110       * @throws IOException if an IO error occurs in the underlying input stream,
111       * this error is thrown
112       */
113      public int read() throws IOException
114      {
115        int temp = in.read();
116    
117        if (state == true && temp != -1)
118          digest.update((byte) temp);
119    
120        return temp;
121      }
122    
123      /**
124       * Reads bytes from the input stream and updates the digest.
125       * This method reads the underlying input stream and if the
126       * on flag is true then updates the message digest.
127       *
128       * @param b a byte array to store the data from the input stream
129       * @param off an offset to start at in the array
130       * @param len length of data to read
131       * @return Returns count of bytes read, -1 is returned to indicate that
132       * the end of stream was reached before this read call
133       *
134       * @throws IOException if an IO error occurs in the underlying input stream,
135       * this error is thrown
136       */
137      public int read(byte[]b, int off, int len) throws IOException
138      {
139        int temp = in.read(b, off, len);
140    
141        if (state == true && temp != -1)
142          digest.update(b, off, temp);
143    
144        return temp;
145      }
146    
147      /**
148       * Sets the flag specifing if this DigestInputStream updates the
149       * digest in the write() methods. The default is on;
150       *
151       * @param on True means it digests stream, false means it does not
152       */
153      public void on(boolean on)
154      {
155        state = on;
156      }
157    
158      /**
159       * Converts the input stream and underlying message digest to a string.
160       *
161       * @return A string representing the input stream and message digest.
162       */
163      public String toString()
164      {
165        return "[Digest Input Stream] " + digest.toString();
166      }
167    }