Java Source Code: gnu.javax.crypto.assembly.DeflateTransformer


   1: /* DeflateTransformer.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: import java.util.zip.DataFormatException;
  43: import java.util.zip.Deflater;
  44: import java.util.zip.Inflater;
  45: 
  46: /**
  47:  * A {@link Transformer} Adapter allowing inclusion of a DEFLATE compression
  48:  * algorithm in an {@link Assembly} chain. The {@link Direction#FORWARD}
  49:  * transformation is a compression (deflate) of input data, while the
  50:  * {@link Direction#REVERSED} one is a decompression (inflate) that restores the
  51:  * original data.
  52:  * <p>
  53:  * This {@link Transformer} uses a {@link Deflater} instance to carry on the
  54:  * compression, and an {@link Inflater} to do the decompression.
  55:  * <p>
  56:  * When using such a {@link Transformer}, in an {@link Assembly}, there must
  57:  * be at least one element behind this instance in the constructed chain;
  58:  * otherwise, a {@link TransformerException} is thrown at initialisation time.
  59:  */
  60: class DeflateTransformer
  61:     extends Transformer
  62:	  {
  63:  private Deflater compressor;
  64:
  65:  private Inflater decompressor;
  66:
  67:  private int outputBlockSize = 512; // default zlib buffer size
  68:
  69:  private byte[] zlibBuffer;
  70:
  71:  DeflateTransformer()
  72:	    {
  73:    super();
  74:
  75:  }
  76:
  77:  void initDelegate(Map attributes) throws TransformerException
  78:	    {
  79:    if (tail == null)
  80:	        {
  81:        IllegalStateException cause = new IllegalStateException(
  82:            "Compression transformer missing its tail!");
  83:        throw new TransformerException("initDelegate()", cause);
  84:      }
  85:    outputBlockSize = tail.currentBlockSize();
  86:    zlibBuffer = new byte[outputBlockSize];
  87:    Direction flow = (Direction) attributes.get(DIRECTION);
  88:    if (flow == Direction.FORWARD)
  89:      compressor = new Deflater();
  90:    else
  91:      decompressor = new Inflater();
  92:  }
  93:
  94:  int delegateBlockSize()
  95:	    {
  96:    return 1;
  97:  }
  98:
  99:  void resetDelegate()
 100:	    {
 101:    compressor = null;
 102:    decompressor = null;
 103:    outputBlockSize = 1;
 104:    zlibBuffer = null;
 105:  }
 106:
 107:  byte[] updateDelegate(byte[] in, int offset, int length)
 108:      throws TransformerException
 109:	    {
 110:    byte[] result;
 111:    if (wired == Direction.FORWARD)
 112:	        {
 113:        compressor.setInput(in, offset, length);
 114:        while (! compressor.needsInput())
 115:          compress();
 116:      }
 117:    else // decompression: inflate first and then update tail
 118:      decompress(in, offset, length);
 119:    result = inBuffer.toByteArray();
 120:    inBuffer.reset();
 121:    return result;
 122:  }
 123:
 124:  byte[] lastUpdateDelegate() throws TransformerException
 125:	    {
 126:    // process multiples of blocksize as much as possible
 127:    if (wired == Direction.FORWARD) // compressing
 128:	        {
 129:        if (! compressor.finished())
 130:	            {
 131:            compressor.finish();
 132:            while (! compressor.finished())
 133:              compress();
 134:          }
 135:      }
 136:    else // decompressing
 137:	        {
 138:        if (! decompressor.finished())
 139:	            {
 140:            IllegalStateException cause = new IllegalStateException(
 141:                "Compression transformer, after last update, must be finished "
 142:                + "but isn't");
 143:            throw new TransformerException("lastUpdateDelegate()", cause);
 144:          }
 145:      }
 146:    byte[] result = inBuffer.toByteArray();
 147:    inBuffer.reset();
 148:    return result;
 149:  }
 150:
 151:  private void compress()
 152:	    {
 153:    int len = compressor.deflate(zlibBuffer);
 154:    if (len > 0)
 155:      inBuffer.write(zlibBuffer, 0, len);
 156:  }
 157:
 158:  private void decompress(byte[] in, int offset, int length)
 159:      throws TransformerException
 160:	    {
 161:    decompressor.setInput(in, offset, length);
 162:    int len = 1;
 163:    while (len > 0)
 164:	        {
 165:        try
 166:	            {
 167:            len = decompressor.inflate(zlibBuffer);
 168:          }
 169:        catch (DataFormatException x)
 170:	            {
 171:            throw new TransformerException("decompress()", x);
 172:          }
 173:        if (len > 0)
 174:          inBuffer.write(zlibBuffer, 0, len);
 175:      }
 176:  }
 177:}