2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package java.security;
20 import java.io.FilterOutputStream;
21 import java.io.IOException;
22 import java.io.OutputStream;
25 * {@code DigestOutputStream} is a {@code FilterOutputStream} which maintains an
26 * associated message digest.
28 public class DigestOutputStream extends FilterOutputStream {
31 * The message digest for this stream.
33 protected MessageDigest digest;
35 // Indicates whether digest functionality is on or off
36 private boolean isOn = true;
39 * Constructs a new instance of this {@code DigestOutputStream}, using the
40 * given {@code stream} and the {@code digest}.
47 public DigestOutputStream(OutputStream stream, MessageDigest digest) {
53 * Returns the message digest for this stream.
55 * @return the message digest for this stream.
57 public MessageDigest getMessageDigest() {
62 * Sets the message digest which this stream will use.
65 * the message digest which this stream will use.
67 public void setMessageDigest(MessageDigest digest) {
72 * Writes the specified {@code int} to the stream. Updates the digest if
73 * this function is {@link #on(boolean)}.
76 * the byte to be written.
78 * if writing to the stream causes a {@code IOException}
81 public void write(int b) throws IOException {
82 // update digest only if digest functionality is on
84 digest.update((byte)b);
91 * Writes {@code len} bytes into the stream, starting from the specified
92 * offset. Updates the digest if this function is {@link #on(boolean)}.
95 * the buffer to write to.
97 * the index of the first byte in {@code b} to write.
99 * the number of bytes in {@code b} to write.
100 * @throws IOException
101 * if writing to the stream causes an {@code IOException}.
104 public void write(byte[] b, int off, int len) throws IOException {
105 // update digest only if digest functionality is on
107 digest.update(b, off, len);
110 out.write(b, off, len);
114 * Enables or disables the digest function (default is on).
117 * {@code true} if the digest should be computed, {@code false}
121 public void on(boolean on) {
126 * Returns a string containing a concise, human-readable description of this
127 * {@code DigestOutputStream} including the digest.
129 * @return a printable representation for this {@code DigestOutputStream}.
132 public String toString() {
133 return super.toString() + ", " + digest.toString() +
134 (isOn ? ", is on" : ", is off");