2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.XML / System.Xml / XmlInputStream.cs
index 007bff3c6b0502bedd50901706811048620e03be..e60951cd38e85961d6e16e68ad55e2230725ce9e 100644 (file)
@@ -7,6 +7,27 @@
 //
 //     (C)2003 Atsushi Enomoto
 //
+
+//
+// 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;
 using System.IO;
 using System.Text;
@@ -63,7 +84,7 @@ namespace System.Xml
 
                private void Initialize (Stream stream)
                {
-                       buffer = new byte [1024];
+                       buffer = new byte [64];
                        this.stream = stream;
                        enc = Encoding.UTF8; // Default to UTF8 if we can't guess it
                        bufLength = stream.Read (buffer, 0, buffer.Length);
@@ -109,15 +130,14 @@ namespace System.Xml
                                // try to get encoding name from XMLDecl.
                                if (bufLength >= 5 && Encoding.ASCII.GetString (buffer, 1, 4) == "?xml") {
                                        bufPos += 4;
-                                       int loop = 0;
                                        c = SkipWhitespace ();
 
                                        // version. It is optional here.
                                        if (c == 'v') {
-                                               while (loop++ >= 0 && c >= 0) {
-                                                       ReadByteSpecial ();
+                                               while (c >= 0) {
                                                        c = ReadByteSpecial ();
                                                        if (c == '0') { // 0 of 1.0
+                                                               ReadByteSpecial ();
                                                                break;
                                                        }
                                                }
@@ -126,7 +146,7 @@ namespace System.Xml
 
                                        if (c == 'e') {
                                                int remaining = bufLength - bufPos;
-                                               if (remaining >= 7 && Encoding.ASCII.GetString(buffer, 0, 7) == "ncoding") {
+                                               if (remaining >= 7 && Encoding.ASCII.GetString(buffer, bufPos, 7) == "ncoding") {
                                                        bufPos += 7;
                                                        c = SkipWhitespace();
                                                        if (c != '=')
@@ -134,7 +154,7 @@ namespace System.Xml
                                                        c = SkipWhitespace ();
                                                        int quoteChar = c;
                                                        StringBuilder sb = new StringBuilder ();
-                                                       while (loop++ >= 0) {
+                                                       while (true) {
                                                                c = ReadByteSpecial ();
                                                                if (c == quoteChar)
                                                                        break;
@@ -176,13 +196,12 @@ namespace System.Xml
                }
 
                // skips whitespace and returns misc char that was read from stream
-               private int SkipWhitespace ()   // ms may be null
+               private int SkipWhitespace ()
                {
-                       int loop = 0;
                        int c;
-                       while (loop++ >= 0) { // defends infinite loop (expecting overflow)
+                       while (true) {
                                c = ReadByteSpecial ();
-                               switch (c) {
+                               switch ((char) c) {
                                case '\r': goto case ' ';
                                case '\n': goto case ' ';
                                case '\t': goto case ' ';
@@ -250,13 +269,13 @@ namespace System.Xml
                {
                        int ret;
                        if (count <= bufLength - bufPos)        {       // all from buffer
-                               Array.Copy (this.buffer, bufPos, buffer, offset, count);
+                               Buffer.BlockCopy (this.buffer, bufPos, buffer, offset, count);
                                bufPos += count;
                                ret = count;
                        } else {
                                int bufRest = bufLength - bufPos;
                                if (bufLength > bufPos) {
-                                       Array.Copy (this.buffer, bufPos, buffer, offset, bufRest);
+                                       Buffer.BlockCopy (this.buffer, bufPos, buffer, offset, bufRest);
                                        bufPos += bufRest;
                                }
                                ret = bufRest +