jdk/src/share/classes/org/jcp/xml/dsig/internal/DigesterOutputStream.java
author mullan
Mon, 22 Sep 2008 10:43:17 -0400
changeset 1337 e8d6cef36199
parent 2 90ce3da70b43
child 1639 a97859015238
permissions -rw-r--r--
6469266: Integrate Apache XMLSec 1.4.2 into JDK 7 Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * Copyright 1999-2005 The Apache Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *  Licensed under the Apache License, Version 2.0 (the "License");
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *  you may not use this file except in compliance with the License.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *  You may obtain a copy of the License at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *      http://www.apache.org/licenses/LICENSE-2.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *  Unless required by applicable law or agreed to in writing, software
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *  distributed under the License is distributed on an "AS IS" BASIS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *  See the License for the specific language governing permissions and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *  limitations under the License.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
/*
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    22
 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    23
 */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    24
/*
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    25
 * $Id: DigesterOutputStream.java,v 1.2 2008/07/24 15:20:31 mullan Exp $
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package org.jcp.xml.dsig.internal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.MessageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.logging.Logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.org.apache.xml.internal.security.utils.UnsyncByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class has been modified slightly to use java.security.MessageDigest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * objects as input, rather than
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    41
 * com.sun.org.apache.xml.internal.security.algorithms.MessageDigestAlgorithm objects.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * It also optionally caches the input bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author raul
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    45
 * @author Sean Mullan
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class DigesterOutputStream extends OutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private boolean buffer = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private UnsyncByteArrayOutputStream bos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private final MessageDigest md;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static Logger log = Logger.getLogger("org.jcp.xml.dsig.internal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Creates a DigesterOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @param md the MessageDigest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public DigesterOutputStream(MessageDigest md) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this(md, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Creates a DigesterOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @param md the MessageDigest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @param buffer if true, caches the input bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public DigesterOutputStream(MessageDigest md, boolean buffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.md = md;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.buffer = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (buffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            bos = new UnsyncByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /** @inheritDoc */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public void write(byte[] input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        write(input, 0, input.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /** @inheritDoc */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public void write(int input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (buffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            bos.write(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        md.update((byte)input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /** @inheritDoc */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public void write(byte[] input, int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (buffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            bos.write(input, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (log.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            log.log(Level.FINER, "Pre-digested input:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            StringBuffer sb = new StringBuffer(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            for (int i=offset; i<(offset+len); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                sb.append((char) input[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            log.log(Level.FINER, sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        md.update(input, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @return the digest value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public byte[] getDigestValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
         return md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @return an input stream containing the cached bytes, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *    null if not cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public InputStream getInputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (buffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return new ByteArrayInputStream(bos.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
}