Source for gnu.javax.crypto.assembly.Assembly

   1: /* Assembly.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.util.Map;
  42: 
  43: /**
  44:  * An <code>Assembly</code> is a construction consisting of a chain of
  45:  * {@link Transformer} elements; each wired in pre- or post- transformation
  46:  * mode. This chain is terminated by one <code>LoopbackTransformer</code>
  47:  * element.
  48:  * <p>
  49:  * Once constructed, and correctly initialised, the bulk of the methods
  50:  * available on the <code>Assembly</code> are delegated to the <i>head</i> of
  51:  * the {@link Transformer} chain of the <code>Assembly</code>.
  52:  *
  53:  * @see Transformer
  54:  */
  55: public class Assembly
  56: {
  57:   public static final String DIRECTION = "gnu.crypto.assembly.assembly.direction";
  58: 
  59:   /** Flag that tells if the instance is initialised or not; and if yes how. */
  60:   private Direction wired;
  61: 
  62:   /** The first Transformer in the chain. */
  63:   private Transformer head;
  64: 
  65:   /**
  66:    * Trivial constructor that sets the <i>chain</i> to a
  67:    * <code>LoopbackTransformer</code>.
  68:    */
  69:   public Assembly()
  70:   {
  71:     super();
  72: 
  73:     wired = null;
  74:     head = new LoopbackTransformer();
  75:   }
  76: 
  77:   /**
  78:    * Adds the designated {@link Transformer} and signals that it should operate
  79:    * in pre-processing mode; i.e. it should apply its internal transformation
  80:    * algorithm on the input data stream, <b>before</b> it passes that stream to
  81:    * the next element in the <i>chain</i>.
  82:    *
  83:    * @param t the {@link Transformer} to add at the head of the current chain.
  84:    * @throws IllegalArgumentException if the designated {@link Transformer} has
  85:    *           a non-null tail; i.e. it is already an element of a chain.
  86:    */
  87:   public void addPreTransformer(Transformer t)
  88:   {
  89:     wireTransformer(t, Operation.PRE_PROCESSING);
  90:   }
  91: 
  92:   /**
  93:    * Adds the designated {@link Transformer} and signals that it should operate
  94:    * in post-processing mode; i.e. it should apply its internal transformation
  95:    * algorithm on the input data stream, <b>after</b> it passes that stream to
  96:    * the next element in the <i>chain</i>.
  97:    *
  98:    * @param t the {@link Transformer} to add at the head of the current chain.
  99:    * @throws IllegalArgumentException if the designated {@link Transformer} has
 100:    *           a non-null tail; i.e. it is already an element of a chain.
 101:    */
 102:   public void addPostTransformer(Transformer t)
 103:   {
 104:     wireTransformer(t, Operation.POST_PROCESSING);
 105:   }
 106: 
 107:   /**
 108:    * Initialises the <code>Assembly</code> for operation with specific
 109:    * characteristics.
 110:    *
 111:    * @param attributes a set of name-value pairs that describes the desired
 112:    *          future behaviour of this instance.
 113:    * @throws IllegalStateException if the instance is already initialised.
 114:    */
 115:   public void init(Map attributes) throws TransformerException
 116:   {
 117:     if (wired != null)
 118:       throw new IllegalStateException();
 119:     Direction flow = (Direction) attributes.get(DIRECTION);
 120:     if (flow == null)
 121:       flow = Direction.FORWARD;
 122:     attributes.put(Transformer.DIRECTION, flow);
 123:     head.init(attributes);
 124:     wired = flow;
 125:   }
 126: 
 127:   /**
 128:    * Resets the <code>Assembly</code> for re-initialisation and use with other
 129:    * characteristics. This method always succeeds.
 130:    */
 131:   public void reset()
 132:   {
 133:     head.reset();
 134:     wired = null;
 135:   }
 136: 
 137:   /**
 138:    * Convenience method that calls the method with same name and three
 139:    * arguments, using a byte array of length <code>1</code> whose contents are
 140:    * the designated byte.
 141:    *
 142:    * @param b the byte to process.
 143:    * @return the result of transformation.
 144:    * @throws IllegalStateException if the instance is not initialised.
 145:    * @throws TransformerException if a transformation-related exception occurs
 146:    *           during the operation.
 147:    * @see #update(byte[], int, int)
 148:    */
 149:   public byte[] update(byte b) throws TransformerException
 150:   {
 151:     return update(new byte[] { b }, 0, 1);
 152:   }
 153: 
 154:   /**
 155:    * Convenience method that calls the method with same name and three
 156:    * arguments. All bytes in <code>in</code>, starting from index position
 157:    * <code>0</code> are considered.
 158:    *
 159:    * @param in the input data bytes.
 160:    * @return the result of transformation.
 161:    * @throws IllegalStateException if the instance is not initialised.
 162:    * @throws TransformerException if a transformation-related exception occurs
 163:    *           during the operation.
 164:    * @see #update(byte[], int, int)
 165:    */
 166:   public byte[] update(byte[] in) throws TransformerException
 167:   {
 168:     return update(in, 0, in.length);
 169:   }
 170: 
 171:   /**
 172:    * Processes a designated number of bytes from a given byte array.
 173:    *
 174:    * @param in the input data bytes.
 175:    * @param offset index of <code>in</code> from which to start considering
 176:    *          data.
 177:    * @param length the count of bytes to process.
 178:    * @return the result of transformation.
 179:    * @throws IllegalStateException if the instance is not initialised.
 180:    * @throws TransformerException if a transformation-related exception occurs
 181:    *           during the operation.
 182:    */
 183:   public byte[] update(byte[] in, int offset, int length)
 184:       throws TransformerException
 185:   {
 186:     if (wired == null)
 187:       throw new IllegalStateException();
 188:     return head.update(in, offset, length);
 189:   }
 190: 
 191:   /**
 192:    * Convenience method that calls the method with same name and three arguments
 193:    * using a 0-long byte array.
 194:    *
 195:    * @return the result of transformation.
 196:    * @throws IllegalStateException if the instance is not initialised.
 197:    * @throws TransformerException if a transformation-related exception occurs
 198:    *           during the operation.
 199:    * @see #lastUpdate(byte[], int, int)
 200:    */
 201:   public byte[] lastUpdate() throws TransformerException
 202:   {
 203:     return lastUpdate(new byte[0], 0, 0);
 204:   }
 205: 
 206:   /**
 207:    * Convenience method that calls the method with same name and three
 208:    * arguments, using a byte array of length <code>1</code> whose contents are
 209:    * the designated byte.
 210:    *
 211:    * @param b the byte to process.
 212:    * @return the result of transformation.
 213:    * @throws IllegalStateException if the instance is not initialised.
 214:    * @throws TransformerException if a transformation-related exception occurs
 215:    *           during the operation.
 216:    * @see #lastUpdate(byte[], int, int)
 217:    */
 218:   public byte[] lastUpdate(byte b) throws TransformerException
 219:   {
 220:     return lastUpdate(new byte[] { b }, 0, 1);
 221:   }
 222: 
 223:   /**
 224:    * Convenience method that calls the method with same name and three
 225:    * arguments. All bytes in <code>in</code>, starting from index position
 226:    * <code>0</code> are considered.
 227:    *
 228:    * @param in the input data bytes.
 229:    * @return the result of transformation.
 230:    * @throws IllegalStateException if the instance is not initialised.
 231:    * @throws TransformerException if a transformation-related exception occurs
 232:    *           during the operation.
 233:    * @see #lastUpdate(byte[], int, int)
 234:    */
 235:   public byte[] lastUpdate(byte[] in) throws TransformerException
 236:   {
 237:     return lastUpdate(in, 0, in.length);
 238:   }
 239: 
 240:   /**
 241:    * Processes a designated number of bytes from a given byte array and signals,
 242:    * at the same time, that this is the last <i>push</i> operation for this
 243:    * <code>Assembly</code>.
 244:    *
 245:    * @param in the input data bytes.
 246:    * @param offset index of <code>in</code> from which to start considering
 247:    *          data.
 248:    * @param length the count of bytes to process.
 249:    * @return the result of transformation.
 250:    * @throws IllegalStateException if the instance is not initialised.
 251:    * @throws TransformerException if a transformation-related exception occurs
 252:    *           during the operation.
 253:    */
 254:   public byte[] lastUpdate(byte[] in, int offset, int length)
 255:       throws TransformerException
 256:   {
 257:     if (wired == null)
 258:       throw new IllegalStateException();
 259:     byte[] result = head.lastUpdate(in, offset, length);
 260:     reset();
 261:     return result;
 262:   }
 263: 
 264:   private void wireTransformer(Transformer t, Operation mode)
 265:   {
 266:     if (t.tail != null)
 267:       throw new IllegalArgumentException();
 268:     t.setMode(mode);
 269:     t.tail = head;
 270:     head = t;
 271:   }
 272: }