File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\compress\CompressDeflate.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.compress;
8
9 import java.util.StringTokenizer;
10 import java.util.zip.DataFormatException;
11 import java.util.zip.Deflater;
12 import java.util.zip.Inflater;
13 import org.h2.constant.ErrorCode;
14 import org.h2.message.DbException;
15
16 /**
17 * This is a wrapper class for the Deflater class.
18 * This algorithm supports the following options:
19 * <ul>
20 * <li>l or level: -1 (default), 0 (no compression),
21 * 1 (best speed), ..., 9 (best compression)
22 * </li><li>s or strategy: 0 (default),
23 * 1 (filtered), 2 (huffman only)
24 * </li></ul>
25 * See also java.util.zip.Deflater for details.
26 */
27 public class CompressDeflate implements Compressor {
28
29 private int level = Deflater.DEFAULT_COMPRESSION;
30 private int strategy = Deflater.DEFAULT_STRATEGY;
31
32 public void setOptions(String options) {
33 if (options == null) {
34 return;
35 }
36 try {
37 StringTokenizer tokenizer = new StringTokenizer(options);
38 while (tokenizer.hasMoreElements()) {
39 String option = tokenizer.nextToken();
40 if ("level".equals(option) || "l".equals(option)) {
41 level = Integer.parseInt(tokenizer.nextToken());
42 } else if ("strategy".equals(option) || "s".equals(option)) {
43 strategy = Integer.parseInt(tokenizer.nextToken());
44 }
45 Deflater deflater = new Deflater(level);
46 deflater.setStrategy(strategy);
47 }
48 } catch (Exception e) {
49 throw DbException.get(ErrorCode.UNSUPPORTED_COMPRESSION_OPTIONS_1, options);
50 }
51 }
52
53 public int compress(byte[] in, int inLen, byte[] out, int outPos) {
54 Deflater deflater = new Deflater(level);
55 deflater.setStrategy(strategy);
56 deflater.setInput(in, 0, inLen);
57 deflater.finish();
58 int compressed = deflater.deflate(out, outPos, out.length - outPos);
59 while (compressed == 0) {
60 // the compressed length is 0, meaning compression didn't work
61 // (sounds like a JDK bug)
62 // try again, using the default strategy and compression level
63 strategy = Deflater.DEFAULT_STRATEGY;
64 level = Deflater.DEFAULT_COMPRESSION;
65 return compress(in, inLen, out, outPos);
66 }
67 deflater.end();
68 return outPos + compressed;
69 }
70
71 public int getAlgorithm() {
72 return Compressor.DEFLATE;
73 }
74
75 public void expand(byte[] in, int inPos, int inLen, byte[] out, int outPos, int outLen) {
76 Inflater decompresser = new Inflater();
77 decompresser.setInput(in, inPos, inLen);
78 decompresser.finished();
79 try {
80 int len = decompresser.inflate(out, outPos, outLen);
81 if (len != outLen) {
82 throw new DataFormatException(len + " " + outLen);
83 }
84 } catch (DataFormatException e) {
85 throw DbException.get(ErrorCode.COMPRESSION_ERROR, e);
86 }
87 decompresser.end();
88 }
89
90 }
91