📄 transformer.java
字号:
/* Transformer.java -- Copyright (C) 2003, 2006 Free Software Foundation, Inc.This file is a part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or (atyour option) any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301USALinking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package gnu.javax.crypto.assembly;import gnu.javax.crypto.pad.IPad;import java.io.ByteArrayOutputStream;import java.util.Map;/** * <p>A <code>Transformer</code> is an abstract representation of a two-way * <i>transformation</i> that can be chained together with other instances of * this type. Examples of such transformations in this library are: * {@link Cascade} cipher, {@link gnu.crypto.pad.IPad} algorithm, and a * ZLib-based deflater/inflater algorithm. A special implementation of a * <code>Transformer</code> to close a chain is also provided.</p> * * <p>A <code>Transformer</code> is characterised by the followings:<p> * <ul> * <li>It can be chained to other instances, to form an {@link Assembly}.</li> * <li>When configured in an {@link Assembly}, it can be set to apply its * internal transformation on the input data stream before (pre-processing) * or after (post-processing) passing the input data to the next element in * the chain. Note that the same type <code>Transformer</code> can be used as * either in pre-processing or a post-processing modes.</li> * <li>A special transformer --<code>LoopbackTransformer</code>-- is used to * close the chain.</li> * <li>A useful type of <code>Transformer</code> --one we're interested in-- * has internal buffers. The distinction between a casual push (update) * operation and the last one allows to correctly flush any intermediate * bytes that may exist in those buffers.</li> * </ul> * * <p>To allow wiring <code>Transformer</code> instances together, a * <i>minimal-output-size</i> in bytes is necessary. The trivial case of a * value of <code>1</code> for such attribute practically means that no output * buffering, from the previous element, is needed --which is independant of * buffering the input if the <code>Transformer</code> implementation itself is * block-based.</p> * * @see CascadeTransformer * @see PaddingTransformer * @see DeflateTransformer * @version $Revision: 1.1 $ */public abstract class Transformer{ // Constants and variables // ------------------------------------------------------------------------- public static final String DIRECTION = "gnu.crypto.assembly.transformer.direction"; // public static final String MODE = "gnu.crypto.assembly.transformer.mode"; protected Direction wired; protected Operation mode; protected Transformer tail = null; protected ByteArrayOutputStream inBuffer = new ByteArrayOutputStream(2048); protected ByteArrayOutputStream outBuffer = new ByteArrayOutputStream(2048); // Constructor(s) // ------------------------------------------------------------------------- /** Trivial protected constructor. */ protected Transformer() { super(); this.wired = null; } // Class methods // ------------------------------------------------------------------------- public static final Transformer getCascadeTransformer(Cascade cascade) { return new CascadeTransformer(cascade); } public static final Transformer getPaddingTransformer(IPad padding) { return new PaddingTransformer(padding); } public static final Transformer getDeflateTransformer() { return new DeflateTransformer(); } // Instance methods // ------------------------------------------------------------------------- /** * Sets the operational mode of this <code>Transformer</code>. * * @param mode the processing mode this <code>Transformer</code> is required * to operate in. * @throws IllegalStateException if this instance has already been assigned * an operational mode. */ public void setMode(final Operation mode) { if (this.mode != null) { throw new IllegalStateException(); } this.mode = mode; } /** * Returns <code>true</code> if this <code>Transformer</code> was wired in * pre-processing mode; <code>false</code> otherwise. * * @return <code>true</code> if this <code>Transformer</code> has been wired * in pre-processing mode; <code>false</code> otherwise. * @throws IllegalStateException if this instance has not yet been assigned * an operational <i>type</i>. */ public boolean isPreProcessing() { if (mode == null) { throw new IllegalStateException(); } return (mode == Operation.PRE_PROCESSING); } /** * Returns <code>true</code> if this <code>Transformer</code> was wired in * post-processing mode; <code>false</code> otherwise. * * @return <code>true</code> if this <code>Transformer</code> has been wired * in post-processing mode; <code>false</code> otherwise. * @throws IllegalStateException if this instance has not yet been assigned * an operational <i>type</i>. */ public boolean isPostProcessing() { return !isPreProcessing(); } /** * Initialises the <code>Transformer</code> for operation with specific * characteristics. * * @param attributes a set of name-value pairs that describes the desired * future behaviour of this instance. * @throws IllegalStateException if the instance is already initialised. */ public void init(Map attributes) throws TransformerException { if (wired != null) { throw new IllegalStateException(); } Direction flow = (Direction) attributes.get(DIRECTION); if (flow == null) { flow = Direction.FORWARD; } wired = flow; inBuffer.reset(); outBuffer.reset(); tail.init(attributes); // initialise tail first initDelegate(attributes); // initialise this instance } /** * Returns the block-size of this <code>Transformer</code>. A value of * <code>1</code> indicates that this instance is block-agnostic. * * @return the current minimal required block size. */ public int currentBlockSize() { if (wired == null) { throw new IllegalStateException(); } return delegateBlockSize(); } /** * Resets the <code>Transformer</code> for re-initialisation and use with * other characteristics. This method always succeeds. */ public void reset() { resetDelegate();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -