2006-08-14 Mark Wielaard <mark@klomp.org>
[official-gcc.git] / libjava / classpath / gnu / javax / crypto / assembly / CascadeTransformer.java
blob5fce51a15eb0a528f1c02fbb99b2b7bf07d00d01
1 /* CascadeTransformer.java --
2 Copyright (C) 2003, 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
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.
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.
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
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.
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. */
39 package gnu.javax.crypto.assembly;
41 import java.security.InvalidKeyException;
42 import java.util.Map;
44 /**
45 * An Adapter to use any {@link Cascade} as a {@link Transformer} in an
46 * {@link Assembly}.
48 class CascadeTransformer
49 extends Transformer
51 private Cascade delegate;
53 private int blockSize;
55 CascadeTransformer(Cascade delegate)
57 super();
59 this.delegate = delegate;
62 void initDelegate(Map attributes) throws TransformerException
64 attributes.put(Cascade.DIRECTION, wired);
65 try
67 delegate.init(attributes);
69 catch (InvalidKeyException x)
71 throw new TransformerException("initDelegate()", x);
73 blockSize = delegate.currentBlockSize();
76 int delegateBlockSize()
78 return blockSize;
81 void resetDelegate()
83 delegate.reset();
84 blockSize = 0;
87 byte[] updateDelegate(byte[] in, int offset, int length)
88 throws TransformerException
90 byte[] result = updateInternal(in, offset, length);
91 return result;
94 byte[] lastUpdateDelegate() throws TransformerException
96 if (inBuffer.size() != 0)
98 IllegalStateException cause = new IllegalStateException(
99 "Cascade transformer, after last update, must be empty but isn't");
100 throw new TransformerException("lastUpdateDelegate()", cause);
102 return new byte[0];
105 private byte[] updateInternal(byte[] in, int offset, int length)
107 byte[] result;
108 for (int i = 0; i < length; i++)
110 inBuffer.write(in[offset++] & 0xFF);
111 if (inBuffer.size() >= blockSize)
113 result = inBuffer.toByteArray();
114 inBuffer.reset();
115 delegate.update(result, 0, result, 0);
116 outBuffer.write(result, 0, blockSize);
119 result = outBuffer.toByteArray();
120 outBuffer.reset();
121 return result;