home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / DigestInputStream.java < prev    next >
Text File  |  1998-01-23  |  6KB  |  188 lines

  1. /*
  2.  * @(#)DigestInputStream.java    1.25 97/01/30
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.security;
  24.  
  25. import java.io.IOException;
  26. import java.io.EOFException;
  27. import java.io.InputStream;
  28. import java.io.FilterInputStream;
  29. import java.io.PrintStream;
  30. import java.io.ByteArrayInputStream;
  31.  
  32. /** 
  33.  * A transparent stream that updates the associated message digest using 
  34.  * the bits going through the stream. 
  35.  *
  36.  * <p>To complete the message digest computation, call one of the 
  37.  * <code>digest</code> methods on the associated message 
  38.  * digest after your calls to one of this digest input stream's <a href = 
  39.  * "#read()">read</a> methods.
  40.  *      
  41.  * <p>It is possible to turn this stream on or off (see <a href =
  42.  * "#on">on</a>). When it is on, a call to <code>read</code>
  43.  * results in an update on the message digest.  But when it is off,
  44.  * the message digest is not updated. The default is for the stream
  45.  * to be on.
  46.  *
  47.  * <p>Note that digest objects can compute only one digest (see <a
  48.  * href="MessageDigest">MessageDigest</a>),
  49.  * so that in order to compute intermediate digests, a caller should
  50.  * retain a handle onto the digest object, and clone it for each
  51.  * digest to be computed, leaving the orginal digest untouched.
  52.  *
  53.  * @see MessageDigest
  54.  * 
  55.  * @see DigestOutputStream
  56.  *
  57.  * @version 1.25 97/11/25
  58.  * @author Benjamin Renaud 
  59.  */
  60.  
  61. public class DigestInputStream extends FilterInputStream {
  62.  
  63.     /* NOTE: This should be made a generic UpdaterInputStream */
  64.  
  65.     /* Are we on or off? */
  66.     private boolean on = true;
  67.  
  68.     /**
  69.      * The message digest associated with this stream.
  70.      */
  71.     protected MessageDigest digest;
  72.  
  73.     /**
  74.      * Creates a digest input stream, using the specified input stream
  75.      * and message digest.
  76.      *
  77.      * @param stream the input stream.
  78.      *
  79.      * @param digest the message digest to associate with this stream.
  80.      */
  81.     public DigestInputStream(InputStream stream, MessageDigest digest) {
  82.     super(stream);
  83.     setMessageDigest(digest);
  84.     }
  85.  
  86.     /**
  87.      * Returns the message digest associated with this stream.
  88.      *
  89.      * @return the message digest associated with this stream.
  90.      */
  91.     public MessageDigest getMessageDigest() {
  92.     return digest;
  93.     }    
  94.  
  95.     /**
  96.      * Associates the specified message digest with this stream.
  97.      *
  98.      * @param digest the message digest to be associated with this stream.  
  99.      */
  100.     public void setMessageDigest(MessageDigest digest) {
  101.     this.digest = digest;
  102.     }
  103.  
  104.     /**
  105.      * Reads a byte, and updates the message digest (if the digest
  106.      * function is on).  That is, this method reads a byte from the
  107.      * input stream, blocking until the byte is actually read. If the 
  108.      * digest function is on (see <a href = "#on">on</a>), this method 
  109.      * will then call <code>update</code> on the message digest associated
  110.      * with this stream, passing it the byte read. 
  111.      *
  112.      * @return the byte read.
  113.      *
  114.      * @exception IOException if an I/O error occurs.
  115.      * 
  116.      * @see MessageDigest#update(byte) 
  117.      */
  118.     public int read() throws IOException {
  119.     int ch = in.read();
  120.     if (on && ch != -1) {
  121.         digest.update((byte)ch);
  122.     }
  123.     return ch;
  124.     }
  125.  
  126.     /**
  127.      * Reads into a byte array, and updates the message digest (if the
  128.      * digest function is on).  That is, this method reads up to
  129.      * <code>len</code> bytes from the input stream into the array 
  130.      * <code>b</code>, starting at offset <code>off</code>. This method 
  131.      * blocks until the data is actually
  132.      * read. If the digest function is on (see <a href =
  133.      * "#on">on</a>), this method will then call <code>update</code>
  134.      * on the message digest associated with this stream, passing it
  135.      * the data.
  136.      *
  137.      * @param b    the array into which the data is read.
  138.      *
  139.      * @param off the starting offset into <code>b</code> of where the 
  140.      * data should be placed.
  141.      *
  142.      * @param len the maximum number of bytes to be read from the input
  143.      * stream into b, starting at offset <code>off</code>.
  144.      *
  145.      * @return  the actual number of bytes read. This is less than 
  146.      * <code>len</code> if the end of the stream is reached prior to 
  147.      * reading <code>len</code> bytes. -1 is returned if no bytes were 
  148.      * read because the end of the stream had already been reached when 
  149.      * the call was made.
  150.      *
  151.      * @exception IOException if an I/O error occurs.
  152.      * 
  153.      * @see MessageDigest#update(byte[], int, int) 
  154.      */
  155.     public int read(byte[] b, int off, int len) throws IOException {
  156.     int result = in.read(b, off, len);
  157.     if (on && result != -1) {
  158.         digest.update(b, off, result);
  159.     }
  160.     return result;
  161.     }
  162.  
  163.     /**
  164.      * Turns the digest function on or off. The default is on.  When
  165.      * it is on, a call to <a href = "#read">read</a> results in an
  166.      * update on the message digest.  But when it is off, the message
  167.      * digest is not updated.
  168.      *   
  169.      * @param on true to turn the digest function on, false to turn
  170.      * it off.
  171.      */
  172.     public void on(boolean on) {
  173.     this.on = on;
  174.     }
  175.     
  176.     /**
  177.      * Prints a string representation of this digest input stream and
  178.      * its associated message digest object.  
  179.      */
  180.      public String toString() {
  181.      return "[Digest Input Stream] " + digest.toString();
  182.      }
  183. }    
  184.  
  185.  
  186.   
  187.  
  188.