altserialization.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ FXUpdate3074 / FXUpdate3074 / 1.1 / untmp / whidbey / QFE / ndp / fx / src / xsp / System / Web / Util / altserialization.cs / 1 / altserialization.cs

                            //------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 

/* 
 * AltSerialization.cs 
 *
 * Copyright (c) 1998-2000, Microsoft Corporation 
 *
 */

namespace System.Web.Util { 
    using System.Runtime.Serialization.Formatters;
    using System.Runtime.Serialization.Formatters.Binary; 
    using System.Runtime.Serialization; 
    using System.IO;
 
    internal static class AltSerialization {
        enum TypeID : byte {
            String = 1,
            Int32, 
            Boolean,
            DateTime, 
            Decimal, 
            Byte,
            Char, 
            Single,
            Double,
            SByte,
            Int16, 
            Int64,
            UInt16, 
            UInt32, 
            UInt64,
            TimeSpan, 
            Guid,
            IntPtr,
            UIntPtr,
            Object, 
            Null,
        } 
 
        internal static void WriteValueToStream(Object value, BinaryWriter writer) {
            if (value == null) { 
                writer.Write((byte)TypeID.Null);
            }
            else if (value is String) {
                writer.Write((byte)TypeID.String); 
                writer.Write((String) value);
            } 
            else if (value is Int32) { 
                writer.Write((byte)TypeID.Int32);
                writer.Write((Int32) value); 
            }
            else if (value is Boolean) {
                writer.Write((byte)TypeID.Boolean);
                writer.Write((Boolean) value); 
            }
            else if (value is DateTime) { 
                writer.Write((byte)TypeID.DateTime); 
                writer.Write(((DateTime) value).Ticks);
            } 
            else if (value is Decimal) {
                writer.Write((byte)TypeID.Decimal);
                int[] bits = Decimal.GetBits((Decimal)value);
                for (int i = 0; i < 4; i++) { 
                    writer.Write((int)bits[i]);
                } 
            } 
            else if (value is Byte) {
                writer.Write((byte)TypeID.Byte); 
                writer.Write((byte) value);
            }
            else if (value is Char) {
                writer.Write((byte)TypeID.Char); 
                writer.Write((char) value);
            } 
            else if (value is Single) { 
                writer.Write((byte)TypeID.Single);
                writer.Write((float) value); 
            }
            else if (value is Double) {
                writer.Write((byte)TypeID.Double);
                writer.Write((double) value); 
            }
            else if (value is SByte) { 
                writer.Write((byte)TypeID.SByte); 
                writer.Write((SByte) value);
            } 
            else if (value is Int16) {
                writer.Write((byte)TypeID.Int16);
                writer.Write((short) value);
            } 
            else if (value is Int64) {
                writer.Write((byte)TypeID.Int64); 
                writer.Write((long) value); 
            }
            else if (value is UInt16) { 
                writer.Write((byte)TypeID.UInt16);
                writer.Write((UInt16) value);
            }
            else if (value is UInt32) { 
                writer.Write((byte)TypeID.UInt32);
                writer.Write((UInt32) value); 
            } 
            else if (value is UInt64) {
                writer.Write((byte)TypeID.UInt64); 
                writer.Write((UInt64) value);
            }
            else if (value is TimeSpan) {
                writer.Write((byte)TypeID.TimeSpan); 
                writer.Write(((TimeSpan) value).Ticks);
            } 
            else if (value is Guid) { 
                writer.Write((byte)TypeID.Guid);
                Guid guid = (Guid) value; 
                byte[] bits = guid.ToByteArray();
                writer.Write(bits);
            }
            else if (value is IntPtr) { 
                writer.Write((byte)TypeID.IntPtr);
                IntPtr  v = (IntPtr) value; 
                if (IntPtr.Size == 4) { 
                    writer.Write((Int32)v.ToInt32());
                } 
                else {
                    Debug.Assert(IntPtr.Size == 8);
                    writer.Write((Int64)v.ToInt64());
                } 
            }
            else if (value is UIntPtr) { 
                writer.Write((byte)TypeID.UIntPtr); 
                UIntPtr  v = (UIntPtr) value;
                if (UIntPtr.Size == 4) { 
                    writer.Write((UInt32)v.ToUInt32());
                }
                else {
                    Debug.Assert(UIntPtr.Size == 8); 
                    writer.Write((UInt64)v.ToUInt64());
                } 
            } 
            else {
                writer.Write((byte)TypeID.Object); 
                BinaryFormatter formatter = new BinaryFormatter();
                try {
                    formatter.Serialize(writer.BaseStream, value);
                } catch (Exception innerException) { 
                    HttpException outerException = new HttpException(SR.GetString(SR.Cant_serialize_session_state), innerException);
                    outerException.SetFormatter(new UseLastUnhandledErrorFormatter(outerException)); 
                    throw outerException; 
                }
            } 
        }

        internal static Object ReadValueFromStream(BinaryReader reader) {
            TypeID  id; 
            Object  value = null;
 
            id = (TypeID) reader.ReadByte(); 
            switch (id) {
                case TypeID.String: 
                    value = reader.ReadString();
                    break;

                case TypeID.Int32: 
                    value = reader.ReadInt32();
                    break; 
 
                case TypeID.Boolean:
                    value = reader.ReadBoolean(); 
                    break;

                case TypeID.DateTime:
                    value = new DateTime(reader.ReadInt64()); 
                    break;
 
                case TypeID.Decimal: 
                    {
                        int[] bits = new int[4]; 
                        for (int i = 0; i < 4; i++) {
                            bits[i] = reader.ReadInt32();
                        }
 
                        value = new Decimal(bits);
                    } 
                    break; 

                case TypeID.Byte: 
                    value = reader.ReadByte();
                    break;

                case TypeID.Char: 
                    value = reader.ReadChar();
                    break; 
 
                case TypeID.Single:
                    value = reader.ReadSingle(); 
                    break;

                case TypeID.Double:
                    value = reader.ReadDouble(); 
                    break;
 
                case TypeID.SByte: 
                    value = reader.ReadSByte();
                    break; 

                case TypeID.Int16:
                    value = reader.ReadInt16();
                    break; 

                case TypeID.Int64: 
                    value = reader.ReadInt64(); 
                    break;
 
                case TypeID.UInt16:
                    value = reader.ReadUInt16();
                    break;
 
                case TypeID.UInt32:
                    value = reader.ReadUInt32(); 
                    break; 

                case TypeID.UInt64: 
                    value = reader.ReadUInt64();
                    break;

                case TypeID.TimeSpan: 
                    value = new TimeSpan(reader.ReadInt64());
                    break; 
 
                case TypeID.Guid:
                    { 
                        byte[] bits = reader.ReadBytes(16);
                        value = new Guid(bits);
                    }
                    break; 

                case TypeID.IntPtr: 
                    if (IntPtr.Size == 4) { 
                        value = new IntPtr(reader.ReadInt32());
                    } 
                    else {
                        Debug.Assert(IntPtr.Size == 8);
                        value = new IntPtr(reader.ReadInt64());
                    } 
                    break;
 
                case TypeID.UIntPtr: 
                    if (UIntPtr.Size == 4) {
                        value = new UIntPtr(reader.ReadUInt32()); 
                    }
                    else {
                        Debug.Assert(UIntPtr.Size == 8);
                        value = new UIntPtr(reader.ReadUInt64()); 
                    }
                    break; 
 
                case TypeID.Object:
                    BinaryFormatter formatter = new BinaryFormatter(); 
                    value = formatter.Deserialize(reader.BaseStream);
                    break;

                case TypeID.Null: 
                    value = null;
                    break; 
            } 

            return value; 
        }
    }
}
 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

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