001: /*
002: * reserved comment block
003: * DO NOT REMOVE OR ALTER!
004: */
005: /*
006: * Copyright 1999-2005 The Apache Software Foundation.
007: *
008: * Licensed under the Apache License, Version 2.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: */
021: /*
022: * $Id: DigesterOutputStream.java,v 1.1.2.2 2005/08/12 18:15:35 mullan Exp $
023: */
024: package org.jcp.xml.dsig.internal;
025:
026: import java.io.ByteArrayInputStream;
027: import java.io.InputStream;
028: import java.io.OutputStream;
029: import java.security.MessageDigest;
030: import java.util.logging.Logger;
031: import java.util.logging.Level;
032:
033: import com.sun.org.apache.xml.internal.security.utils.UnsyncByteArrayOutputStream;
034:
035: /**
036: * This class has been modified slightly to use java.security.MessageDigest
037: * objects as input, rather than
038: * org.apache.xml.security.algorithms.MessageDigestAlgorithm objects.
039: * It also optionally caches the input bytes.
040: *
041: * @author raul
042: */
043: public class DigesterOutputStream extends OutputStream {
044: private boolean buffer = false;
045: private UnsyncByteArrayOutputStream bos;
046: private final MessageDigest md;
047: private static Logger log = Logger
048: .getLogger("org.jcp.xml.dsig.internal");
049:
050: /**
051: * Creates a DigesterOutputStream.
052: *
053: * @param md the MessageDigest
054: */
055: public DigesterOutputStream(MessageDigest md) {
056: this (md, false);
057: }
058:
059: /**
060: * Creates a DigesterOutputStream.
061: *
062: * @param md the MessageDigest
063: * @param buffer if true, caches the input bytes
064: */
065: public DigesterOutputStream(MessageDigest md, boolean buffer) {
066: this .md = md;
067: this .buffer = buffer;
068: if (buffer) {
069: bos = new UnsyncByteArrayOutputStream();
070: }
071: }
072:
073: /** @inheritDoc */
074: public void write(byte[] input) {
075: write(input, 0, input.length);
076: }
077:
078: /** @inheritDoc */
079: public void write(int input) {
080: if (buffer) {
081: bos.write(input);
082: }
083: md.update((byte) input);
084: }
085:
086: /** @inheritDoc */
087: public void write(byte[] input, int offset, int len) {
088: if (buffer) {
089: bos.write(input, offset, len);
090: }
091: if (log.isLoggable(Level.FINER)) {
092: log.log(Level.FINER, "Pre-digested input:");
093: StringBuffer sb = new StringBuffer(len);
094: for (int i = offset; i < (offset + len); i++) {
095: sb.append((char) input[i]);
096: }
097: log.log(Level.FINER, sb.toString());
098: }
099: md.update(input, offset, len);
100: }
101:
102: /**
103: * @return the digest value
104: */
105: public byte[] getDigestValue() {
106: return md.digest();
107: }
108:
109: /**
110: * @return an input stream containing the cached bytes, or
111: * null if not cached
112: */
113: public InputStream getInputStream() {
114: if (buffer) {
115: return new ByteArrayInputStream(bos.toByteArray());
116: } else {
117: return null;
118: }
119: }
120: }
|