public static byte[] ToBytes(this HGlobalCache <byte> hGCache)
        {
            var destinetion = new byte[hGCache.Count];

            Unsafe.CopyBlock(ref destinetion[0], ref hGCache.GetPointer()[0], (uint)hGCache.Count);

            return(destinetion);
        }
        /// <summary>
        /// 将 source 的内容缓存到 HGlobalCache 中。
        /// </summary>
        /// <param name="hGCache">HGlobalCache</param>
        /// <param name="source">source</param>
        /// <param name="encoding">编码</param>
        /// <returns>返回缓冲的长度</returns>
        public static void ReadFrom(this HGlobalCache <char> hGCache, HGlobalCache <byte> source, Encoding encoding)
        {
            if (source.Count <= 0)
            {
                return;
            }

            var maxCharsCount = encoding.GetMaxCharCount(source.Count);

            if (maxCharsCount >= hGCache.Capacity)
            {
                hGCache.Expand(maxCharsCount);
            }

            hGCache.Count = encoding.GetChars(
                source.GetPointer(),
                source.Count,
                hGCache.GetPointer(),
                hGCache.Capacity);
        }
        /// <summary>
        /// 将 HGlobalCache 中的内容写入到 destination 中。
        /// </summary>
        /// <param name="hGCache">HGlobalCache</param>
        /// <param name="destination">destination</param>
        /// <param name="encoding">编码</param>
        public static void WriteTo(this HGlobalCache <char> hGCache, HGlobalCache <byte> destination, Encoding encoding)
        {
            if (hGCache.Count <= 0)
            {
                return;
            }

            var maxBytesCount = encoding.GetMaxByteCount(hGCache.Count);

            if (maxBytesCount > destination.Capacity)
            {
                destination.Expand(maxBytesCount - destination.Capacity);
            }

            destination.Count = encoding.GetBytes(
                hGCache.GetPointer(),
                hGCache.Count,
                destination.GetPointer(),
                destination.Capacity);
        }
        /// <summary>
        /// 在字符缓存的后面拼接一个字符串。
        /// </summary>
        /// <param name="hGCache">字符缓存</param>
        /// <param name="value">字符串</param>
        /// <returns>返回当前字符缓存</returns>
        public static HGlobalCache <char> Append(this HGlobalCache <char> hGCache, string value)
        {
            var length = value.Length;

            if (hGCache.Capacity - hGCache.Count <= length)
            {
                hGCache.Expand(length + 2);
            }

            var pointer = hGCache.GetPointer() + hGCache.Count;

            for (int i = 0; i < length; ++i)
            {
                pointer[i] = value[i];
            }

            hGCache.Count += length;

            return(hGCache);
        }
        public static byte[] ToBytes(this HGlobalCache <char> hGCache, Encoding encoding)
        {
            var hGBytes = BytesPool.Rent();

            var maxBytesCount = encoding.GetMaxByteCount(hGCache.Count);

            if (maxBytesCount > hGBytes.Capacity)
            {
                hGBytes.Expand(maxBytesCount - hGBytes.Capacity);
            }

            hGBytes.Count = encoding.GetBytes(
                hGCache.GetPointer(),
                hGCache.Count,
                hGBytes.GetPointer(),
                hGBytes.Capacity);

            var bytes = hGBytes.ToBytes();

            BytesPool.Return(hGBytes);

            return(bytes);
        }
        /// <summary>
        /// 将 source 的内容缓存到 HGlobalCache 中。
        /// </summary>
        /// <param name="hGCache">HGlobalCache</param>
        /// <param name="source">source</param>
        /// <param name="encoding">编码</param>
        /// <returns>返回缓冲的长度</returns>
        public static void ReadFrom(this HGlobalCache <char> hGCache, ArraySegment <byte> source, Encoding encoding)
        {
            if (source.Count <= 0)
            {
                return;
            }

            var maxCharsCount = encoding.GetMaxCharCount(source.Count);

            if (maxCharsCount >= hGCache.Capacity)
            {
                hGCache.Expand(maxCharsCount);
            }

            fixed(byte *pSource = &source.Array[source.Offset])
            {
                hGCache.Count = encoding.GetChars(
                    pSource,
                    source.Count,
                    hGCache.GetPointer(),
                    hGCache.Capacity);
            }
        }
        /// <summary>
        /// 异步将 Stream 的内容缓存到 HGlobalCache 中。
        /// </summary>
        /// <param name="hGCache">HGlobalCache</param>
        /// <param name="stream">Stream</param>
        /// <param name="encoding">编码</param>
        /// <returns>返回缓冲的长度</returns>
        public static async Task ReadFromAsync(this HGlobalCache <char> hGCache, Stream stream, Encoding encoding)
        {
            var hGBytes = BytesPool.Rent();

            await hGBytes.ReadFromAsync(stream);

            var maxCharsCount = encoding.GetMaxCharCount(hGBytes.Count);

            if (maxCharsCount > hGCache.Capacity)
            {
                hGCache.Expand(maxCharsCount - hGCache.Capacity);
            }

            unsafe
            {
                hGCache.Count = encoding.GetChars(
                    hGBytes.GetPointer(),
                    hGBytes.Count,
                    hGCache.GetPointer(),
                    hGCache.Capacity);
            }

            BytesPool.Return(hGBytes);
        }
        /// <summary>
        /// 异步将 HGlobalCache 中的内容写入到流中。
        /// </summary>
        /// <param name="hGCache">HGlobalCache</param>
        /// <param name="stream">流</param>
        /// <param name="encoding">编码</param>
        public static async Task WriteToAsync(this HGlobalCache <char> hGCache, Stream stream, Encoding encoding)
        {
            var hGBytes = BytesPool.Rent();

            var maxBytesCount = encoding.GetMaxByteCount(hGCache.Count);

            if (maxBytesCount > hGBytes.Capacity)
            {
                hGBytes.Expand(maxBytesCount - hGBytes.Capacity);
            }

            unsafe
            {
                hGBytes.Count = encoding.GetBytes(
                    hGCache.GetPointer(),
                    hGCache.Count,
                    hGBytes.GetPointer(),
                    hGBytes.Capacity);
            }

            await hGBytes.WriteToAsync(stream);

            BytesPool.Return(hGBytes);
        }
 public static string ToStringEx(this HGlobalCache <char> hGCache)
 {
     return(new string(hGCache.GetPointer(), 0, hGCache.Count));
 }