Assembly.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* Assembly.java --
  2. Copyright (C) 2003, 2006 Free Software Foundation, Inc.
  3. This file is a part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or (at
  7. your option) any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. USA
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.crypto.assembly;
  32. import java.util.Map;
  33. /**
  34. * An <code>Assembly</code> is a construction consisting of a chain of
  35. * {@link Transformer} elements; each wired in pre- or post- transformation
  36. * mode. This chain is terminated by one <code>LoopbackTransformer</code>
  37. * element.
  38. * <p>
  39. * Once constructed, and correctly initialised, the bulk of the methods
  40. * available on the <code>Assembly</code> are delegated to the <i>head</i> of
  41. * the {@link Transformer} chain of the <code>Assembly</code>.
  42. *
  43. * @see Transformer
  44. */
  45. public class Assembly
  46. {
  47. public static final String DIRECTION = "gnu.crypto.assembly.assembly.direction";
  48. /** Flag that tells if the instance is initialised or not; and if yes how. */
  49. private Direction wired;
  50. /** The first Transformer in the chain. */
  51. private Transformer head;
  52. /**
  53. * Trivial constructor that sets the <i>chain</i> to a
  54. * <code>LoopbackTransformer</code>.
  55. */
  56. public Assembly()
  57. {
  58. super();
  59. wired = null;
  60. head = new LoopbackTransformer();
  61. }
  62. /**
  63. * Adds the designated {@link Transformer} and signals that it should operate
  64. * in pre-processing mode; i.e. it should apply its internal transformation
  65. * algorithm on the input data stream, <b>before</b> it passes that stream to
  66. * the next element in the <i>chain</i>.
  67. *
  68. * @param t the {@link Transformer} to add at the head of the current chain.
  69. * @throws IllegalArgumentException if the designated {@link Transformer} has
  70. * a non-null tail; i.e. it is already an element of a chain.
  71. */
  72. public void addPreTransformer(Transformer t)
  73. {
  74. wireTransformer(t, Operation.PRE_PROCESSING);
  75. }
  76. /**
  77. * Adds the designated {@link Transformer} and signals that it should operate
  78. * in post-processing mode; i.e. it should apply its internal transformation
  79. * algorithm on the input data stream, <b>after</b> it passes that stream to
  80. * the next element in the <i>chain</i>.
  81. *
  82. * @param t the {@link Transformer} to add at the head of the current chain.
  83. * @throws IllegalArgumentException if the designated {@link Transformer} has
  84. * a non-null tail; i.e. it is already an element of a chain.
  85. */
  86. public void addPostTransformer(Transformer t)
  87. {
  88. wireTransformer(t, Operation.POST_PROCESSING);
  89. }
  90. /**
  91. * Initialises the <code>Assembly</code> for operation with specific
  92. * characteristics.
  93. *
  94. * @param attributes a set of name-value pairs that describes the desired
  95. * future behaviour of this instance.
  96. * @throws IllegalStateException if the instance is already initialised.
  97. */
  98. public void init(Map attributes) throws TransformerException
  99. {
  100. if (wired != null)
  101. throw new IllegalStateException();
  102. Direction flow = (Direction) attributes.get(DIRECTION);
  103. if (flow == null)
  104. flow = Direction.FORWARD;
  105. attributes.put(Transformer.DIRECTION, flow);
  106. head.init(attributes);
  107. wired = flow;
  108. }
  109. /**
  110. * Resets the <code>Assembly</code> for re-initialisation and use with other
  111. * characteristics. This method always succeeds.
  112. */
  113. public void reset()
  114. {
  115. head.reset();
  116. wired = null;
  117. }
  118. /**
  119. * Convenience method that calls the method with same name and three
  120. * arguments, using a byte array of length <code>1</code> whose contents are
  121. * the designated byte.
  122. *
  123. * @param b the byte to process.
  124. * @return the result of transformation.
  125. * @throws IllegalStateException if the instance is not initialised.
  126. * @throws TransformerException if a transformation-related exception occurs
  127. * during the operation.
  128. * @see #update(byte[], int, int)
  129. */
  130. public byte[] update(byte b) throws TransformerException
  131. {
  132. return update(new byte[] { b }, 0, 1);
  133. }
  134. /**
  135. * Convenience method that calls the method with same name and three
  136. * arguments. All bytes in <code>in</code>, starting from index position
  137. * <code>0</code> are considered.
  138. *
  139. * @param in the input data bytes.
  140. * @return the result of transformation.
  141. * @throws IllegalStateException if the instance is not initialised.
  142. * @throws TransformerException if a transformation-related exception occurs
  143. * during the operation.
  144. * @see #update(byte[], int, int)
  145. */
  146. public byte[] update(byte[] in) throws TransformerException
  147. {
  148. return update(in, 0, in.length);
  149. }
  150. /**
  151. * Processes a designated number of bytes from a given byte array.
  152. *
  153. * @param in the input data bytes.
  154. * @param offset index of <code>in</code> from which to start considering
  155. * data.
  156. * @param length the count of bytes to process.
  157. * @return the result of transformation.
  158. * @throws IllegalStateException if the instance is not initialised.
  159. * @throws TransformerException if a transformation-related exception occurs
  160. * during the operation.
  161. */
  162. public byte[] update(byte[] in, int offset, int length)
  163. throws TransformerException
  164. {
  165. if (wired == null)
  166. throw new IllegalStateException();
  167. return head.update(in, offset, length);
  168. }
  169. /**
  170. * Convenience method that calls the method with same name and three arguments
  171. * using a 0-long byte array.
  172. *
  173. * @return the result of transformation.
  174. * @throws IllegalStateException if the instance is not initialised.
  175. * @throws TransformerException if a transformation-related exception occurs
  176. * during the operation.
  177. * @see #lastUpdate(byte[], int, int)
  178. */
  179. public byte[] lastUpdate() throws TransformerException
  180. {
  181. return lastUpdate(new byte[0], 0, 0);
  182. }
  183. /**
  184. * Convenience method that calls the method with same name and three
  185. * arguments, using a byte array of length <code>1</code> whose contents are
  186. * the designated byte.
  187. *
  188. * @param b the byte to process.
  189. * @return the result of transformation.
  190. * @throws IllegalStateException if the instance is not initialised.
  191. * @throws TransformerException if a transformation-related exception occurs
  192. * during the operation.
  193. * @see #lastUpdate(byte[], int, int)
  194. */
  195. public byte[] lastUpdate(byte b) throws TransformerException
  196. {
  197. return lastUpdate(new byte[] { b }, 0, 1);
  198. }
  199. /**
  200. * Convenience method that calls the method with same name and three
  201. * arguments. All bytes in <code>in</code>, starting from index position
  202. * <code>0</code> are considered.
  203. *
  204. * @param in the input data bytes.
  205. * @return the result of transformation.
  206. * @throws IllegalStateException if the instance is not initialised.
  207. * @throws TransformerException if a transformation-related exception occurs
  208. * during the operation.
  209. * @see #lastUpdate(byte[], int, int)
  210. */
  211. public byte[] lastUpdate(byte[] in) throws TransformerException
  212. {
  213. return lastUpdate(in, 0, in.length);
  214. }
  215. /**
  216. * Processes a designated number of bytes from a given byte array and signals,
  217. * at the same time, that this is the last <i>push</i> operation for this
  218. * <code>Assembly</code>.
  219. *
  220. * @param in the input data bytes.
  221. * @param offset index of <code>in</code> from which to start considering
  222. * data.
  223. * @param length the count of bytes to process.
  224. * @return the result of transformation.
  225. * @throws IllegalStateException if the instance is not initialised.
  226. * @throws TransformerException if a transformation-related exception occurs
  227. * during the operation.
  228. */
  229. public byte[] lastUpdate(byte[] in, int offset, int length)
  230. throws TransformerException
  231. {
  232. if (wired == null)
  233. throw new IllegalStateException();
  234. byte[] result = head.lastUpdate(in, offset, length);
  235. reset();
  236. return result;
  237. }
  238. private void wireTransformer(Transformer t, Operation mode)
  239. {
  240. if (t.tail != null)
  241. throw new IllegalArgumentException();
  242. t.setMode(mode);
  243. t.tail = head;
  244. head = t;
  245. }
  246. }