Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
CompressionTools.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 
19 // To enable compression support in Lucene.Net ,
20 // you will need to define 'SHARP_ZIP_LIB' and reference the SharpLibZip
21 // library. The SharpLibZip library can be downloaded from:
22 // http://www.icsharpcode.net/OpenSource/SharpZipLib/
23 
24 using System;
25 using Lucene.Net.Support;
26 using UnicodeUtil = Lucene.Net.Util.UnicodeUtil;
27 
28 namespace Lucene.Net.Documents
29 {
30 
31  /// <summary>Simple utility class providing static methods to
32  /// compress and decompress binary data for stored fields.
33  /// This class uses java.util.zip.Deflater and Inflater
34  /// classes to compress and decompress.
35  /// </summary>
36 
37  public class CompressionTools
38  {
39 
40  // Export only static methods
41  private CompressionTools()
42  {
43  }
44 
45  /// <summary>Compresses the specified byte range using the
46  /// specified compressionLevel (constants are defined in
47  /// java.util.zip.Deflater).
48  /// </summary>
49  public static byte[] Compress(byte[] value_Renamed, int offset, int length, int compressionLevel)
50  {
51  /* Create an expandable byte array to hold the compressed data.
52  * You cannot use an array that's the same size as the orginal because
53  * there is no guarantee that the compressed data will be smaller than
54  * the uncompressed data. */
55  System.IO.MemoryStream bos = new System.IO.MemoryStream(length);
56 
57  Deflater compressor = SharpZipLib.CreateDeflater();
58 
59  try
60  {
61  compressor.SetLevel(compressionLevel);
62  compressor.SetInput(value_Renamed, offset, length);
63  compressor.Finish();
64 
65  // Compress the data
66  byte[] buf = new byte[1024];
67  while (!compressor.IsFinished)
68  {
69  int count = compressor.Deflate(buf);
70  bos.Write(buf, 0, count);
71  }
72  }
73  finally
74  {
75  }
76 
77  return bos.ToArray();
78  }
79 
80  /// <summary>Compresses the specified byte range, with default BEST_COMPRESSION level </summary>
81  public static byte[] Compress(byte[] value_Renamed, int offset, int length)
82  {
83  return Compress(value_Renamed, offset, length, Deflater.BEST_COMPRESSION);
84  }
85 
86  /// <summary>Compresses all bytes in the array, with default BEST_COMPRESSION level </summary>
87  public static byte[] Compress(byte[] value_Renamed)
88  {
89  return Compress(value_Renamed, 0, value_Renamed.Length, Deflater.BEST_COMPRESSION);
90  }
91 
92  /// <summary>Compresses the String value, with default BEST_COMPRESSION level </summary>
93  public static byte[] CompressString(System.String value_Renamed)
94  {
95  return CompressString(value_Renamed, Deflater.BEST_COMPRESSION);
96  }
97 
98  /// <summary>Compresses the String value using the specified
99  /// compressionLevel (constants are defined in
100  /// java.util.zip.Deflater).
101  /// </summary>
102  public static byte[] CompressString(System.String value_Renamed, int compressionLevel)
103  {
104  UnicodeUtil.UTF8Result result = new UnicodeUtil.UTF8Result();
105  UnicodeUtil.UTF16toUTF8(value_Renamed, 0, value_Renamed.Length, result);
106  return Compress(result.result, 0, result.length, compressionLevel);
107  }
108 
109  /// <summary>Decompress the byte array previously returned by
110  /// compress
111  /// </summary>
112  public static byte[] Decompress(byte[] value_Renamed)
113  {
114  // Create an expandable byte array to hold the decompressed data
115  System.IO.MemoryStream bos = new System.IO.MemoryStream(value_Renamed.Length);
116 
117  Inflater decompressor = SharpZipLib.CreateInflater();
118 
119  try
120  {
121  decompressor.SetInput(value_Renamed);
122 
123  // Decompress the data
124  byte[] buf = new byte[1024];
125  while (!decompressor.IsFinished)
126  {
127  int count = decompressor.Inflate(buf);
128  bos.Write(buf, 0, count);
129  }
130  }
131  finally
132  {
133  }
134 
135  return bos.ToArray();
136  }
137 
138  /// <summary>Decompress the byte array previously returned by
139  /// compressString back into a String
140  /// </summary>
141  public static System.String DecompressString(byte[] value_Renamed)
142  {
143  UnicodeUtil.UTF16Result result = new UnicodeUtil.UTF16Result();
144  byte[] bytes = Decompress(value_Renamed);
145  UnicodeUtil.UTF8toUTF16(bytes, 0, bytes.Length, result);
146  return new System.String(result.result, 0, result.length);
147  }
148  }
149 }
150