ArrayHelper.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / Serialization / System / Xml / ArrayHelper.cs / 1305376 / ArrayHelper.cs

                            //------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------
namespace System.Xml
{ 
    using System;
 
    abstract class ArrayHelper 
    {
        public TArray[] ReadArray(XmlDictionaryReader reader, TArgument localName, TArgument namespaceUri, int maxArrayLength) 
        {
            TArray[][] arrays = null;
            TArray[] array = null;
            int arrayCount = 0; 
            int totalRead = 0;
            int count; 
            if (reader.TryGetArrayLength(out count)) 
            {
                if (count > maxArrayLength) 
                    XmlExceptionHelper.ThrowMaxArrayLengthOrMaxItemsQuotaExceeded(reader, maxArrayLength);

                if (count > XmlDictionaryReader.MaxInitialArrayLength)
                    count = XmlDictionaryReader.MaxInitialArrayLength; 
            }
            else 
            { 
                count = 32;
            } 
            while (true)
            {
                array = new TArray[count];
                int read = 0; 
                while (read < array.Length)
                { 
                    int actual = ReadArray(reader, localName, namespaceUri, array, read, array.Length - read); 
                    if (actual == 0)
                        break; 
                    read += actual;
                }
                if (totalRead > maxArrayLength - read)
                    XmlExceptionHelper.ThrowMaxArrayLengthOrMaxItemsQuotaExceeded(reader, maxArrayLength); 
                totalRead += read;
                if (read < array.Length || reader.NodeType == XmlNodeType.EndElement) 
                    break; 
                if (arrays == null)
                    arrays = new TArray[32][]; 
                arrays[arrayCount++] = array;
                count = count * 2;
            }
            if (totalRead != array.Length || arrayCount > 0) 
            {
                TArray[] newArray = new TArray[totalRead]; 
                int offset = 0; 
                for (int i = 0; i < arrayCount; i++)
                { 
                    Array.Copy(arrays[i], 0, newArray, offset, arrays[i].Length);
                    offset += arrays[i].Length;
                }
                Array.Copy(array, 0, newArray, offset, totalRead - offset); 
                array = newArray;
            } 
            return array; 
        }
 
        public void WriteArray(XmlDictionaryWriter writer, string prefix, TArgument localName, TArgument namespaceUri, XmlDictionaryReader reader)
        {
            int count;
            if (reader.TryGetArrayLength(out count)) 
                count = Math.Min(count, 256);
            else 
                count = 256; 
            TArray[] array = new TArray[count];
            while (true) 
            {
                int actual = ReadArray(reader, localName, namespaceUri, array, 0, array.Length);
                if (actual == 0)
                    break; 
                WriteArray(writer, prefix, localName, namespaceUri, array, 0, actual);
            } 
        } 

        protected abstract int ReadArray(XmlDictionaryReader reader, TArgument localName, TArgument namespaceUri, TArray[] array, int offset, int count); 
        protected abstract void WriteArray(XmlDictionaryWriter writer, string prefix, TArgument localName, TArgument namespaceUri, TArray[] array, int offset, int count);
    }

    // Supported array types 
    // bool
    // Int16 
    // Int32 
    // Int64
    // Float 
    // Double
    // Decimal
    // DateTime
    // Guid 
    // TimeSpan
 
    // Int8 is not supported since sbyte[] is non-cls compliant, and uncommon 
    // UniqueId is not supported since elements may be variable size strings
 
    class BooleanArrayHelperWithString : ArrayHelper
    {
        static public readonly BooleanArrayHelperWithString Instance = new BooleanArrayHelperWithString();
 
        protected override int ReadArray(XmlDictionaryReader reader, string localName, string namespaceUri, bool[] array, int offset, int count)
        { 
            return reader.ReadArray(localName, namespaceUri, array, offset, count); 
        }
 
        protected override void WriteArray(XmlDictionaryWriter writer, string prefix, string localName, string namespaceUri, bool[] array, int offset, int count)
        {
            writer.WriteArray(prefix, localName, namespaceUri, array, offset, count);
        } 
    }
 
    class BooleanArrayHelperWithDictionaryString : ArrayHelper 
    {
        static public readonly BooleanArrayHelperWithDictionaryString Instance = new BooleanArrayHelperWithDictionaryString(); 

        protected override int ReadArray(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString namespaceUri, bool[] array, int offset, int count)
        {
            return reader.ReadArray(localName, namespaceUri, array, offset, count); 
        }
 
        protected override void WriteArray(XmlDictionaryWriter writer, string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, bool[] array, int offset, int count) 
        {
            writer.WriteArray(prefix, localName, namespaceUri, array, offset, count); 
        }
    }

    class Int16ArrayHelperWithString : ArrayHelper 
    {
        static public readonly Int16ArrayHelperWithString Instance = new Int16ArrayHelperWithString(); 
 
        protected override int ReadArray(XmlDictionaryReader reader, string localName, string namespaceUri, Int16[] array, int offset, int count)
        { 
            return reader.ReadArray(localName, namespaceUri, array, offset, count);
        }

        protected override void WriteArray(XmlDictionaryWriter writer, string prefix, string localName, string namespaceUri, Int16[] array, int offset, int count) 
        {
            writer.WriteArray(prefix, localName, namespaceUri, array, offset, count); 
        } 
    }
 
    class Int16ArrayHelperWithDictionaryString : ArrayHelper
    {
        static public readonly Int16ArrayHelperWithDictionaryString Instance = new Int16ArrayHelperWithDictionaryString();
 
        protected override int ReadArray(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString namespaceUri, Int16[] array, int offset, int count)
        { 
            return reader.ReadArray(localName, namespaceUri, array, offset, count); 
        }
 
        protected override void WriteArray(XmlDictionaryWriter writer, string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, Int16[] array, int offset, int count)
        {
            writer.WriteArray(prefix, localName, namespaceUri, array, offset, count);
        } 
    }
 
    class Int32ArrayHelperWithString : ArrayHelper 
    {
        static public readonly Int32ArrayHelperWithString Instance = new Int32ArrayHelperWithString(); 

        protected override int ReadArray(XmlDictionaryReader reader, string localName, string namespaceUri, Int32[] array, int offset, int count)
        {
            return reader.ReadArray(localName, namespaceUri, array, offset, count); 
        }
 
        protected override void WriteArray(XmlDictionaryWriter writer, string prefix, string localName, string namespaceUri, Int32[] array, int offset, int count) 
        {
            writer.WriteArray(prefix, localName, namespaceUri, array, offset, count); 
        }
    }

    class Int32ArrayHelperWithDictionaryString : ArrayHelper 
    {
        static public readonly Int32ArrayHelperWithDictionaryString Instance = new Int32ArrayHelperWithDictionaryString(); 
 
        protected override int ReadArray(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString namespaceUri, Int32[] array, int offset, int count)
        { 
            return reader.ReadArray(localName, namespaceUri, array, offset, count);
        }

        protected override void WriteArray(XmlDictionaryWriter writer, string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, Int32[] array, int offset, int count) 
        {
            writer.WriteArray(prefix, localName, namespaceUri, array, offset, count); 
        } 
    }
 
    class Int64ArrayHelperWithString : ArrayHelper
    {
        static public readonly Int64ArrayHelperWithString Instance = new Int64ArrayHelperWithString();
 
        protected override int ReadArray(XmlDictionaryReader reader, string localName, string namespaceUri, Int64[] array, int offset, int count)
        { 
            return reader.ReadArray(localName, namespaceUri, array, offset, count); 
        }
 
        protected override void WriteArray(XmlDictionaryWriter writer, string prefix, string localName, string namespaceUri, Int64[] array, int offset, int count)
        {
            writer.WriteArray(prefix, localName, namespaceUri, array, offset, count);
        } 
    }
 
    class Int64ArrayHelperWithDictionaryString : ArrayHelper 
    {
        static public readonly Int64ArrayHelperWithDictionaryString Instance = new Int64ArrayHelperWithDictionaryString(); 

        protected override int ReadArray(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString namespaceUri, Int64[] array, int offset, int count)
        {
            return reader.ReadArray(localName, namespaceUri, array, offset, count); 
        }
 
        protected override void WriteArray(XmlDictionaryWriter writer, string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, Int64[] array, int offset, int count) 
        {
            writer.WriteArray(prefix, localName, namespaceUri, array, offset, count); 
        }
    }

    class SingleArrayHelperWithString : ArrayHelper 
    {
        static public readonly SingleArrayHelperWithString Instance = new SingleArrayHelperWithString(); 
 
        protected override int ReadArray(XmlDictionaryReader reader, string localName, string namespaceUri, float[] array, int offset, int count)
        { 
            return reader.ReadArray(localName, namespaceUri, array, offset, count);
        }

        protected override void WriteArray(XmlDictionaryWriter writer, string prefix, string localName, string namespaceUri, float[] array, int offset, int count) 
        {
            writer.WriteArray(prefix, localName, namespaceUri, array, offset, count); 
        } 
    }
 
    class SingleArrayHelperWithDictionaryString : ArrayHelper
    {
        static public readonly SingleArrayHelperWithDictionaryString Instance = new SingleArrayHelperWithDictionaryString();
 
        protected override int ReadArray(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString namespaceUri, float[] array, int offset, int count)
        { 
            return reader.ReadArray(localName, namespaceUri, array, offset, count); 
        }
 
        protected override void WriteArray(XmlDictionaryWriter writer, string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, float[] array, int offset, int count)
        {
            writer.WriteArray(prefix, localName, namespaceUri, array, offset, count);
        } 
    }
 
    class DoubleArrayHelperWithString : ArrayHelper 
    {
        static public readonly DoubleArrayHelperWithString Instance = new DoubleArrayHelperWithString(); 

        protected override int ReadArray(XmlDictionaryReader reader, string localName, string namespaceUri, double[] array, int offset, int count)
        {
            return reader.ReadArray(localName, namespaceUri, array, offset, count); 
        }
 
        protected override void WriteArray(XmlDictionaryWriter writer, string prefix, string localName, string namespaceUri, double[] array, int offset, int count) 
        {
            writer.WriteArray(prefix, localName, namespaceUri, array, offset, count); 
        }
    }

    class DoubleArrayHelperWithDictionaryString : ArrayHelper 
    {
        static public readonly DoubleArrayHelperWithDictionaryString Instance = new DoubleArrayHelperWithDictionaryString(); 
 
        protected override int ReadArray(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString namespaceUri, double[] array, int offset, int count)
        { 
            return reader.ReadArray(localName, namespaceUri, array, offset, count);
        }

        protected override void WriteArray(XmlDictionaryWriter writer, string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, double[] array, int offset, int count) 
        {
            writer.WriteArray(prefix, localName, namespaceUri, array, offset, count); 
        } 
    }
 
    class DecimalArrayHelperWithString : ArrayHelper
    {
        static public readonly DecimalArrayHelperWithString Instance = new DecimalArrayHelperWithString();
 
        protected override int ReadArray(XmlDictionaryReader reader, string localName, string namespaceUri, decimal[] array, int offset, int count)
        { 
            return reader.ReadArray(localName, namespaceUri, array, offset, count); 
        }
 
        protected override void WriteArray(XmlDictionaryWriter writer, string prefix, string localName, string namespaceUri, decimal[] array, int offset, int count)
        {
            writer.WriteArray(prefix, localName, namespaceUri, array, offset, count);
        } 
    }
 
    class DecimalArrayHelperWithDictionaryString : ArrayHelper 
    {
        static public readonly DecimalArrayHelperWithDictionaryString Instance = new DecimalArrayHelperWithDictionaryString(); 

        protected override int ReadArray(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString namespaceUri, decimal[] array, int offset, int count)
        {
            return reader.ReadArray(localName, namespaceUri, array, offset, count); 
        }
 
        protected override void WriteArray(XmlDictionaryWriter writer, string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, decimal[] array, int offset, int count) 
        {
            writer.WriteArray(prefix, localName, namespaceUri, array, offset, count); 
        }
    }

    class DateTimeArrayHelperWithString : ArrayHelper 
    {
        static public readonly DateTimeArrayHelperWithString Instance = new DateTimeArrayHelperWithString(); 
 
        protected override int ReadArray(XmlDictionaryReader reader, string localName, string namespaceUri, DateTime[] array, int offset, int count)
        { 
            return reader.ReadArray(localName, namespaceUri, array, offset, count);
        }

        protected override void WriteArray(XmlDictionaryWriter writer, string prefix, string localName, string namespaceUri, DateTime[] array, int offset, int count) 
        {
            writer.WriteArray(prefix, localName, namespaceUri, array, offset, count); 
        } 
    }
 
    class DateTimeArrayHelperWithDictionaryString : ArrayHelper
    {
        static public readonly DateTimeArrayHelperWithDictionaryString Instance = new DateTimeArrayHelperWithDictionaryString();
 
        protected override int ReadArray(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString namespaceUri, DateTime[] array, int offset, int count)
        { 
            return reader.ReadArray(localName, namespaceUri, array, offset, count); 
        }
 
        protected override void WriteArray(XmlDictionaryWriter writer, string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, DateTime[] array, int offset, int count)
        {
            writer.WriteArray(prefix, localName, namespaceUri, array, offset, count);
        } 
    }
 
    class GuidArrayHelperWithString : ArrayHelper 
    {
        static public readonly GuidArrayHelperWithString Instance = new GuidArrayHelperWithString(); 

        protected override int ReadArray(XmlDictionaryReader reader, string localName, string namespaceUri, Guid[] array, int offset, int count)
        {
            return reader.ReadArray(localName, namespaceUri, array, offset, count); 
        }
 
        protected override void WriteArray(XmlDictionaryWriter writer, string prefix, string localName, string namespaceUri, Guid[] array, int offset, int count) 
        {
            writer.WriteArray(prefix, localName, namespaceUri, array, offset, count); 
        }
    }

    class GuidArrayHelperWithDictionaryString : ArrayHelper 
    {
        static public readonly GuidArrayHelperWithDictionaryString Instance = new GuidArrayHelperWithDictionaryString(); 
 
        protected override int ReadArray(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString namespaceUri, Guid[] array, int offset, int count)
        { 
            return reader.ReadArray(localName, namespaceUri, array, offset, count);
        }

        protected override void WriteArray(XmlDictionaryWriter writer, string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, Guid[] array, int offset, int count) 
        {
            writer.WriteArray(prefix, localName, namespaceUri, array, offset, count); 
        } 
    }
 
    class TimeSpanArrayHelperWithString : ArrayHelper
    {
        static public readonly TimeSpanArrayHelperWithString Instance = new TimeSpanArrayHelperWithString();
 
        protected override int ReadArray(XmlDictionaryReader reader, string localName, string namespaceUri, TimeSpan[] array, int offset, int count)
        { 
            return reader.ReadArray(localName, namespaceUri, array, offset, count); 
        }
 
        protected override void WriteArray(XmlDictionaryWriter writer, string prefix, string localName, string namespaceUri, TimeSpan[] array, int offset, int count)
        {
            writer.WriteArray(prefix, localName, namespaceUri, array, offset, count);
        } 
    }
 
    class TimeSpanArrayHelperWithDictionaryString : ArrayHelper 
    {
        static public readonly TimeSpanArrayHelperWithDictionaryString Instance = new TimeSpanArrayHelperWithDictionaryString(); 

        protected override int ReadArray(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString namespaceUri, TimeSpan[] array, int offset, int count)
        {
            return reader.ReadArray(localName, namespaceUri, array, offset, count); 
        }
 
        protected override void WriteArray(XmlDictionaryWriter writer, string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, TimeSpan[] array, int offset, int count) 
        {
            writer.WriteArray(prefix, localName, namespaceUri, array, offset, count); 
        }
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.


                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK