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

  1. /*
  2.  * @(#)DigestOutputStream.java    1.19 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.OutputStream;
  28. import java.io.FilterOutputStream;
  29. import java.io.PrintStream;
  30. import java.io.ByteArrayOutputStream;
  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 ouput stream's <a href = 
  39.  * "#write(int)">write</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>write</code> results in
  43.  * an update on the message digest.  But when it is off, the message 
  44.  * digest is not updated. The default is for the stream to be on.
  45.  *
  46.  * @see MessageDigest
  47.  * @see DigestInputStream
  48.  *
  49.  * @version 1.19 97/11/25
  50.  * @author Benjamin Renaud */
  51.  
  52. public class DigestOutputStream extends FilterOutputStream {
  53.   
  54.     private boolean on = true;
  55.  
  56.     /**  
  57.      * The message digest associated with this stream.  
  58.      */  
  59.     protected MessageDigest digest;
  60.  
  61.     /**
  62.      * Creates a digest output stream, using the specified output stream
  63.      * and message digest.
  64.      *
  65.      * @param stream the output stream.
  66.      *
  67.      * @param digest the message digest to associate with this stream.
  68.      */
  69.     public DigestOutputStream(OutputStream stream, MessageDigest digest) {
  70.     super(stream);
  71.     setMessageDigest(digest);
  72.     }
  73.  
  74.     /**
  75.      * Returns the message digest associated with this stream.
  76.      *
  77.      * @return the message digest associated with this stream.
  78.      */
  79.     public MessageDigest getMessageDigest() {
  80.     return digest;
  81.     }    
  82.  
  83.     /**
  84.      * Associates the specified message digest with this stream.
  85.      *
  86.      * @param digest the message digest to be associated with this stream.  
  87.      */
  88.     public void setMessageDigest(MessageDigest digest) {
  89.     this.digest = digest;
  90.     }
  91.  
  92.     /**
  93.      * Updates the message digest (if the digest function is on) using   
  94.      * the specified byte, and in any case writes the byte      
  95.      * to the output stream. That is, if the digest function is on
  96.      * (see <a href = "#on">on</a>), this method calls
  97.      * <code>update</code> on the message digest associated with this
  98.      * stream, passing it the byte <code>b</code>. This method then
  99.      * writes the byte to the output stream, blocking until the byte 
  100.      * is actually written.
  101.      *
  102.      * @param b the byte to be used for updating and writing to the    
  103.      * output stream.    
  104.      *
  105.      * @exception IOException if an I/O error occurs.
  106.      * 
  107.      * @see MessageDigest#update(byte) 
  108.      */
  109.     public void write(int b) throws IOException {
  110.     if (on) {
  111.         digest.update((byte)b);
  112.     }
  113.     out.write(b);
  114.     }
  115.  
  116.     /**
  117.      * Updates the message digest (if the digest function is on) using
  118.      * the specified subarray, and in any case writes the subarray to
  119.      * the output stream. That is, if the digest function is on (see
  120.      * <a href = "#on">on</a>), this method calls <code>update</code>
  121.      * on the message digest associated with this stream, passing it
  122.      * the subarray specifications. This method then writes the subarray 
  123.      * bytes to the output stream, blocking until the bytes are actually
  124.      * written.
  125.      *
  126.      * @param b the array containing the subarray to be used for updating 
  127.      * and writing to the output stream.
  128.      *
  129.      * @param off the offset into <code>b</code> of the first byte to 
  130.      * be updated and written.
  131.      *
  132.      * @param len the number of bytes of data to be updated and written 
  133.      * from <code>b</code>, starting at offset <code>off</code>.
  134.      *
  135.      * @exception IOException if an I/O error occurs.
  136.      * 
  137.      * @see MessageDigest#update(byte[], int, int)
  138.      */
  139.     public void write(byte[] b, int off, int len) throws IOException {
  140.     if (on) {
  141.         digest.update(b, off, len);
  142.     }
  143.     out.write(b, off, len);
  144.     }
  145.  
  146.     /**
  147.      * Turns the digest function on or off. The default is on.  When
  148.      * it is on, a call to <a href = "#write">write</a> results in an
  149.      * update on the message digest.  But when it is off, the message
  150.      * digest is not updated.
  151.      *    
  152.      * @param on true to turn the digest function on, false to turn it
  153.      * off. 
  154.      */
  155.     public void on(boolean on) {
  156.     this.on = on;
  157.     }
  158.     
  159.     /**
  160.      * Prints a string representation of this digest output stream and
  161.      * its associated message digest object.  
  162.      */
  163.      public String toString() {
  164.      return "[Digest Output Stream] " + digest.toString();
  165.      }
  166. }    
  167.  
  168.  
  169.   
  170.  
  171.