Remove debug spew
[beagle.git] / beagled / Lucene.Net / SharpZipLibAdapter.cs
blobc5b7d2721e1c3aadbd65bc6f5340cdf39fe59b3b
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /// To enable compression support in Lucene.Net 1.9 using .NET 1.1. Framework,
18 /// you will need to define 'SHARP_ZIP_LIB' and referance the SharpLibZip
19 /// library. The SharpLibZip library can be downloaded from:
20 /// http://www.icsharpcode.net/OpenSource/SharpZipLib/
21 ///
22 /// You can use any other cmpression library you have by plugging it into this
23 /// code or by providing your own adapter as in this one.
26 #if SHARP_ZIP_LIB
28 using System;
30 using ICSharpCode.SharpZipLib;
31 using ICSharpCode.SharpZipLib.Zip.Compression;
32 using System.IO;
34 namespace Lucene.Net.Index.Compression
36 public class SharpZipLibAdapter : SupportClass.CompressionSupport.ICompressionAdapter
38 public byte[] Compress(byte[] input)
40 // Create the compressor with highest level of compression
41 Deflater compressor = new Deflater();
42 compressor.SetLevel(Deflater.BEST_COMPRESSION);
44 // Give the compressor the data to compress
45 compressor.SetInput(input);
46 compressor.Finish();
49 * Create an expandable byte array to hold the compressed data.
50 * You cannot use an array that's the same size as the orginal because
51 * there is no guarantee that the compressed data will be smaller than
52 * the uncompressed data.
54 MemoryStream bos = new MemoryStream(input.Length);
56 // Compress the data
57 byte[] buf = new byte[1024];
58 while (!compressor.IsFinished)
60 int count = compressor.Deflate(buf);
61 bos.Write(buf, 0, count);
64 // Get the compressed data
65 return bos.ToArray();
68 public byte[] Uncompress(byte[] input)
70 Inflater decompressor = new Inflater();
71 decompressor.SetInput(input);
73 // Create an expandable byte array to hold the decompressed data
74 MemoryStream bos = new MemoryStream(input.Length);
76 // Decompress the data
77 byte[] buf = new byte[1024];
78 while (!decompressor.IsFinished)
80 int count = decompressor.Inflate(buf);
81 bos.Write(buf, 0, count);
84 // Get the decompressed data
85 return bos.ToArray();
90 #endif