public static unsafe void ReadFrom(this HGlobalCache <byte> hGCache, Stream stream) { int offset = 0; Loop: if (offset >= hGCache.Capacity) { hGCache.Expand(1218); } int readCount = stream.Read(hGCache.Context, offset, hGCache.Capacity - offset); //int readCount = VersionDifferences.ReadBytes( // stream, // hGCache.GetPointer() + offset, // hGCache.Capacity - offset); offset += readCount; if (offset == hGCache.Capacity) { goto Loop; } hGCache.Count = offset; }
public static async Task ReadFromAsync(this HGlobalCache <byte> hGCache, Stream stream) { int offset = 0; Loop: if (offset >= hGCache.Capacity) { hGCache.Expand(1218); } int readCount = await stream.ReadAsync( hGCache.Context, offset, hGCache.Capacity - offset); offset += readCount; if (offset == hGCache.Capacity) { goto Loop; } hGCache.Count = offset; }
public static unsafe void ReadFrom(this HGlobalCache <char> hGCache, TextReader textReader) { int offset = 0; Loop: if (offset >= hGCache.Capacity) { hGCache.Expand(1218); } int readCount = textReader.Read(hGCache.Context, offset, hGCache.Capacity - offset); //int readCount = VersionDifferences.ReadChars( // textReader, // hGCache.GetPointer() + offset, // hGCache.Capacity - offset); offset += readCount; if (offset == hGCache.Capacity) { goto Loop; } hGCache.Count = offset; }
/// <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, ReadOnlySpan <byte> source, Encoding encoding) { var maxCharsCount = encoding.GetMaxCharCount(source.Length); if (maxCharsCount >= hGCache.Capacity) { hGCache.Expand(maxCharsCount); } hGCache.Count = encoding.GetChars(source, hGCache); }
/// <summary> /// 使用指定哈希算法类型计算字节缓存的哈希值。以十六进制字符串返回。 /// </summary> /// <typeparam name="THashAlgorithm">哈希算法类型</typeparam> /// <param name="hGCache">字节缓存</param> /// <returns>返回 Hash 值的十六进制字符串</returns> public static string ComputeHash <THashAlgorithm>(this HGlobalCache <byte> hGCache) where THashAlgorithm : HashAlgorithm { const int byte_len = 2; #if NETCOREAPP && !NETCOREAPP2_0 var bytes = ((Span <byte>)hGCache).Slice(hGCache.Count); var instance = THashAlgorithmInstances <THashAlgorithm> .Instance; if (hGCache.Capacity - hGCache.Count < instance.HashSize) { hGCache.Expand(instance.HashSize); } if (instance.TryComputeHash(hGCache, bytes, out var written)) { bytes = bytes.Slice(0, written); } else { bytes = instance.ComputeHash(hGCache.Context, 0, hGCache.Count); } #else var bytes = THashAlgorithmInstances <THashAlgorithm> .Instance.ComputeHash(hGCache.Context, 0, hGCache.Count); #endif var str = StringHelper.MakeString(bytes.Length * byte_len); fixed(char *pStr = str) { var pStr2 = pStr; foreach (var item in bytes) { NumberHelper.Hex.ToString(item, byte_len, pStr2); pStr2 += byte_len; } } return(str); }
/// <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); }
/// <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); } }
public static async Task ReadFromAsync(this HGlobalCache <byte> hGCache, Stream stream) { int offset = 0; Loop: if (offset >= hGCache.Capacity) { hGCache.Expand(1218); } //IntPtr address; //unsafe //{ // address = (IntPtr)(hGCache.GetPointer() + offset); //} //int readCount = await VersionDifferences.ReadBytesAsync( // stream, // address, // hGCache.Capacity - offset); int readCount = await stream.ReadAsync( hGCache.Context, offset, hGCache.Capacity - offset); offset += readCount; if (offset == hGCache.Capacity) { goto Loop; } hGCache.Count = offset; }
/// <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); }