View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /**
6    * Licensed to the Apache Software Foundation (ASF) under one
7    * or more contributor license agreements. See the NOTICE file
8    * distributed with this work for additional information
9    * regarding copyright ownership. The ASF licenses this file
10   * to you under the Apache License, Version 2.0 (the
11   * "License"); you may not use this file except in compliance
12   * with the License. You may obtain a copy of the License at
13   *
14   * http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing,
17   * software distributed under the License is distributed on an
18   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19   * KIND, either express or implied. See the License for the
20   * specific language governing permissions and limitations
21   * under the License.
22   */
23  package com.sun.org.apache.xml.internal.security.utils;
24  
25  import java.io.ByteArrayOutputStream;
26  
27  import com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithm;
28  import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException;
29  
30  /**
31   * @author raul
32   *
33   */
34  public class SignerOutputStream extends ByteArrayOutputStream {
35      private static java.util.logging.Logger log =
36          java.util.logging.Logger.getLogger(SignerOutputStream.class.getName());
37  
38      final SignatureAlgorithm sa;
39  
40      /**
41       * @param sa
42       */
43      public SignerOutputStream(SignatureAlgorithm sa) {
44          this.sa = sa;
45      }
46  
47      /** @inheritDoc */
48      public void write(byte[] arg0)  {
49          try {
50              sa.update(arg0);
51          } catch (XMLSignatureException e) {
52              throw new RuntimeException("" + e);
53          }
54      }
55  
56      /** @inheritDoc */
57      public void write(int arg0) {
58          try {
59              sa.update((byte)arg0);
60          } catch (XMLSignatureException e) {
61              throw new RuntimeException("" + e);
62          }
63      }
64  
65      /** @inheritDoc */
66      public void write(byte[] arg0, int arg1, int arg2) {
67          if (log.isLoggable(java.util.logging.Level.FINE)) {
68              log.log(java.util.logging.Level.FINE, "Canonicalized SignedInfo:");
69              StringBuilder sb = new StringBuilder(arg2);
70              for (int i = arg1; i < (arg1 + arg2); i++) {
71                  sb.append((char)arg0[i]);
72              }
73              log.log(java.util.logging.Level.FINE, sb.toString());
74          }
75          try {
76              sa.update(arg0, arg1, arg2);
77          } catch (XMLSignatureException e) {
78              throw new RuntimeException("" + e);
79          }
80      }
81  }