GNU Classpath (0.95) | |
Frames | No Frames |
1: /* DigestInputStream.java --- An Input stream tied to a message digest 2: Copyright (C) 1999, 2003, 2004, 2005 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package java.security; 40: 41: import java.io.FilterInputStream; 42: import java.io.IOException; 43: import java.io.InputStream; 44: 45: /** 46: * DigestInputStream is a class that ties an InputStream with a 47: * MessageDigest. The Message Digest is used by the class to 48: * update it self as bytes are read from the InputStream. 49: * 50: * The updating to the digest depends on the on flag which is set 51: * to true by default to tell the class to update the data 52: * in the message digest. 53: * 54: * @version 0.0 55: * @author Mark Benvenuto (ivymccough@worldnet.att.net) 56: */ 57: public class DigestInputStream extends FilterInputStream 58: { 59: /** 60: * The message digest for the DigestInputStream 61: */ 62: protected MessageDigest digest; 63: 64: //Manages the on flag 65: private boolean state = true; 66: 67: /** 68: * Constructs a new DigestInputStream. 69: * It associates a MessageDigest with the stream to 70: * compute the stream as data is written. 71: * 72: * @param stream An InputStream to associate this stream with 73: * @param digest A MessageDigest to hash the stream with 74: */ 75: public DigestInputStream(InputStream stream, MessageDigest digest) 76: { 77: super(stream); 78: //this.in = stream; 79: this.digest = digest; 80: } 81: 82: /** 83: * Returns the MessageDigest associated with this DigestInputStream 84: * 85: * @return The MessageDigest used to hash this stream 86: */ 87: public MessageDigest getMessageDigest() 88: { 89: return digest; 90: } 91: 92: /** 93: * Sets the current MessageDigest to current parameter 94: * 95: * @param digest A MessageDigest to associate with this stream 96: */ 97: public void setMessageDigest(MessageDigest digest) 98: { 99: 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: }
GNU Classpath (0.95) |