Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / javax / crypto / assembly / DeflateTransformer.java
blob35328a6c1dcec1e93b5d73e82372c3bc903f42cd
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
19 USA
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.
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;
41 import java.util.Map;
42 import java.util.zip.DataFormatException;
43 import java.util.zip.Deflater;
44 import java.util.zip.Inflater;
46 /**
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;
76 // Constructor(s)
77 // -------------------------------------------------------------------------
79 DeflateTransformer()
81 super();
85 // Class methods
86 // -------------------------------------------------------------------------
88 // Instance methods
89 // -------------------------------------------------------------------------
91 void initDelegate(Map attributes) throws TransformerException
93 if (tail == null)
95 throw new TransformerException(
96 "initDelegate()",
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();
107 else
109 decompressor = new Inflater();
113 int delegateBlockSize()
115 // return outputBlockSize;
116 return 1;
119 void resetDelegate()
121 compressor = null;
122 decompressor = null;
123 outputBlockSize = 1;
124 zlibBuffer = null;
127 byte[] updateDelegate(byte[] in, int offset, int length)
128 throws TransformerException
130 byte[] result;
131 if (wired == Direction.FORWARD)
133 compressor.setInput(in, offset, length);
134 while (!compressor.needsInput())
136 compress();
139 else
140 { // decompression: inflate first and then update tail
141 decompress(in, offset, length);
144 result = inBuffer.toByteArray();
145 inBuffer.reset();
146 return result;
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()) {
158 // compress();
159 // }
160 // }
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"));
166 // }
167 // }
169 // result = inBuffer.toByteArray();
170 // inBuffer.reset();
171 // return result;
172 // }
173 byte[] lastUpdateDelegate() throws TransformerException
175 // process multiples of blocksize as much as possible
176 if (wired == Direction.FORWARD)
177 { // compressing
178 if (!compressor.finished())
180 compressor.finish();
181 while (!compressor.finished())
183 compress();
187 else
188 { // decompressing
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();
199 inBuffer.reset();
200 return result;
203 private void compress()
205 int len = compressor.deflate(zlibBuffer);
206 if (len > 0)
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);
216 int len = 1;
217 while (len > 0)
221 len = decompressor.inflate(zlibBuffer);
223 catch (DataFormatException x)
225 throw new TransformerException("decompress()", x);
227 if (len > 0)
229 inBuffer.write(zlibBuffer, 0, len);