1 /* DeflateTransformer.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
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
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
;
42 import java
.util
.zip
.DataFormatException
;
43 import java
.util
.zip
.Deflater
;
44 import java
.util
.zip
.Inflater
;
47 * <p>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
51 * the original data.</p>
53 * <p>This {@link Transformer} uses a {@link Deflater} instance to carry on the
54 * compression, and an {@link Inflater} to do the decompression.</p>
56 * <p>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.</p>
60 * @version Revision: $
62 class DeflateTransformer
extends Transformer
65 // Constants and variables
66 // -------------------------------------------------------------------------
68 private Deflater compressor
;
70 private Inflater decompressor
;
72 private int outputBlockSize
= 512; // default zlib buffer size
74 private byte[] zlibBuffer
;
77 // -------------------------------------------------------------------------
86 // -------------------------------------------------------------------------
89 // -------------------------------------------------------------------------
91 void initDelegate(Map attributes
) throws TransformerException
95 throw new TransformerException(
97 new IllegalStateException(
98 "Compression transformer missing its tail!"));
100 outputBlockSize
= tail
.currentBlockSize();
101 zlibBuffer
= new byte[outputBlockSize
];
102 Direction flow
= (Direction
) attributes
.get(DIRECTION
);
103 if (flow
== Direction
.FORWARD
)
105 compressor
= new Deflater();
109 decompressor
= new Inflater();
113 int delegateBlockSize()
115 // return outputBlockSize;
127 byte[] updateDelegate(byte[] in
, int offset
, int length
)
128 throws TransformerException
131 if (wired
== Direction
.FORWARD
)
133 compressor
.setInput(in
, offset
, length
);
134 while (!compressor
.needsInput())
140 { // decompression: inflate first and then update tail
141 decompress(in
, offset
, length
);
144 result
= inBuffer
.toByteArray();
149 // byte[] lastUpdateDelegate(byte[] in, int offset, int length)
150 // throws TransformerException {
151 // // process multiples of blocksize as much as possible
152 // byte[] result = this.updateDelegate(in, offset, length);
153 // inBuffer.write(result, 0, result.length);
154 // if (wired == Direction.FORWARD) { // compressing
155 // if (!compressor.finished()) {
156 // compressor.finish();
157 // while (!compressor.finished()) {
161 // } else { // decompressing
162 // if (!decompressor.finished()) {
163 // throw new TransformerException("lastUpdateDelegate()",
164 // new IllegalStateException("Compression transformer, after last "
165 // +"update, must be finished but isn't"));
169 // result = inBuffer.toByteArray();
173 byte[] lastUpdateDelegate() throws TransformerException
175 // process multiples of blocksize as much as possible
176 if (wired
== Direction
.FORWARD
)
178 if (!compressor
.finished())
181 while (!compressor
.finished())
189 if (!decompressor
.finished())
191 throw new TransformerException(
192 "lastUpdateDelegate()",
193 new IllegalStateException(
194 "Compression transformer, after last "
195 + "update, must be finished but isn't"));
198 byte[] result
= inBuffer
.toByteArray();
203 private void compress()
205 int len
= compressor
.deflate(zlibBuffer
);
208 inBuffer
.write(zlibBuffer
, 0, len
);
212 private void decompress(byte[] in
, int offset
, int length
)
213 throws TransformerException
215 decompressor
.setInput(in
, offset
, length
);
221 len
= decompressor
.inflate(zlibBuffer
);
223 catch (DataFormatException x
)
225 throw new TransformerException("decompress()", x
);
229 inBuffer
.write(zlibBuffer
, 0, len
);