2004-11-09 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System / System.Net / ChunkStream.cs
index 7929d98c21798e13e3e166bbaf6673ce44ec220a..3d530991a65496a419ff70201e135df6a7fb9e26 100644 (file)
@@ -7,6 +7,28 @@
 // (C) 2003 Ximian, Inc (http://www.ximian.com)
 //
 
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System.Collections;
 using System.Globalization;
 using System.IO;
 using System.Text;
@@ -22,7 +44,24 @@ namespace System.Net
                        Trailer
                }
 
-               MemoryStream ms;
+               class Chunk {
+                       public byte [] Bytes;
+                       public int Offset;
+
+                       public Chunk (byte [] chunk)
+                       {
+                               this.Bytes = chunk;
+                       }
+
+                       public int Read (byte [] buffer, int offset, int size)
+                       {
+                               int nread = (size > Bytes.Length - Offset) ? Bytes.Length - Offset : size;
+                               Buffer.BlockCopy (Bytes, Offset, buffer, offset, nread);
+                               Offset += nread;
+                               return nread;
+                       }
+               }
+
                WebHeaderCollection headers;
                int chunkSize;
                int chunkRead;
@@ -31,41 +70,57 @@ namespace System.Net
                StringBuilder saved;
                bool sawCR;
                bool gotit;
-               long readPosition;
+               ArrayList chunks;
                
                public ChunkStream (byte [] buffer, int offset, int size, WebHeaderCollection headers)
                {
                        this.headers = headers;
-                       ms = new MemoryStream ();
                        saved = new StringBuilder ();
+                       chunks = new ArrayList ();
                        chunkSize = -1;
-                       if (offset < size)
-                               Write (buffer, offset, size);
+                       Write (buffer, offset, size);
                }
 
                public void ResetBuffer ()
                {
-                       ms.SetLength (0);
-                       readPosition = 0;
                        chunkSize = -1;
                        chunkRead = 0;
+                       chunks.Clear ();
                }
                
                public void WriteAndReadBack (byte [] buffer, int offset, int size, ref int read)
                {
-                       Write (buffer, offset, read);
+                       Write (buffer, offset, offset+read);
                        read = Read (buffer, offset, size);
                }
 
                public int Read (byte [] buffer, int offset, int size)
                {
-                       ms.Position = readPosition;
-                       int r = ms.Read (buffer, offset, size);
-                       readPosition += r;
-                       ms.Position = ms.Length;
-                       return r;
+                       return ReadFromChunks (buffer, offset, size);
                }
 
+               int ReadFromChunks (byte [] buffer, int offset, int size)
+               {
+                       int count = chunks.Count;
+                       int nread = 0;
+                       for (int i = 0; i < count; i++) {
+                               Chunk chunk = (Chunk) chunks [i];
+                               if (chunk == null)
+                                       continue;
+
+                               if (chunk.Offset == chunk.Bytes.Length) {
+                                       chunks [i] = null;
+                                       continue;
+                               }
+                               
+                               nread += chunk.Read (buffer, offset + nread, size - nread);
+                               if (nread == size)
+                                       break;
+                       }
+
+                       return nread;
+               }
+               
                public void Write (byte [] buffer, int offset, int size)
                {
                        InternalWrite (buffer, ref offset, size);
@@ -112,15 +167,11 @@ namespace System.Net
                }
 
                public bool WantMore {
-                       get { return (chunkRead != chunkSize || chunkSize != 0); }
-               }
-               
-               public bool EOF {
-                       get { return (Available == 0); }
+                       get { return (chunkRead != chunkSize || chunkSize != 0 || state != State.None); }
                }
-               
-               public int Available {
-                       get { return (int) (ms.Length - readPosition); }
+
+               public int ChunkLeft {
+                       get { return chunkSize - chunkRead; }
                }
                
                State ReadBody (byte [] buffer, ref int offset, int size)
@@ -132,7 +183,9 @@ namespace System.Net
                        if (diff + chunkRead > chunkSize)
                                diff = chunkSize - chunkRead;
 
-                       ms.Write (buffer, offset, diff);
+                       byte [] chunk = new byte [diff];
+                       Buffer.BlockCopy (buffer, offset, chunk, 0, diff);
+                       chunks.Add (new Chunk (chunk));
                        offset += diff;
                        chunkRead += diff;
                        return (chunkRead == chunkSize) ? State.BodyFinished : State.Body;