1 package org.apache.lucene.document;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.util.zip.Deflater;
21 import java.util.zip.Inflater;
22 import java.util.zip.DataFormatException;
23 import java.io.ByteArrayOutputStream;
24 import org.apache.lucene.util.UnicodeUtil;
26 /** Simple utility class providing static methods to
27 * compress and decompress binary data for stored fields.
28 * This class uses java.util.zip.Deflater and Inflater
29 * classes to compress and decompress.
32 public class CompressionTools {
34 // Export only static methods
35 private CompressionTools() {}
37 /** Compresses the specified byte range using the
38 * specified compressionLevel (constants are defined in
39 * java.util.zip.Deflater). */
40 public static byte[] compress(byte[] value, int offset, int length, int compressionLevel) {
42 /* Create an expandable byte array to hold the compressed data.
43 * You cannot use an array that's the same size as the orginal because
44 * there is no guarantee that the compressed data will be smaller than
45 * the uncompressed data. */
46 ByteArrayOutputStream bos = new ByteArrayOutputStream(length);
48 Deflater compressor = new Deflater();
51 compressor.setLevel(compressionLevel);
52 compressor.setInput(value, offset, length);
56 final byte[] buf = new byte[1024];
57 while (!compressor.finished()) {
58 int count = compressor.deflate(buf);
59 bos.write(buf, 0, count);
65 return bos.toByteArray();
68 /** Compresses the specified byte range, with default BEST_COMPRESSION level */
69 public static byte[] compress(byte[] value, int offset, int length) {
70 return compress(value, offset, length, Deflater.BEST_COMPRESSION);
73 /** Compresses all bytes in the array, with default BEST_COMPRESSION level */
74 public static byte[] compress(byte[] value) {
75 return compress(value, 0, value.length, Deflater.BEST_COMPRESSION);
78 /** Compresses the String value, with default BEST_COMPRESSION level */
79 public static byte[] compressString(String value) {
80 return compressString(value, Deflater.BEST_COMPRESSION);
83 /** Compresses the String value using the specified
84 * compressionLevel (constants are defined in
85 * java.util.zip.Deflater). */
86 public static byte[] compressString(String value, int compressionLevel) {
87 UnicodeUtil.UTF8Result result = new UnicodeUtil.UTF8Result();
88 UnicodeUtil.UTF16toUTF8(value, 0, value.length(), result);
89 return compress(result.result, 0, result.length, compressionLevel);
92 /** Decompress the byte array previously returned by
94 public static byte[] decompress(byte[] value) throws DataFormatException {
95 // Create an expandable byte array to hold the decompressed data
96 ByteArrayOutputStream bos = new ByteArrayOutputStream(value.length);
98 Inflater decompressor = new Inflater();
101 decompressor.setInput(value);
103 // Decompress the data
104 final byte[] buf = new byte[1024];
105 while (!decompressor.finished()) {
106 int count = decompressor.inflate(buf);
107 bos.write(buf, 0, count);
113 return bos.toByteArray();
116 /** Decompress the byte array previously returned by
117 * compressString back into a String */
118 public static String decompressString(byte[] value) throws DataFormatException {
119 UnicodeUtil.UTF16Result result = new UnicodeUtil.UTF16Result();
120 final byte[] bytes = decompress(value);
121 UnicodeUtil.UTF8toUTF16(bytes, 0, bytes.length, result);
122 return new String(result.result, 0, result.length);