List:Commits« Previous MessageNext Message »
From:rburnett Date:June 1 2007 5:33pm
Subject:Connector/NET commit: r750 - trunk/Driver/Source
View as plain text  
Modified:
   trunk/Driver/Source/CompressedStream.cs
Log:
merged from 5.0

Modified: trunk/Driver/Source/CompressedStream.cs
===================================================================
--- trunk/Driver/Source/CompressedStream.cs	2007-06-01 15:28:02 UTC (rev 749)
+++ trunk/Driver/Source/CompressedStream.cs	2007-06-01 15:33:34 UTC (rev 750)
@@ -35,6 +35,8 @@
 
         // reading fields
         private byte[] localByte;
+        private byte[] inBuffer;
+        private WeakReference inBufferRef;
         private int inPos;
         private int maxInPos;
         private ZInputStream zInStream;
@@ -43,17 +45,12 @@
         {
             this.baseStream = baseStream;
             localByte = new byte[1];
-            cache = new MemoryStream();
+			cache = new MemoryStream();
+            inBufferRef = new WeakReference(inBuffer, false);
         }
 
         #region Properties
 
-        //TODO: remove comment
-/*		public Stream BaseStream 
-		{
-			get { return baseStream; }
-		}
-*/
 
         public override bool CanRead
         {
@@ -110,9 +107,8 @@
                 throw new ArgumentException(Resources.BufferNotLargeEnough, "buffer");
 
             if (inPos == maxInPos)
-            {
                 PrepareNextPacket();
-            }
+
             int countToRead = Math.Min(count, maxInPos - inPos);
             int countRead;
             if (zInStream != null)
@@ -120,6 +116,15 @@
             else
                 countRead = baseStream.Read(buffer, offset, countToRead);
             inPos += countRead;
+
+            // release the weak reference
+            if (inPos == maxInPos)
+            {
+                zInStream = null;
+                inBufferRef.Target = inBuffer;
+                inBuffer = null;
+            }
+
             return countRead;
         }
 
@@ -142,7 +147,9 @@
             }
             else
             {
-                zInStream = new ZInputStream(baseStream);
+                ReadNextPacket(compressedLength);
+                MemoryStream ms = new MemoryStream(inBuffer);
+                zInStream = new ZInputStream(ms);
                 zInStream.maxInput = compressedLength;
             }
 
@@ -150,6 +157,21 @@
             maxInPos = unCompressedLength;
         }
 
+        private void ReadNextPacket(int len)
+        {
+            inBuffer = (byte[])inBufferRef.Target;
+            if (inBuffer == null || inBuffer.Length < len)
+                inBuffer = new byte[len];
+            int numRead = 0;
+            int numToRead = len;
+            while (numToRead > 0)
+            {
+                int read = baseStream.Read(inBuffer, numRead, numToRead);
+                numRead += read;
+                numToRead -= read;
+            }
+        }
+
         private MemoryStream CompressCache()
         {
             // small arrays almost never yeild a benefit from compressing
@@ -246,4 +268,4 @@
             return baseStream.Seek(offset, origin);
         }
     }
-}
\ No newline at end of file
+}

Thread
Connector/NET commit: r750 - trunk/Driver/Sourcerburnett1 Jun