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
LZOCompressor.cs
Go to the documentation of this file.
1 /*
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements. See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership. The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with 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,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied. See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20 */
21 
22 // LZO.Net
23 // $Id: LZOCompressor.cs,v 1.1 2004/02/22 17:44:04 laptop Exp $
24 
25 namespace Simplicit.Net.Lzo {
26  using System;
27  using System.Diagnostics;
28  using System.Runtime.InteropServices;
29 
30  /// <summary>
31  /// Wrapper class for the highly performant LZO compression library
32  /// </summary>
33  public class LZOCompressor {
34  private static TraceSwitch _traceSwitch = new TraceSwitch("Simplicit.Net.Lzo", "Switch for tracing of the LZOCompressor-Class");
35 
36  #region Dll-Imports
37  [DllImport("lzo.dll")]
38  private static extern int __lzo_init3();
39  [DllImport("lzo.dll")]
40  private static extern string lzo_version_string();
41  [DllImport("lzo.dll")]
42  private static extern string lzo_version_date();
43  [DllImport("lzo.dll")]
44  private static extern int lzo1x_1_compress(
45  byte[] src,
46  int src_len,
47  byte[] dst,
48  ref int dst_len,
49  byte[] wrkmem
50  );
51  [DllImport("lzo.dll")]
52  private static extern int lzo1x_decompress(
53  byte[] src,
54  int src_len,
55  byte[] dst,
56  ref int dst_len,
57  byte[] wrkmem);
58  #endregion
59 
60  private byte[] _workMemory = new byte[16384L * 4];
61 
62  static LZOCompressor() {
63  int init = __lzo_init3();
64  if(init != 0) {
65  throw new Exception("Initialization of LZO-Compressor failed !");
66  }
67  }
68 
69  /// <summary>
70  /// Constructor.
71  /// </summary>
72  public LZOCompressor() {
73  }
74 
75  /// <summary>
76  /// Version string of the compression library.
77  /// </summary>
78  public string Version {
79  get {
80  return lzo_version_string();
81  }
82  }
83 
84  /// <summary>
85  /// Version date of the compression library
86  /// </summary>
87  public string VersionDate {
88  get {
89  return lzo_version_date();
90  }
91  }
92 
93  /// <summary>
94  /// Compresses a byte array and returns the compressed data in a new
95  /// array. You need the original length of the array to decompress it.
96  /// </summary>
97  /// <param name="src">Source array for compression</param>
98  /// <returns>Byte array containing the compressed data</returns>
99  public byte[] Compress(byte[] src) {
100  if(_traceSwitch.TraceVerbose) {
101  Trace.WriteLine(String.Format("LZOCompressor: trying to compress {0}", src.Length));
102  }
103  byte[] dst = new byte[src.Length + src.Length / 64 + 16 + 3 + 4];
104  int outlen = 0;
105  lzo1x_1_compress(src, src.Length, dst, ref outlen, _workMemory);
106  if(_traceSwitch.TraceVerbose) {
107  Trace.WriteLine(String.Format("LZOCompressor: compressed {0} to {1} bytes", src.Length, outlen));
108  }
109  byte[] ret = new byte[outlen + 4];
110  Array.Copy(dst, 0, ret, 0, outlen);
111  byte[] outlenarr = BitConverter.GetBytes(src.Length);
112  Array.Copy(outlenarr, 0, ret, outlen, 4);
113  return ret;
114  }
115 
116  /// <summary>
117  /// Decompresses compressed data to its original state.
118  /// </summary>
119  /// <param name="src">Source array to be decompressed</param>
120  /// <returns>Decompressed data</returns>
121  public byte[] Decompress(byte[] src) {
122  if(_traceSwitch.TraceVerbose) {
123  Trace.WriteLine(String.Format("LZOCompressor: trying to decompress {0}", src.Length));
124  }
125  int origlen = BitConverter.ToInt32(src, src.Length - 4);
126  byte[] dst = new byte[origlen];
127  int outlen = origlen;
128  lzo1x_decompress(src, src.Length - 4, dst, ref outlen, _workMemory);
129  if(_traceSwitch.TraceVerbose) {
130  Trace.WriteLine(String.Format("LZOCompressor: decompressed {0} to {1} bytes", src.Length, origlen));
131  }
132  return dst;
133  }
134  }
135 }