Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / security / DigestInputStream.java
blobe8d2b63fdfcd5f97f9215f06b628dc90a64e7f2e
1 /* DigestInputStream.java --- An Input stream tied to a message digest
2 Copyright (C) 1999, 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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.
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. */
39 package java.security;
41 import java.io.FilterInputStream;
42 import java.io.IOException;
43 import java.io.InputStream;
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.
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.
54 * @version 0.0
55 * @author Mark Benvenuto (ivymccough@worldnet.att.net)
57 public class DigestInputStream extends FilterInputStream
59 /**
60 * The message digest for the DigestInputStream
62 protected MessageDigest digest;
64 //Manages the on flag
65 private boolean state = true;
67 /**
68 * Constructs a new DigestInputStream.
69 * It associates a MessageDigest with the stream to
70 * compute the stream as data is written.
72 * @param stream An InputStream to associate this stream with
73 * @param digest A MessageDigest to hash the stream with
75 public DigestInputStream(InputStream stream, MessageDigest digest)
77 super(stream);
78 //this.in = stream;
79 this.digest = digest;
82 /**
83 * Returns the MessageDigest associated with this DigestInputStream
85 * @return The MessageDigest used to hash this stream
87 public MessageDigest getMessageDigest()
89 return digest;
92 /**
93 * Sets the current MessageDigest to current parameter
95 * @param digest A MessageDigest to associate with this stream
97 public void setMessageDigest(MessageDigest digest)
99 this.digest = digest;
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.
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
110 * @throws IOException if an IO error occurs in the underlying input stream,
111 * this error is thrown
113 public int read() throws IOException
115 int temp = in.read();
117 if (state == true && temp != -1)
118 digest.update((byte) temp);
120 return temp;
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.
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
134 * @throws IOException if an IO error occurs in the underlying input stream,
135 * this error is thrown
137 public int read(byte[]b, int off, int len) throws IOException
139 int temp = in.read(b, off, len);
141 if (state == true && temp != -1)
142 digest.update(b, off, temp);
144 return temp;
148 * Sets the flag specifing if this DigestInputStream updates the
149 * digest in the write() methods. The default is on;
151 * @param on True means it digests stream, false means it does not
153 public void on(boolean on)
155 state = on;
159 * Converts the input stream and underlying message digest to a string.
161 * @return A string representing the input stream and message digest.
163 public String toString()
165 return "[Digest Input Stream] " + digest.toString();