1 /* DigestInputStream.java --- An Input stream tied to a message digest
2 Copyright (C) 1999 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)
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
21 As a special exception, if you link this library with other files to
22 produce an executable, this library does not by itself cause the
23 resulting executable to be covered by the GNU General Public License.
24 This exception does not however invalidate any other reasons why the
25 executable file might be covered by the GNU General Public License. */
27 package java
.security
;
28 //import java.security.MessageDigest;
29 import java
.io
.InputStream
;
30 import java
.io
.FilterInputStream
;
31 import java
.io
.IOException
;
34 DigestInputStream is a class that ties an InputStream with a
35 MessageDigest. The Message Digest is used by the class to
36 update it self as bytes are read from the InputStream.
38 The updating to the digest depends on the on flag which is set
39 to true by default to tell the class to update the data
40 in the message digest.
43 @author Mark Benvenuto <ivymccough@worldnet.att.net>
45 public class DigestInputStream
extends FilterInputStream
48 The message digest for the DigestInputStream
50 protected MessageDigest digest
;
53 private boolean state
= true;
56 Constructs a new DigestInputStream.
57 It associates a MessageDigest with the stream to
58 compute the stream as data is written.
60 @param stream An InputStream to associate this stream with
61 @param digest A MessageDigest to hash the stream with
63 public DigestInputStream(InputStream stream
, MessageDigest digest
)
71 Returns the MessageDigest associated with this DigestInputStream
73 @return The MessageDigest used to hash this stream
75 public MessageDigest
getMessageDigest()
81 Sets the current MessageDigest to current parameter
83 @param digest A MessageDigest to associate with this stream
85 public void setMessageDigest(MessageDigest digest
)
91 Reads a byte from the input stream and updates the digest.
92 This method reads the underlying input stream and if the
93 on flag is true then updates the message digest.
95 @return Returns a byte from the input stream, -1 is returned to indicate that
96 the end of stream was reached before this read call
98 @throws IOException if an IO error occurs in the underlying input stream,
101 public int read() throws IOException
103 int temp
= in
.read();
105 if (state
== true && temp
!= -1)
106 digest
.update((byte) temp
);
112 Reads bytes from the input stream and updates the digest.
113 This method reads the underlying input stream and if the
114 on flag is true then updates the message digest.
116 @param b a byte array to store the data from the input stream
117 @param off an offset to start at in the array
118 @param len length of data to read
119 @return Returns count of bytes read, -1 is returned to indicate that
120 the end of stream was reached before this read call
122 @throws IOException if an IO error occurs in the underlying input stream,
125 public int read(byte[]b
, int off
, int len
) throws IOException
127 int temp
= in
.read(b
, off
, len
);
129 if (state
== true && temp
!= -1)
130 digest
.update(b
, off
, len
);
136 Sets the flag specifing if this DigestInputStream updates the
137 digest in the write() methods. The default is on;
139 @param on True means it digests stream, false means it does not
141 public void on(boolean on
)
147 Converts the input stream and underlying message digest to a string.
149 @return A string representing the input stream and message digest.
151 public String
toString()
153 return "[Digest Input Stream] " + digest
.toString();