1adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/*
2adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  Licensed to the Apache Software Foundation (ASF) under one or more
3adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  contributor license agreements.  See the NOTICE file distributed with
4adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  this work for additional information regarding copyright ownership.
5adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  The ASF licenses this file to You under the Apache License, Version 2.0
6adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  (the "License"); you may not use this file except in compliance with
7adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  the License.  You may obtain a copy of the License at
8adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *
9adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *     http://www.apache.org/licenses/LICENSE-2.0
10adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *
11adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  Unless required by applicable law or agreed to in writing, software
12adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  distributed under the License is distributed on an "AS IS" BASIS,
13adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  See the License for the specific language governing permissions and
15adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  limitations under the License.
16adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */
17adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
18adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectpackage java.security;
19adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
20adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.FilterOutputStream;
21adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.IOException;
22adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.OutputStream;
23adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
24adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/**
25adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code DigestOutputStream} is a {@code FilterOutputStream} which maintains an
26adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * associated message digest.
27adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */
28adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectpublic class DigestOutputStream extends FilterOutputStream {
29adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
30adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
31adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * The message digest for this stream.
32adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
33adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    protected MessageDigest digest;
34adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
35adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    // Indicates whether digest functionality is on or off
36adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    private boolean isOn = true;
37adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
38adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
39adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Constructs a new instance of this {@code DigestOutputStream}, using the
40adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * given {@code stream} and the {@code digest}.
412f9e468ed4985edfd5e351faf2089d91e561e41dElliott Hughes     *
42adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param stream
43adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the output stream.
44adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param digest
45adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the message digest.
46adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
47adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public DigestOutputStream(OutputStream stream, MessageDigest digest) {
48adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        super(stream);
49adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        this.digest = digest;
50adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
51adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
52adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
53adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns the message digest for this stream.
542f9e468ed4985edfd5e351faf2089d91e561e41dElliott Hughes     *
55adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the message digest for this stream.
56adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
57adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public MessageDigest getMessageDigest() {
58adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return digest;
59adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
60adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
61adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
62adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Sets the message digest which this stream will use.
632f9e468ed4985edfd5e351faf2089d91e561e41dElliott Hughes     *
64adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param digest
65adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the message digest which this stream will use.
66adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
67adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void setMessageDigest(MessageDigest digest) {
68adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        this.digest = digest;
69adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
70adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
71adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
72adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Writes the specified {@code int} to the stream. Updates the digest if
73adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * this function is {@link #on(boolean)}.
742f9e468ed4985edfd5e351faf2089d91e561e41dElliott Hughes     *
75adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param b
76adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the byte to be written.
77adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws IOException
78adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *             if writing to the stream causes a {@code IOException}
79adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
802f9e468ed4985edfd5e351faf2089d91e561e41dElliott Hughes    @Override
81adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void write(int b) throws IOException {
82adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        // update digest only if digest functionality is on
83adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (isOn) {
84adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            digest.update((byte)b);
85adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
86adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        // write the byte
87adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        out.write(b);
88adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
89adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
90adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
91adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Writes {@code len} bytes into the stream, starting from the specified
92adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * offset. Updates the digest if this function is {@link #on(boolean)}.
932f9e468ed4985edfd5e351faf2089d91e561e41dElliott Hughes     *
94adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param b
95adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the buffer to write to.
96adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param off
97adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the index of the first byte in {@code b} to write.
98adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param len
99adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the number of bytes in {@code b} to write.
100adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws IOException
101adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *             if writing to the stream causes an {@code IOException}.
102adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
1032f9e468ed4985edfd5e351faf2089d91e561e41dElliott Hughes    @Override
104adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void write(byte[] b, int off, int len) throws IOException {
105adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        // update digest only if digest functionality is on
106adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (isOn) {
107adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            digest.update(b, off, len);
108adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
109adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        // write len bytes
110adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        out.write(b, off, len);
111adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
112adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
113adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
114adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Enables or disables the digest function (default is on).
1152f9e468ed4985edfd5e351faf2089d91e561e41dElliott Hughes     *
116adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param on
117adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            {@code true} if the digest should be computed, {@code false}
118adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            otherwise.
119d21d78fd49a2d798218e8c8aefbddb26a0e71bbbElliott Hughes     * @see MessageDigest
120adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
121adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void on(boolean on) {
122adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        isOn = on;
123adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
124adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
125adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
126adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns a string containing a concise, human-readable description of this
127adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * {@code DigestOutputStream} including the digest.
1282f9e468ed4985edfd5e351faf2089d91e561e41dElliott Hughes     *
129adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return a printable representation for this {@code DigestOutputStream}.
130adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
1312f9e468ed4985edfd5e351faf2089d91e561e41dElliott Hughes    @Override
132adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public String toString() {
133f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes        return super.toString() + ", " + digest.toString() +
134f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes            (isOn ? ", is on" : ", is off");
135adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
136adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project}
137