src/java.xml.crypto/share/classes/org/jcp/xml/dsig/internal/DigesterOutputStream.java
author ccheung
Wed, 09 Oct 2019 16:42:18 -0700
changeset 58528 d0519b8bd8d9
parent 50614 3810c9a2efa1
permissions -rw-r--r--
8231529: [TESTBUG] runtime/cds/appcds/dynamicArchive/DynamicLotsOfClasses.java shouldn't wrap SkippedException into Exception Summary: Removed the try-catch in the test. Reviewed-by: iklam, lfoltan
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
 */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
     5
/**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
     7
 * or more contributor license agreements. See the NOTICE file
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
     8
 * distributed with this work for additional information
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
     9
 * regarding copyright ownership. The ASF licenses this file
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    10
 * to you under the Apache License, Version 2.0 (the
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    11
 * "License"); you may not use this file except in compliance
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    12
 * with the License. You may obtain a copy of the License at
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    14
 * http://www.apache.org/licenses/LICENSE-2.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    16
 * Unless required by applicable law or agreed to in writing,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    17
 * software distributed under the License is distributed on an
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    18
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    19
 * KIND, either express or implied. See the License for the
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    20
 * specific language governing permissions and limitations
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    21
 * under the License.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
/*
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    24
 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    25
 */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    26
/*
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    27
 * $Id: DigesterOutputStream.java, v 1.5 2005/12/20 20:02:39 mullan Exp $
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
package org.jcp.xml.dsig.internal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ByteArrayInputStream;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    32
import java.io.IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.MessageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.org.apache.xml.internal.security.utils.UnsyncByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * This class has been modified slightly to use java.security.MessageDigest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * objects as input, rather than
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    42
 * com.sun.org.apache.xml.internal.security.algorithms.MessageDigestAlgorithm objects.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * It also optionally caches the input bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class DigesterOutputStream extends OutputStream {
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    47
    private static final com.sun.org.slf4j.internal.Logger LOG =
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    48
        com.sun.org.slf4j.internal.LoggerFactory.getLogger(DigesterOutputStream.class);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    49
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    50
    private final boolean buffer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private UnsyncByteArrayOutputStream bos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private final MessageDigest md;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Creates a DigesterOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @param md the MessageDigest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public DigesterOutputStream(MessageDigest md) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this(md, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Creates a DigesterOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @param md the MessageDigest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param buffer if true, caches the input bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public DigesterOutputStream(MessageDigest md, boolean buffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.md = md;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this.buffer = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (buffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            bos = new UnsyncByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public void write(int input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (buffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            bos.write(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        md.update((byte)input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    84
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public void write(byte[] input, int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (buffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            bos.write(input, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    89
        if (LOG.isDebugEnabled()) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    90
            LOG.debug("Pre-digested input:");
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    91
            StringBuilder sb = new StringBuilder(len);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    92
            for (int i = offset; i < (offset + len); i++) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    93
                sb.append((char)input[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            }
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents: 47216
diff changeset
    95
            LOG.debug(sb.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        md.update(input, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @return the digest value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public byte[] getDigestValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
         return md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @return an input stream containing the cached bytes, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *    null if not cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public InputStream getInputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (buffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return new ByteArrayInputStream(bos.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   118
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   119
    @Override
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   120
    public void close() throws IOException {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   121
        if (buffer) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   122
            bos.close();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   123
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   124
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
}