~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/src/java/org/apache/lucene/document/CompressionTools.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.document;
2
 
 
3
 
/**
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
10
 
 *
11
 
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 
 *
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.
18
 
 */
19
 
 
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;
25
 
 
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.
30
 
 */
31
 
 
32
 
public class CompressionTools {
33
 
 
34
 
  // Export only static methods
35
 
  private CompressionTools() {}
36
 
 
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) {
41
 
 
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);
47
 
 
48
 
    Deflater compressor = new Deflater();
49
 
 
50
 
    try {
51
 
      compressor.setLevel(compressionLevel);
52
 
      compressor.setInput(value, offset, length);
53
 
      compressor.finish();
54
 
 
55
 
      // Compress the data
56
 
      final byte[] buf = new byte[1024];
57
 
      while (!compressor.finished()) {
58
 
        int count = compressor.deflate(buf);
59
 
        bos.write(buf, 0, count);
60
 
      }
61
 
    } finally {
62
 
      compressor.end();
63
 
    }
64
 
 
65
 
    return bos.toByteArray();
66
 
  }
67
 
 
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);
71
 
  }
72
 
  
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);
76
 
  }
77
 
 
78
 
  /** Compresses the String value, with default BEST_COMPRESSION level */
79
 
  public static byte[] compressString(String value) {
80
 
    return compressString(value, Deflater.BEST_COMPRESSION);
81
 
  }
82
 
 
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);
90
 
  }
91
 
 
92
 
  /** Decompress the byte array previously returned by
93
 
   *  compress */
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);
97
 
 
98
 
    Inflater decompressor = new Inflater();
99
 
 
100
 
    try {
101
 
      decompressor.setInput(value);
102
 
 
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);
108
 
      }
109
 
    } finally {  
110
 
      decompressor.end();
111
 
    }
112
 
    
113
 
    return bos.toByteArray();
114
 
  }
115
 
 
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);
123
 
  }
124
 
}