OSDN Git Service

2006-08-14 Mark Wielaard <mark@klomp.org>
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / javax / crypto / assembly / CascadeTransformer.java
1 /* CascadeTransformer.java -- 
2    Copyright (C) 2003, 2006 Free Software Foundation, Inc.
3
4 This file is a part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at
9 your option) any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 USA
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version.  */
37
38
39 package gnu.javax.crypto.assembly;
40
41 import java.security.InvalidKeyException;
42 import java.util.Map;
43
44 /**
45  * An Adapter to use any {@link Cascade} as a {@link Transformer} in an
46  * {@link Assembly}.
47  */
48 class CascadeTransformer
49     extends Transformer
50 {
51   private Cascade delegate;
52
53   private int blockSize;
54
55   CascadeTransformer(Cascade delegate)
56   {
57     super();
58
59     this.delegate = delegate;
60   }
61
62   void initDelegate(Map attributes) throws TransformerException
63   {
64     attributes.put(Cascade.DIRECTION, wired);
65     try
66       {
67         delegate.init(attributes);
68       }
69     catch (InvalidKeyException x)
70       {
71         throw new TransformerException("initDelegate()", x);
72       }
73     blockSize = delegate.currentBlockSize();
74   }
75
76   int delegateBlockSize()
77   {
78     return blockSize;
79   }
80
81   void resetDelegate()
82   {
83     delegate.reset();
84     blockSize = 0;
85   }
86
87   byte[] updateDelegate(byte[] in, int offset, int length)
88       throws TransformerException
89   {
90     byte[] result = updateInternal(in, offset, length);
91     return result;
92   }
93
94   byte[] lastUpdateDelegate() throws TransformerException
95   {
96     if (inBuffer.size() != 0)
97       {
98         IllegalStateException cause = new IllegalStateException(
99             "Cascade transformer, after last update, must be empty but isn't");
100         throw new TransformerException("lastUpdateDelegate()", cause);
101       }
102     return new byte[0];
103   }
104
105   private byte[] updateInternal(byte[] in, int offset, int length)
106   {
107     byte[] result;
108     for (int i = 0; i < length; i++)
109       {
110         inBuffer.write(in[offset++] & 0xFF);
111         if (inBuffer.size() >= blockSize)
112           {
113             result = inBuffer.toByteArray();
114             inBuffer.reset();
115             delegate.update(result, 0, result, 0);
116             outBuffer.write(result, 0, blockSize);
117           }
118       }
119     result = outBuffer.toByteArray();
120     outBuffer.reset();
121     return result;
122   }
123 }