Source Code Cross Referenced for DigestInputStream.java in  » JDK-Core » security » java » security » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. JDK Core
2. JDK Modules
3. JDK Modules com.sun
4. JDK Modules com.sun.java
5. JDK Modules Platform
6. JDK Modules sun
7. Open Source Build
8. Open Source Graphic Library
9. Open Source IDE Eclipse
10. Open Source J2EE
11. Open Source JDBC Driver
12. Open Source Library
13. Open Source Library Database
14. Open Source Net
15. Open Source Script
16. Science
17. Security
18. Sevlet Container
19. SUN GlassFish
20. Swing Library
21. Web Services apache cxf 2.0.1
22. Web Services AXIS2
23. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java Source Code / Java Documentation » JDK Core » security » java.security 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 1996-1999 Sun Microsystems, Inc.  All Rights Reserved.
003:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004:         *
005:         * This code is free software; you can redistribute it and/or modify it
006:         * under the terms of the GNU General Public License version 2 only, as
007:         * published by the Free Software Foundation.  Sun designates this
008:         * particular file as subject to the "Classpath" exception as provided
009:         * by Sun in the LICENSE file that accompanied this code.
010:         *
011:         * This code is distributed in the hope that it will be useful, but WITHOUT
012:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013:         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014:         * version 2 for more details (a copy is included in the LICENSE file that
015:         * accompanied this code).
016:         *
017:         * You should have received a copy of the GNU General Public License version
018:         * 2 along with this work; if not, write to the Free Software Foundation,
019:         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020:         *
021:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022:         * CA 95054 USA or visit www.sun.com if you need additional information or
023:         * have any questions.
024:         */
025:
026:        package java.security;
027:
028:        import java.io.IOException;
029:        import java.io.EOFException;
030:        import java.io.InputStream;
031:        import java.io.FilterInputStream;
032:        import java.io.PrintStream;
033:        import java.io.ByteArrayInputStream;
034:
035:        /**
036:         * A transparent stream that updates the associated message digest using
037:         * the bits going through the stream.
038:         *
039:         * <p>To complete the message digest computation, call one of the
040:         * <code>digest</code> methods on the associated message
041:         * digest after your calls to one of this digest input stream's
042:         * {@link #read() read} methods.
043:         *
044:         * <p>It is possible to turn this stream on or off (see
045:         * {@link #on(boolean) on}). When it is on, a call to one of the
046:         * <code>read</code> methods
047:         * results in an update on the message digest.  But when it is off,
048:         * the message digest is not updated. The default is for the stream
049:         * to be on.
050:         *
051:         * <p>Note that digest objects can compute only one digest (see
052:         * {@link MessageDigest}),
053:         * so that in order to compute intermediate digests, a caller should
054:         * retain a handle onto the digest object, and clone it for each
055:         * digest to be computed, leaving the orginal digest untouched.
056:         *
057:         * @see MessageDigest
058:         *
059:         * @see DigestOutputStream
060:         *
061:         * @version 1.44 07/05/05
062:         * @author Benjamin Renaud
063:         */
064:
065:        public class DigestInputStream extends FilterInputStream {
066:
067:            /* NOTE: This should be made a generic UpdaterInputStream */
068:
069:            /* Are we on or off? */
070:            private boolean on = true;
071:
072:            /**
073:             * The message digest associated with this stream.
074:             */
075:            protected MessageDigest digest;
076:
077:            /**
078:             * Creates a digest input stream, using the specified input stream
079:             * and message digest.
080:             *
081:             * @param stream the input stream.
082:             *
083:             * @param digest the message digest to associate with this stream.
084:             */
085:            public DigestInputStream(InputStream stream, MessageDigest digest) {
086:                super (stream);
087:                setMessageDigest(digest);
088:            }
089:
090:            /**
091:             * Returns the message digest associated with this stream.
092:             *
093:             * @return the message digest associated with this stream.
094:             * @see #setMessageDigest(java.security.MessageDigest)
095:             */
096:            public MessageDigest getMessageDigest() {
097:                return digest;
098:            }
099:
100:            /**
101:             * Associates the specified message digest with this stream.
102:             *
103:             * @param digest the message digest to be associated with this stream.
104:             * @see #getMessageDigest()
105:             */
106:            public void setMessageDigest(MessageDigest digest) {
107:                this .digest = digest;
108:            }
109:
110:            /**
111:             * Reads a byte, and updates the message digest (if the digest
112:             * function is on).  That is, this method reads a byte from the
113:             * input stream, blocking until the byte is actually read. If the
114:             * digest function is on (see {@link #on(boolean) on}), this method
115:             * will then call <code>update</code> on the message digest associated
116:             * with this stream, passing it the byte read.
117:             *
118:             * @return the byte read.
119:             *
120:             * @exception IOException if an I/O error occurs.
121:             *
122:             * @see MessageDigest#update(byte)
123:             */
124:            public int read() throws IOException {
125:                int ch = in.read();
126:                if (on && ch != -1) {
127:                    digest.update((byte) ch);
128:                }
129:                return ch;
130:            }
131:
132:            /**
133:             * Reads into a byte array, and updates the message digest (if the
134:             * digest function is on).  That is, this method reads up to
135:             * <code>len</code> bytes from the input stream into the array
136:             * <code>b</code>, starting at offset <code>off</code>. This method
137:             * blocks until the data is actually
138:             * read. If the digest function is on (see
139:             * {@link #on(boolean) on}), this method will then call <code>update</code>
140:             * on the message digest associated with this stream, passing it
141:             * the data.
142:             *
143:             * @param b	the array into which the data is read.
144:             *
145:             * @param off the starting offset into <code>b</code> of where the
146:             * data should be placed.
147:             *
148:             * @param len the maximum number of bytes to be read from the input
149:             * stream into b, starting at offset <code>off</code>.
150:             *
151:             * @return  the actual number of bytes read. This is less than
152:             * <code>len</code> if the end of the stream is reached prior to
153:             * reading <code>len</code> bytes. -1 is returned if no bytes were
154:             * read because the end of the stream had already been reached when
155:             * the call was made.
156:             *
157:             * @exception IOException if an I/O error occurs.
158:             *
159:             * @see MessageDigest#update(byte[], int, int)
160:             */
161:            public int read(byte[] b, int off, int len) throws IOException {
162:                int result = in.read(b, off, len);
163:                if (on && result != -1) {
164:                    digest.update(b, off, result);
165:                }
166:                return result;
167:            }
168:
169:            /**
170:             * Turns the digest function on or off. The default is on.  When
171:             * it is on, a call to one of the <code>read</code> methods results in an
172:             * update on the message digest.  But when it is off, the message
173:             * digest is not updated.
174:             *
175:             * @param on true to turn the digest function on, false to turn
176:             * it off.
177:             */
178:            public void on(boolean on) {
179:                this .on = on;
180:            }
181:
182:            /**
183:             * Prints a string representation of this digest input stream and
184:             * its associated message digest object.
185:             */
186:            public String toString() {
187:                return "[Digest Input Stream] " + digest.toString();
188:            }
189:        }
ww_w.ja___v_a2___s.__c_o__m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.