Debian Codegraph @

src/main/org/h2/tools/CompressTool.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.tools;
7
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.OutputStream;
11 import java.util.zip.DeflaterOutputStream;
12 import java.util.zip.GZIPInputStream;
13 import java.util.zip.GZIPOutputStream;
14 import java.util.zip.InflaterInputStream;
15 import java.util.zip.ZipEntry;
16 import java.util.zip.ZipInputStream;
17 import java.util.zip.ZipOutputStream;
18
19 import org.h2.api.ErrorCode;
20 import org.h2.compress.CompressDeflate;
21 import org.h2.compress.CompressLZF;
22 import org.h2.compress.CompressNo;
23 import org.h2.compress.Compressor;
24 import org.h2.compress.LZFInputStream;
25 import org.h2.compress.LZFOutputStream;
26 import org.h2.engine.Constants;
27 import org.h2.message.DbException;
28 import org.h2.util.Bits;
29 import org.h2.util.StringUtils;
30 import org.h2.util.Utils;
31
32 /**
33 * A tool to losslessly compress data, and expand the compressed data again.
34 */
35 public class CompressTool {
36
37 private static final int MAX_BUFFER_SIZE = 3 * Constants.IO_BUFFER_SIZE_COMPRESS;
38
39 private byte[] buffer;
40
41 private CompressTool() {
42 // don't allow construction
43 }
44
45 private byte[] getBuffer(int min) {
46 if (min > MAX_BUFFER_SIZE) {
47 return Utils.newBytes(min);
48 }
49 if (buffer == null || buffer.length < min) {
50 buffer = Utils.newBytes(min);
51 }
52 return buffer;
53 }
54
55 /**
56 * Get a new instance. Each instance uses a separate buffer, so multiple
57 * instances can be used concurrently. However each instance alone is not
58 * multithreading safe.
59 *
60 * @return a new instance
61 */
62 public static CompressTool getInstance() {
63 return new CompressTool();
64 }
65
66 /**
67 * Compressed the data using the specified algorithm. If no algorithm is
68 * supplied, LZF is used
69 *
70 * @param in the byte array with the original data
71 * @param algorithm the algorithm (LZF, DEFLATE)
72 * @return the compressed data
73 */
74 public byte[] compress(byte[] in, String algorithm) {
75 int len = in.length;
76 if (in.length < 5) {
77 algorithm = "NO";
78 }
79 Compressor compress = getCompressor(algorithm);
80 byte[] buff = getBuffer((len < 100 ? len + 100 : len) * 2);
81 int newLen = compress(in, in.length, compress, buff);
82 return Utils.copyBytes(buff, newLen);
83 }
84
85 private static int compress(byte[] in, int len, Compressor compress,
86 byte[] out) {
87 out[0] = (byte) compress.getAlgorithm();
88 int start = 1 + writeVariableInt(out, 1, len);
89 int newLen = compress.compress(in, 0, len, out, start);
90 if (newLen > len + start || newLen <= 0) {
91 out[0] = Compressor.NO;
92 System.arraycopy(in, 0, out, start, len);
93 newLen = len + start;
94 }
95 return newLen;
96 }
97
98 /**
99 * Expands the compressed data.
100 *
101 * @param in the byte array with the compressed data
102 * @return the uncompressed data
103 */
104 public byte[] expand(byte[] in) {
105 if (in.length == 0) {
106 throw DbException.get(ErrorCode.COMPRESSION_ERROR);
107 }
108 int algorithm = in[0];
109 Compressor compress = getCompressor(algorithm);
110 try {
111 int len = readVariableInt(in, 1);
112 int start = 1 + getVariableIntLength(len);
113 byte[] buff = Utils.newBytes(len);
114 compress.expand(in, start, in.length - start, buff, 0, len);
115 return buff;
116 } catch (Exception e) {
117 throw DbException.get(ErrorCode.COMPRESSION_ERROR, e);
118 }
119 }
120
121 /**
122 * INTERNAL
123 * @param in compressed data
124 * @param out uncompressed result
125 * @param outPos the offset at the output array
126 */
127 public static void expand(byte[] in, byte[] out, int outPos) {
128 int algorithm = in[0];
129 Compressor compress = getCompressor(algorithm);
130 try {
131 int len = readVariableInt(in, 1);
132 int start = 1 + getVariableIntLength(len);
133 compress.expand(in, start, in.length - start, out, outPos, len);
134 } catch (Exception e) {
135 throw DbException.get(ErrorCode.COMPRESSION_ERROR, e);
136 }
137 }
138
139 /**
140 * Read a variable size integer using Rice coding.
141 *
142 * @param buff the buffer
143 * @param pos the position
144 * @return the integer
145 */
146 public static int readVariableInt(byte[] buff, int pos) {
147 int x = buff[pos++] & 0xff;
148 if (x < 0x80) {
149 return x;
150 }
151 if (x < 0xc0) {
152 return ((x & 0x3f) << 8) + (buff[pos] & 0xff);
153 }
154 if (x < 0xe0) {
155 return ((x & 0x1f) << 16) +
156 ((buff[pos++] & 0xff) << 8) +
157 (buff[pos] & 0xff);
158 }
159 if (x < 0xf0) {
160 return ((x & 0xf) << 24) +
161 ((buff[pos++] & 0xff) << 16) +
162 ((buff[pos++] & 0xff) << 8) +
163 (buff[pos] & 0xff);
164 }
165 return Bits.readInt(buff, pos);
166 }
167
168 /**
169 * Write a variable size integer using Rice coding.
170 * Negative values need 5 bytes.
171 *
172 * @param buff the buffer
173 * @param pos the position
174 * @param x the value
175 * @return the number of bytes written (0-5)
176 */
177 public static int writeVariableInt(byte[] buff, int pos, int x) {
178 if (x < 0) {
179 buff[pos++] = (byte) 0xf0;
180 Bits.writeInt(buff, pos, x);
181 return 5;
182 } else if (x < 0x80) {
183 buff[pos] = (byte) x;
184 return 1;
185 } else if (x < 0x4000) {
186 buff[pos++] = (byte) (0x80 | (x >> 8));
187 buff[pos] = (byte) x;
188 return 2;
189 } else if (x < 0x20_0000) {
190 buff[pos++] = (byte) (0xc0 | (x >> 16));
191 buff[pos++] = (byte) (x >> 8);
192 buff[pos] = (byte) x;
193 return 3;
194 } else if (x < 0x1000_0000) {
195 Bits.writeInt(buff, pos, x | 0xe000_0000);
196 return 4;
197 } else {
198 buff[pos++] = (byte) 0xf0;
199 Bits.writeInt(buff, pos, x);
200 return 5;
201 }
202 }
203
204 /**
205 * Get a variable size integer length using Rice coding.
206 * Negative values need 5 bytes.
207 *
208 * @param x the value
209 * @return the number of bytes needed (0-5)
210 */
211 public static int getVariableIntLength(int x) {
212 if (x < 0) {
213 return 5;
214 } else if (x < 0x80) {
215 return 1;
216 } else if (x < 0x4000) {
217 return 2;
218 } else if (x < 0x20_0000) {
219 return 3;
220 } else if (x < 0x1000_0000) {
221 return 4;
222 } else {
223 return 5;
224 }
225 }
226
227 private static Compressor getCompressor(String algorithm) {
228 if (algorithm == null) {
229 algorithm = "LZF";
230 }
231 int idx = algorithm.indexOf(' ');
232 String options = null;
233 if (idx > 0) {
234 options = algorithm.substring(idx + 1);
235 algorithm = algorithm.substring(0, idx);
236 }
237 int a = getCompressAlgorithm(algorithm);
238 Compressor compress = getCompressor(a);
239 compress.setOptions(options);
240 return compress;
241 }
242
243 /**
244 * INTERNAL
245 * @param algorithm to translate into index
246 * @return index of the specified algorithm
247 */
248 private static int getCompressAlgorithm(String algorithm) {
249 algorithm = StringUtils.toUpperEnglish(algorithm);
250 if ("NO".equals(algorithm)) {
251 return Compressor.NO;
252 } else if ("LZF".equals(algorithm)) {
253 return Compressor.LZF;
254 } else if ("DEFLATE".equals(algorithm)) {
255 return Compressor.DEFLATE;
256 } else {
257 throw DbException.get(
258 ErrorCode.UNSUPPORTED_COMPRESSION_ALGORITHM_1,
259 algorithm);
260 }
261 }
262
263 private static Compressor getCompressor(int algorithm) {
264 switch (algorithm) {
265 case Compressor.NO:
266 return new CompressNo();
267 case Compressor.LZF:
268 return new CompressLZF();
269 case Compressor.DEFLATE:
270 return new CompressDeflate();
271 default:
272 throw DbException.get(
273 ErrorCode.UNSUPPORTED_COMPRESSION_ALGORITHM_1,
274 Integer.toString(algorithm));
275 }
276 }
277
278 /**
279 * INTERNAL
280 * @param out stream
281 * @param compressionAlgorithm to be used
282 * @param entryName in a zip file
283 * @return compressed stream
284 */
285 public static OutputStream wrapOutputStream(OutputStream out,
286 String compressionAlgorithm, String entryName) {
287 try {
288 if ("GZIP".equals(compressionAlgorithm)) {
289 out = new GZIPOutputStream(out);
290 } else if ("ZIP".equals(compressionAlgorithm)) {
291 ZipOutputStream z = new ZipOutputStream(out);
292 z.putNextEntry(new ZipEntry(entryName));
293 out = z;
294 } else if ("DEFLATE".equals(compressionAlgorithm)) {
295 out = new DeflaterOutputStream(out);
296 } else if ("LZF".equals(compressionAlgorithm)) {
297 out = new LZFOutputStream(out);
298 } else if (compressionAlgorithm != null) {
299 throw DbException.get(
300 ErrorCode.UNSUPPORTED_COMPRESSION_ALGORITHM_1,
301 compressionAlgorithm);
302 }
303 return out;
304 } catch (IOException e) {
305 throw DbException.convertIOException(e, null);
306 }
307 }
308
309 /**
310 * INTERNAL
311 * @param in stream
312 * @param compressionAlgorithm to be used
313 * @param entryName in a zip file
314 * @return in stream or null if there is no such entry
315 */
316 public static InputStream wrapInputStream(InputStream in,
317 String compressionAlgorithm, String entryName) {
318 try {
319 if ("GZIP".equals(compressionAlgorithm)) {
320 in = new GZIPInputStream(in);
321 } else if ("ZIP".equals(compressionAlgorithm)) {
322 ZipInputStream z = new ZipInputStream(in);
323 while (true) {
324 ZipEntry entry = z.getNextEntry();
325 if (entry == null) {
326 return null;
327 }
328 if (entryName.equals(entry.getName())) {
329 break;
330 }
331 }
332 in = z;
333 } else if ("DEFLATE".equals(compressionAlgorithm)) {
334 in = new InflaterInputStream(in);
335 } else if ("LZF".equals(compressionAlgorithm)) {
336 in = new LZFInputStream(in);
337 } else if (compressionAlgorithm != null) {
338 throw DbException.get(
339 ErrorCode.UNSUPPORTED_COMPRESSION_ALGORITHM_1,
340 compressionAlgorithm);
341 }
342 return in;
343 } catch (IOException e) {
344 throw DbException.convertIOException(e, null);
345 }
346 }
347
348 }
349