You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by di...@apache.org on 2009/12/07 12:09:21 UTC

svn commit: r887884 - in /incubator/lucene.net/trunk/C#/src/Lucene.Net: Document/CompressionTools.cs SupportClass.cs

Author: digy
Date: Mon Dec  7 11:09:20 2009
New Revision: 887884

URL: http://svn.apache.org/viewvc?rev=887884&view=rev
Log:
LUCENENET-321 Compression support via reflection

Modified:
    incubator/lucene.net/trunk/C#/src/Lucene.Net/Document/CompressionTools.cs
    incubator/lucene.net/trunk/C#/src/Lucene.Net/SupportClass.cs

Modified: incubator/lucene.net/trunk/C#/src/Lucene.Net/Document/CompressionTools.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Document/CompressionTools.cs?rev=887884&r1=887883&r2=887884&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/Document/CompressionTools.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/Document/CompressionTools.cs Mon Dec  7 11:09:20 2009
@@ -23,11 +23,6 @@
 
 using System;
 
-#if SHARP_ZIP_LIB
-using ICSharpCode.SharpZipLib;
-using ICSharpCode.SharpZipLib.Zip.Compression;
-#endif
-
 using UnicodeUtil = Lucene.Net.Util.UnicodeUtil;
 
 namespace Lucene.Net.Documents
@@ -55,14 +50,13 @@
 		/// </summary>
 		public static byte[] Compress(byte[] value_Renamed, int offset, int length, int compressionLevel)
 		{
-			#if SHARP_ZIP_LIB
 			/* Create an expandable byte array to hold the compressed data.
 			* You cannot use an array that's the same size as the orginal because
 			* there is no guarantee that the compressed data will be smaller than
 			* the uncompressed data. */
 			System.IO.MemoryStream bos = new System.IO.MemoryStream(length);
-			
-			Deflater compressor = new Deflater();
+
+            SupportClass.SharpZipLib.Deflater compressor = SupportClass.SharpZipLib.CreateDeflater();
 			
 			try
 			{
@@ -83,40 +77,24 @@
 			}
 			
 			return bos.ToArray();
-            #else
-            throw new System.SystemException("Compression support not configured"); 
-            #endif
 		}
 		
 		/// <summary>Compresses the specified byte range, with default BEST_COMPRESSION level </summary>
 		public static byte[] Compress(byte[] value_Renamed, int offset, int length)
         {
-            #if SHARP_ZIP_LIB
-			return Compress(value_Renamed, offset, length, Deflater.BEST_COMPRESSION);
-            #else
-            throw new System.SystemException("Compression support not configured"); 
-            #endif
+			return Compress(value_Renamed, offset, length, SupportClass.SharpZipLib.Deflater.BEST_COMPRESSION);
 		}
 		
 		/// <summary>Compresses all bytes in the array, with default BEST_COMPRESSION level </summary>
 		public static byte[] Compress(byte[] value_Renamed)
 		{
-            #if SHARP_ZIP_LIB
-			return Compress(value_Renamed, 0, value_Renamed.Length, Deflater.BEST_COMPRESSION);
-            #else
-            throw new System.SystemException("Compression support not configured"); 
-            #endif
+            return Compress(value_Renamed, 0, value_Renamed.Length, SupportClass.SharpZipLib.Deflater.BEST_COMPRESSION);
 		}
 		
 		/// <summary>Compresses the String value, with default BEST_COMPRESSION level </summary>
 		public static byte[] CompressString(System.String value_Renamed)
 		{
-			
-            #if SHARP_ZIP_LIB
-			return CompressString(value_Renamed, Deflater.BEST_COMPRESSION);
-            #else
-            throw new System.SystemException("Compression support not configured"); 
-            #endif
+            return CompressString(value_Renamed, SupportClass.SharpZipLib.Deflater.BEST_COMPRESSION);
 		}
 		
 		/// <summary>Compresses the String value using the specified
@@ -135,11 +113,10 @@
 		/// </summary>
 		public static byte[] Decompress(byte[] value_Renamed)
 		{
-            #if SHARP_ZIP_LIB
 			// Create an expandable byte array to hold the decompressed data
 			System.IO.MemoryStream bos = new System.IO.MemoryStream(value_Renamed.Length);
 			
-			Inflater decompressor = new Inflater();
+			SupportClass.SharpZipLib.Inflater decompressor = SupportClass.SharpZipLib.CreateInflater();
 			
 			try
 			{
@@ -158,9 +135,6 @@
 			}
 			
 			return bos.ToArray();
-            #else
-            throw new System.SystemException("Compression support not configured"); 
-            #endif
 		}
 		
 		/// <summary>Decompress the byte array previously returned by

Modified: incubator/lucene.net/trunk/C#/src/Lucene.Net/SupportClass.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/SupportClass.cs?rev=887884&r1=887883&r2=887884&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/SupportClass.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/SupportClass.cs Mon Dec  7 11:09:20 2009
@@ -2130,4 +2130,105 @@
 			get { return isWindows; }
 		}
 	}
+
+    public class SharpZipLib
+    {
+        static System.Reflection.Assembly asm = null;
+
+        static SharpZipLib()
+        {
+            try
+            {
+                asm = System.Reflection.Assembly.Load("ICSharpCode.SharpZipLib");
+            }
+            catch{}
+        }
+
+        public static Deflater CreateDeflater()
+        {
+            if (asm == null) throw new System.IO.FileNotFoundException("Can not load ICSharpCode.SharpZipLib.dll"); 
+            return new Deflater(asm.CreateInstance("ICSharpCode.SharpZipLib.Zip.Compression.Deflater"));
+        }
+
+        public static Inflater CreateInflater()
+        {
+            if (asm == null) throw new System.IO.FileNotFoundException("Can not load ICSharpCode.SharpZipLib.dll");
+            return new Inflater(asm.CreateInstance("ICSharpCode.SharpZipLib.Zip.Compression.Inflater"));
+        }
+
+
+        public class Inflater
+        {
+            object inflaterInstance = null;
+            Type inflaterType;
+
+            internal Inflater(object inflaterInstance)
+            {
+                this.inflaterInstance = inflaterInstance;
+                this.inflaterType = inflaterInstance.GetType();
+            }
+
+            public void SetInput(byte[] buffer)
+            {
+                inflaterType.InvokeMember("SetInput", System.Reflection.BindingFlags.InvokeMethod, null, inflaterInstance, new object[] { buffer });
+            }
+
+            public bool IsFinished
+            {
+                get
+                {
+                    return (bool)inflaterType.InvokeMember("get_IsFinished", System.Reflection.BindingFlags.InvokeMethod, null, inflaterInstance, null);
+                }
+            }
+
+            public int Inflate(byte[] buffer)
+            {
+                return (int)inflaterType.InvokeMember("Inflate", System.Reflection.BindingFlags.InvokeMethod, null, inflaterInstance, new object[] { buffer });                
+            }
+        }
+
+
+        public class Deflater 
+        {
+            public const int BEST_COMPRESSION = 9;
+
+            object deflaterInstance = null;
+            Type deflaterType;
+
+            internal Deflater(object deflaterInstance)
+            {
+                this.deflaterInstance = deflaterInstance;
+                this.deflaterType = deflaterInstance.GetType();
+            }
+            
+            public void SetLevel(int level)
+            {
+                deflaterType.InvokeMember("SetLevel", System.Reflection.BindingFlags.InvokeMethod, null, deflaterInstance, new object[] { level });
+            }
+
+            public void SetInput(byte[] input, int offset, int count)
+            {
+                deflaterType.InvokeMember("SetInput", System.Reflection.BindingFlags.InvokeMethod, null, deflaterInstance, new object[] { input,offset,count });
+            }
+
+            public void Finish()
+            {
+                deflaterType.InvokeMember("Finish", System.Reflection.BindingFlags.InvokeMethod, null, deflaterInstance, null);
+            }
+
+            public bool IsFinished
+            {
+                get
+                {
+                    return (bool)deflaterType.InvokeMember("get_IsFinished", System.Reflection.BindingFlags.InvokeMethod, null, deflaterInstance, null); ;
+                }
+            }
+
+            public int Deflate(byte[] output)
+            {
+                return (int)deflaterType.InvokeMember("Deflate", System.Reflection.BindingFlags.InvokeMethod, null, deflaterInstance, new object[] { output });
+            }
+        }
+
+    }
 }