Merge pull request #1949 from lewurm/fixtype
[mono.git] / mcs / class / System / System.Net / ChunkStream.cs
index b3888fb1581c24f417e505c54c4423411b41c185..b7cc92386f56e22d45233c602cf995a8198daf1a 100644 (file)
@@ -29,6 +29,7 @@
 //
 
 using System.Collections;
+using System.Collections.Generic;
 using System.Globalization;
 using System.IO;
 using System.Text;
@@ -39,6 +40,7 @@ namespace System.Net
        {
                enum State {
                        None,
+                       PartialSize,
                        Body,
                        BodyFinished,
                        Trailer
@@ -65,6 +67,7 @@ namespace System.Net
                internal WebHeaderCollection headers;
                int chunkSize;
                int chunkRead;
+               int totalWritten;
                State state;
                //byte [] waitBuffer;
                StringBuilder saved;
@@ -85,12 +88,14 @@ namespace System.Net
                        saved = new StringBuilder ();
                        chunks = new ArrayList ();
                        chunkSize = -1;
+                       totalWritten = 0;
                }
 
                public void ResetBuffer ()
                {
                        chunkSize = -1;
                        chunkRead = 0;
+                       totalWritten = 0;
                        chunks.Clear ();
                }
                
@@ -110,13 +115,13 @@ namespace System.Net
                {
                        int count = chunks.Count;
                        int nread = 0;
+
+                       var chunksForRemoving = new List<Chunk>(count);
                        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;
+                                       chunksForRemoving.Add(chunk);
                                        continue;
                                }
                                
@@ -125,6 +130,9 @@ namespace System.Net
                                        break;
                        }
 
+                       foreach (var chunk in chunksForRemoving)
+                               chunks.Remove(chunk);
+
                        return nread;
                }
                
@@ -136,9 +144,9 @@ namespace System.Net
                
                void InternalWrite (byte [] buffer, ref int offset, int size)
                {
-                       if (state == State.None) {
+                       if (state == State.None || state == State.PartialSize) {
                                state = GetChunkSize (buffer, ref offset, size);
-                               if (state == State.None)
+                               if (state == State.PartialSize)
                                        return;
                                
                                saved.Length = 0;
@@ -192,6 +200,10 @@ namespace System.Net
                        }
                }
 
+               public int TotalDataSize {
+                       get { return totalWritten; }
+               }
+
                public int ChunkLeft {
                        get { return chunkSize - chunkRead; }
                }
@@ -210,6 +222,7 @@ namespace System.Net
                        chunks.Add (new Chunk (chunk));
                        offset += diff;
                        chunkRead += diff;
+                       totalWritten += diff;
                        return (chunkRead == chunkSize) ? State.BodyFinished : State.Body;
                                
                }
@@ -254,7 +267,7 @@ namespace System.Net
                                        ThrowProtocolViolation ("Cannot parse chunk size.");
                                }
 
-                               return State.None;
+                               return State.PartialSize;
                        }
 
                        chunkRead = 0;