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