880 lines · 31.2 KB · C#
RawDownload
//******************************************************************************************************//  StreamExtensions.cs - Gbtc////  Copyright © 2012, Grid Protection Alliance.  All Rights Reserved.////  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See//  the NOTICE file distributed with this work for additional information regarding copyright ownership.//  The GPA licenses this file to you under the MIT License (MIT), the "License"; you may//  not use this file except in compliance with the License. You may obtain a copy of the License at:////      http://www.opensource.org/licenses/MIT////  Unless agreed to in writing, the subject software distributed under the License is distributed on an//  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the//  License for the specific language governing permissions and limitations.////  Code Modification History://  ----------------------------------------------------------------------------------------------------//  09/19/2008 - J. Ritchie Carroll//       Generated original version of source code.//  10/24/2008 - Pinal C. Patel//       Edited code comments.//  09/14/2009 - Stephen C. Wills//       Added new header and license agreement.//  11/23/2011 - J. Ritchie Carroll//       Modified copy stream to use buffer pool.//  12/14/2012 - Starlynn Danyelle Gilliam//       Modified Header.//  08/15/2014 - Steven E. Chisholm//       Added stream encoding functions. ////****************************************************************************************************** using System;using System.Collections.Generic;using System.IO;using System.Runtime.CompilerServices;using System.Text; namespace GSF.IO{    /// <summary>    /// Defines extension functions related to <see cref="Stream"/> manipulation.    /// </summary>    public static unsafe class StreamExtensions    {        private const int BufferSize = 32768;         /// <summary>        /// Copies input <see cref="Stream"/> onto output <see cref="Stream"/>.        /// </summary>        /// <param name="source">The input <see cref="Stream"/>.</param>        /// <param name="destination">The output <see cref="Stream"/>.</param>        [Obsolete("Since .NET 4.0, any class inheriting from Stream has a native \"CopyTo\" function; also, .NET 4.5 has a \"CopyToAsync\" method.")]        public static void CopyStream(this Stream source, Stream destination)        {            byte[] buffer = BufferPool.TakeBuffer(BufferSize);             try            {                int bytesRead = source.Read(buffer, 0, BufferSize);                 while (bytesRead > 0)                {                    destination.Write(buffer, 0, bytesRead);                    bytesRead = source.Read(buffer, 0, BufferSize);                }            }            finally            {                if ((object)buffer != null)                    BufferPool.ReturnBuffer(buffer);            }        }         /// <summary>        /// Reads entire <see cref="Stream"/> contents, and returns <see cref="byte"/> array of data.        /// </summary>        /// <param name="source">The <see cref="Stream"/> to be converted to <see cref="byte"/> array.</param>        /// <returns>An array of <see cref="byte"/>.</returns>        public static byte[] ReadStream(this Stream source)        {            using (BlockAllocatedMemoryStream outStream = new BlockAllocatedMemoryStream())            {                source.CopyTo(outStream);                return outStream.ToArray();            }        }         #region [ Object Read/Write ]         private enum ObjectType : byte        {            Null = 0,            DBNull = 1,            BooleanTrue = 2,            BooleanFalse = 3,            SByte = 5,            Byte = 6,            Char = 4,            Int16 = 7,            UInt16 = 8,            Int32 = 9,            UInt32 = 10,            Int64 = 11,            UInt64 = 12,            Single = 13,            Double = 14,            Decimal = 15,            DateTime = 16,            String = 17,            ByteArray = 18,            CharArray = 19,            Guid = 20        }         /// <summary>        /// Encodes an object on a stream.        /// </summary>        /// <param name="stream">Destination stream.</param>        /// <param name="value">Object to encode.</param>        public static void WriteObject(this Stream stream, object value)        {            if (value == null)            {                stream.Write((byte)ObjectType.Null);                return;            }            switch (Type.GetTypeCode(value.GetType()))            {                case TypeCode.Empty:                    stream.Write((byte)ObjectType.Null);                    break;                case TypeCode.DBNull:                    stream.Write((byte)ObjectType.DBNull);                    break;                case TypeCode.Boolean:                    if ((bool)value)                    {                        stream.Write((byte)ObjectType.BooleanFalse);                    }                    else                    {                        stream.Write((byte)ObjectType.BooleanTrue);                    }                    break;                case TypeCode.Char:                    stream.Write((byte)ObjectType.Char);                    stream.Write((char)value);                    break;                case TypeCode.SByte:                    stream.Write((byte)ObjectType.SByte);                    stream.Write((sbyte)value);                    break;                case TypeCode.Byte:                    stream.Write((byte)ObjectType.Byte);                    stream.Write((byte)value);                    break;                case TypeCode.Int16:                    stream.Write((byte)ObjectType.Int16);                    stream.Write((short)value);                    break;                case TypeCode.UInt16:                    stream.Write((byte)ObjectType.UInt16);                    stream.Write((ushort)value);                    break;                case TypeCode.Int32:                    stream.Write((byte)ObjectType.Int32);                    stream.Write((int)value);                    break;                case TypeCode.UInt32:                    stream.Write((byte)ObjectType.UInt32);                    stream.Write((uint)value);                    break;                case TypeCode.Int64:                    stream.Write((byte)ObjectType.Int64);                    stream.Write((long)value);                    break;                case TypeCode.UInt64:                    stream.Write((byte)ObjectType.UInt64);                    stream.Write((ulong)value);                    break;                case TypeCode.Single:                    stream.Write((byte)ObjectType.Single);                    stream.Write((float)value);                    break;                case TypeCode.Double:                    stream.Write((byte)ObjectType.Double);                    stream.Write((double)value);                    break;                case TypeCode.Decimal:                    stream.Write((byte)ObjectType.Decimal);                    stream.Write((Decimal)value);                    break;                case TypeCode.DateTime:                    stream.Write((byte)ObjectType.DateTime);                    stream.Write((DateTime)value);                    break;                case TypeCode.String:                    stream.Write((byte)ObjectType.String);                    stream.Write((String)value);                    break;                case TypeCode.Object:                    if (value is byte[])                    {                        stream.Write((byte)ObjectType.ByteArray);                        stream.WriteWithLength((byte[])value);                    }                    else if (value is char[])                    {                        stream.Write((byte)ObjectType.CharArray);                        stream.Write(new string((char[])value));                    }                    else if (value is Guid)                    {                        stream.Write((byte)ObjectType.Guid);                        stream.Write((Guid)value);                    }                    else                    {                        throw new NotSupportedException("This type cannot be serialized");                    }                    break;                default:                    throw new ArgumentOutOfRangeException();            }        }         /// <summary>        /// Reads an object from a stream.        /// </summary>        /// <param name="stream">Source stream.</param>        /// <returns>Decoded object.</returns>        public static object ReadObject(this Stream stream)        {            ObjectType type = (ObjectType)stream.ReadNextByte();            switch (type)            {                case ObjectType.Null:                    return null;                case ObjectType.DBNull:                    return DBNull.Value;                case ObjectType.BooleanTrue:                    return true;                case ObjectType.BooleanFalse:                    return false;                case ObjectType.Char:                    return stream.ReadChar();                case ObjectType.SByte:                    return stream.ReadSByte();                case ObjectType.Byte:                    return stream.ReadNextByte();                case ObjectType.Int16:                    return stream.ReadInt16();                case ObjectType.UInt16:                    return stream.ReadUInt16();                case ObjectType.Int32:                    return stream.ReadInt32();                case ObjectType.UInt32:                    return stream.ReadUInt32();                case ObjectType.Int64:                    return stream.ReadInt64();                case ObjectType.UInt64:                    return stream.ReadUInt64();                case ObjectType.Single:                    return stream.ReadSingle();                case ObjectType.Double:                    return stream.ReadDouble();                case ObjectType.Decimal:                    return stream.ReadDecimal();                case ObjectType.DateTime:                    return stream.ReadDateTime();                case ObjectType.String:                    return stream.ReadString();                case ObjectType.ByteArray:                    return stream.ReadBytes();                case ObjectType.CharArray:                    return stream.ReadString().ToCharArray();                case ObjectType.Guid:                    return stream.ReadGuid();                default:                    throw new ArgumentOutOfRangeException();            }        }         #endregion         #region [ 1 byte values ]         /// <summary>        /// Writes the supplied <paramref name="value"/> to        /// <pararef name="stream"/> in little endian format.        /// </summary>        /// <param name="stream">the stream to write to</param>        /// <param name="value">the value to write</param>        public static void Write(this Stream stream, byte value)        {            stream.WriteByte((byte)value);        }         /// <summary>        /// Read a byte from the stream.         /// Will throw an exception if the end of the stream has been reached.        /// </summary>        /// <param name="stream">the stream to read from.</param>        /// <returns>the value read</returns>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static byte ReadNextByte(this Stream stream)        {            int value = stream.ReadByte();            if (value < 0)                ThrowEOS();            return (byte)value;        }         /// <summary>        /// Reads the value from the stream in little endian format.        /// </summary>        /// <param name="stream">the stream to read from.</param>        /// <returns>The value read</returns>        public static bool ReadBoolean(this Stream stream)        {            return stream.ReadNextByte() != 0;        }         /// <summary>        /// Writes the supplied <paramref name="value"/> to         /// <paramref name="stream"/> in little endian format.        /// </summary>        /// <param name="stream">the stream to write to</param>        /// <param name="value">the value to write</param>        public static void Write(this Stream stream, bool value)        {            if (value)                stream.Write((byte)1);            else                stream.Write((byte)0);        }         /// <summary>        /// Writes the supplied <paramref name="value"/> to         /// <paramref name="stream"/> in little endian format.        /// </summary>        /// <param name="stream">the stream to write to</param>        /// <param name="value">the value to write</param>        public static void Write(this Stream stream, sbyte value)        {            stream.Write((byte)value);        }         /// <summary>        /// Reads the value from the stream in little endian format.        /// </summary>        /// <param name="stream">the stream to read from.</param>        /// <returns>The value read</returns>        public static sbyte ReadSByte(this Stream stream)        {            return (sbyte)stream.ReadNextByte();        }         #endregion         #region [ 2-byte values ]         /// <summary>        /// Writes the supplied <paramref name="value"/> to         /// <paramref name="stream"/> in little endian format.        /// </summary>        /// <param name="stream">the stream to write to</param>        /// <param name="value">the value to write</param>        public static void Write(this Stream stream, short value)        {            Write(stream, LittleEndian.GetBytes(value));        }         /// <summary>        /// Writes the supplied <paramref name="value"/> to         /// <paramref name="stream"/> in little endian format.        /// </summary>        /// <param name="stream">the stream to write to</param>        /// <param name="value">the value to write</param>        public static void Write(this Stream stream, ushort value)        {            Write(stream, (short)value);        }         /// <summary>        /// Writes the supplied <paramref name="value"/> to         /// <paramref name="stream"/> in little endian format.        /// </summary>        /// <param name="stream">the stream to write to</param>        /// <param name="value">the value to write</param>        public static void Write(this Stream stream, char value)        {            Write(stream, (short)value);        }         /// <summary>        /// Reads the value from the stream in little endian format.        /// </summary>        /// <param name="stream">the stream to read from.</param>        /// <returns>The value read</returns>        public static short ReadInt16(this Stream stream)        {            byte[] data = stream.ReadBytes(2);            return LittleEndian.ToInt16(data, 0);        }         /// <summary>        /// Reads the value from the stream in little endian format.        /// </summary>        /// <param name="stream">the stream to read from.</param>        /// <returns>The value read</returns>        public static ushort ReadUInt16(this Stream stream)        {            return (ushort)stream.ReadInt16();        }         /// <summary>        /// Reads the value from the stream in little endian format.        /// </summary>        /// <param name="stream">the stream to read from.</param>        /// <returns>The value read</returns>        public static char ReadChar(this Stream stream)        {            return (char)stream.ReadInt16();        }         #endregion          #region [ 4-byte values ]         /// <summary>        /// Writes the supplied <paramref name="value"/> to         /// <paramref name="stream"/> in little endian format.        /// </summary>        /// <param name="stream">the stream to write to</param>        /// <param name="value">the value to write</param>        public static void Write(this Stream stream, int value)        {            Write(stream, LittleEndian.GetBytes(value));        }         /// <summary>        /// Writes the supplied <paramref name="value"/> to         /// <paramref name="stream"/> in little endian format.        /// </summary>        /// <param name="stream">the stream to write to</param>        /// <param name="value">the value to write</param>        public static void Write(this Stream stream, uint value)        {            stream.Write((int)value);        }         /// <summary>        /// Writes the supplied <paramref name="value"/> to         /// <paramref name="stream"/> in little endian format.        /// </summary>        /// <param name="stream">the stream to write to</param>        /// <param name="value">the value to write</param>        public static void Write(this Stream stream, float value)        {            Write(stream, *(int*)&value);        }          /// <summary>        /// Reads the value from the stream in little endian format.        /// </summary>        /// <param name="stream">the stream to read from.</param>        /// <returns>The value read</returns>        public static int ReadInt32(this Stream stream)        {            byte[] data = stream.ReadBytes(4);            return LittleEndian.ToInt32(data, 0);        }         /// <summary>        /// Reads the value from the stream in little endian format.        /// </summary>        /// <param name="stream">the stream to read from.</param>        /// <returns>The value read</returns>        public static uint ReadUInt32(this Stream stream)        {            return (uint)stream.ReadInt32();        }         /// <summary>        /// Reads the value from the stream in little endian format.        /// </summary>        /// <param name="stream">the stream to read from.</param>        /// <returns>The value read</returns>        public static float ReadSingle(this Stream stream)        {            var value = stream.ReadInt32();            return *(float*)&value;        }         #endregion

Lines 1–500 of 880.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t