/// <summary> /// 占用当前线程的实例。 /// </summary> /// <returns>返回一个实例</returns> public static HGlobalCache <T> OccupancyInstance() { var value = threadInstance; if (value == null) { value = new HGlobalCache <T>(); threadInstance = value; } var nested = 0; Loop: if (value.available) { value.available = false; return(value); } if (nested > 5) { throw new NotSupportedException("Too many nested!"); } value = value.next ?? (value.next = new HGlobalCache <T>()); ++nested; goto Loop; }
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; }
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 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> /// 异步将 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 writer = new StreamWriter(stream, encoding); await hGCache.WriteToAsync(writer); writer.Flush(); }
public static T[] ToArray <T>(this HGlobalCache <T> hGCache) where T : unmanaged { var ret = new T[hGCache.Count]; Array.Copy(hGCache.Context, hGCache.Offset, ret, 0, hGCache.Count); return(ret); }
/// <summary> /// 将 Stream 的内容缓存到 HGlobalCache 中。 /// </summary> /// <param name="hGCache">HGlobalCache</param> /// <param name="stream">Stream</param> /// <param name="encoding">编码</param> /// <returns>返回缓冲的长度</returns> public static void ReadFrom(this HGlobalCache <char> hGCache, Stream stream, Encoding encoding) { var hGBytes = BytesPool.Rent(); hGBytes.ReadFrom(stream); hGCache.ReadFrom(hGBytes, encoding); BytesPool.Return(hGBytes); }
/// <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) { destination.Grow(encoding.GetMaxByteCount(hGCache.Count)); destination.Count += encoding.GetBytes( hGCache.First, hGCache.Count, destination.Current, destination.Rest); }
/// <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) { hGCache.Grow(encoding.GetMaxCharCount(source.Count)); hGCache.Count += encoding.GetChars( source.First, source.Count, hGCache.Current, hGCache.Rest); }
/// <summary> /// 将 HGlobalCache 中的内容写入到流中。 /// </summary> /// <param name="hGCache">HGlobalCache</param> /// <param name="stream">流</param> /// <param name="encoding">编码</param> public static void WriteTo(this HGlobalCache <char> hGCache, Stream stream, Encoding encoding) { var hGBytes = BytesPool.Rent(); hGCache.WriteTo(hGBytes, encoding); hGBytes.WriteTo(stream); BytesPool.Return(hGBytes); }
public static void ReadFrom(this HGlobalCache <byte> hGCache, string str, Encoding encoding) { hGCache.Grow(encoding.GetMaxByteCount(str.Length)); hGCache.Count += encoding.GetBytes( str, 0, str.Length, hGCache.Context, hGCache.Offset + hGCache.Count); }
/// <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); }
public static void ReadFrom <T>(this HGlobalCache <T> hGCache, ref T source, int length) where T : unmanaged { hGCache.Grow(length); Underlying.CopyBlock( ref Underlying.As <T, byte>(ref *hGCache.Current), ref Underlying.As <T, byte>(ref source), checked ((uint)((long)length * sizeof(T))) ); hGCache.Count += length; }
/// <summary> /// 使用指定哈希算法类型和编码类型计算字符缓存的哈希值。以十六进制字符串返回。 /// </summary> /// <typeparam name="THashAlgorithm">哈希算法类型</typeparam> /// <param name="hGCache">字符缓存</param> /// <param name="encoding">指定编码</param> /// <returns>返回 Hash 值的十六进制字符串</returns> public static string ComputeHash <THashAlgorithm>(this HGlobalCache <char> hGCache, Encoding encoding) where THashAlgorithm : HashAlgorithm { var hGBytes = BytesPool.Rent(); hGCache.WriteTo(hGBytes, encoding); var result = ComputeHash <THashAlgorithm>(hGBytes); BytesPool.Return(hGBytes); return(result); }
public static void ReadFrom <T>(this HGlobalCache <T> hGCache, T *source, int length) where T : unmanaged { hGCache.Grow(length); Underlying.CopyBlock( hGCache.Current, source, checked ((uint)((long)length * sizeof(T))) ); hGCache.Count += length; }
/// <summary> /// 将 source 的内容缓存到 HGlobalCache 中。 /// </summary> /// <param name="hGCache">HGlobalCache</param> /// <param name="source">source</param> /// <param name="encoding">编码</param> public static void ReadFrom(this HGlobalCache <char> hGCache, byte[] source, Encoding encoding) { hGCache.Grow(encoding.GetMaxCharCount(source.Length)); fixed(byte *pSource = source) { hGCache.Count += encoding.GetChars( pSource, source.Length, hGCache.Current, hGCache.Rest); } }
/// <summary> /// 将 source 的内容缓存到 HGlobalCache 中。 /// </summary> /// <param name="hGCache">HGlobalCache</param> /// <param name="source">source</param> /// <param name="encoding">编码</param> public static void ReadFrom(this HGlobalCache <char> hGCache, ArraySegment <byte> source, Encoding encoding) { hGCache.Grow(encoding.GetMaxCharCount(source.Count)); fixed(byte *pSource = &source.Array[source.Offset]) { hGCache.Count += encoding.GetChars( pSource, source.Count, hGCache.Current, hGCache.Rest); } }
public static unsafe void ReadFrom(this HGlobalCache <char> hGCache, TextReader textReader) { Loop: hGCache.Grow(1218); int readCount = textReader.Read( hGCache.Context, hGCache.Offset + hGCache.Count, hGCache.Rest); hGCache.Count += readCount; if (readCount != 0) { goto Loop; } }
public static async Task ReadFromAsync(this HGlobalCache <char> hGCache, TextReader textReader) { Loop: hGCache.Grow(1218); var readCount = await textReader.ReadAsync( hGCache.Context, hGCache.Offset + hGCache.Count, hGCache.Rest); hGCache.Count += readCount; if (readCount != 0) { goto Loop; } }
public static async Task ReadFromAsync(this HGlobalCache <byte> hGCache, Stream stream) { Loop: hGCache.Grow(1218); var readCount = await stream.ReadAsync( hGCache.Context, hGCache.Offset + hGCache.Count, hGCache.Rest); hGCache.Count += readCount; if (readCount != 0) { goto Loop; } }
public static unsafe void ReadFrom(this HGlobalCache <byte> hGCache, Stream stream) { Loop: hGCache.Grow(1218); int readCount = stream.Read( hGCache.Context, hGCache.Offset + hGCache.Count, hGCache.Rest); hGCache.Count += readCount; if (readCount != 0) { goto Loop; } }
public new void Return(HGlobalCache <T> hGCache) { if (hGCache.Offset != 0) { hGCache.Offset = 0; } ++heft; average += (hGCache.Available * 1000 - average) / heft; if (hGCache.Available * Ratio <= average) { hGCache.Offset = 0; hGCache.Count = 0; base.Return(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> /// 将 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, 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> /// 将 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 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); }
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; }