public static async Task <int> ReadCharsAsync(TextReader textReader, IntPtr chars, int length) { if (length > 130 && ArrayHelper.IsSupportedOneRankValueArrayInfo) { var ends = ArrayHelper.AsTempOneRankValueArray <char>(chars, length, out var starts); var count = await textReader.ReadAsync(starts, 0, starts.Length); if (count == starts.Length) { count += await textReader.ReadAsync(ends, 0, ends.Length); } unsafe { Unsafe.CopyBlock( ref Unsafe.As <char, byte>(ref ((char *)chars)[0]), ref Unsafe.As <char, byte>(ref starts[0]), (uint)(starts.Length * sizeof(char))); } return(count); } else { const int bufferLength = 128; var buffer = new char[bufferLength]; var total = 0; int readCount; while (total < length && (readCount = await textReader.ReadAsync(buffer, 0, Math.Min(bufferLength, length - total))) != 0) { unsafe { Unsafe.CopyBlock( ref Unsafe.As <char, byte>(ref ((char *)chars)[total]), ref Unsafe.As <char, byte>(ref buffer[0]), (uint)readCount * sizeof(char)); } total += readCount; } return(total); } }
public static async Task <int> ReadBytesAsync(Stream stream, IntPtr bytes, int length) { if (length > 130 && ArrayHelper.IsSupportedOneRankValueArrayInfo) { var ends = ArrayHelper.AsTempOneRankValueArray <byte>(bytes, length, out var starts); var count = await stream.ReadAsync(starts, 0, starts.Length); if (count == starts.Length) { count += await stream.ReadAsync(ends, 0, ends.Length); } unsafe { Unsafe.CopyBlock( ref ((byte *)bytes)[0], ref starts[0], (uint)starts.Length); } return(count); } else { const int bufferLength = 128; var buffer = new byte[bufferLength]; var total = 0; int readCount; while (total < length && (readCount = await stream.ReadAsync(buffer, 0, Math.Min(bufferLength, length - total))) != 0) { unsafe { Unsafe.CopyBlock( ref ((byte *)bytes)[total], ref buffer[0], (uint)readCount); } total += readCount; } return(total); } }
public static unsafe int ReadBytes(Stream stream, byte *bytes, int length) { #if NETCOREAPP && !NETCOREAPP2_0 return(stream.Read(new Span <byte>(bytes, length))); #else if (length > 130 && ArrayHelper.IsSupportedOneRankValueArrayInfo) { var ends = ArrayHelper.AsTempOneRankValueArray <byte>(bytes, length, out var starts); var count = stream.Read(starts, 0, starts.Length); if (count == starts.Length) { count += stream.Read(ends, 0, ends.Length); } Unsafe.CopyBlock( ref bytes[0], ref starts[0], (uint)starts.Length); return(count); } else { const int bufferLength = 128; var buffer = new byte[bufferLength]; var total = 0; int readCount; while (total < length && (readCount = stream.Read(buffer, 0, Math.Min(bufferLength, length - total))) != 0) { Unsafe.CopyBlock( ref bytes[total], ref buffer[0], (uint)readCount); total += readCount; } return(total); } #endif }
public static unsafe int ReadChars(TextReader textReader, char *chars, int length) { #if NETCOREAPP && !NETCOREAPP2_0 return(textReader.Read(new Span <char>(chars, length))); #else if (length > 130 && ArrayHelper.IsSupportedOneRankValueArrayInfo) { var ends = ArrayHelper.AsTempOneRankValueArray <char>(chars, length, out var starts); var count = textReader.Read(starts, 0, starts.Length); if (count == starts.Length) { count += textReader.Read(ends, 0, ends.Length); } Unsafe.CopyBlock( ref Unsafe.As <char, byte>(ref chars[0]), ref Unsafe.As <char, byte>(ref starts[0]), (uint)(starts.Length * sizeof(char))); return(count); } else { const int bufferLength = 128; var buffer = new char[bufferLength]; var total = 0; int readCount; while (total < length && (readCount = textReader.Read(buffer, 0, Math.Min(bufferLength, length - total))) != 0) { Unsafe.CopyBlock( ref Unsafe.As <char, byte>(ref chars[total]), ref Unsafe.As <char, byte>(ref buffer[0]), (uint)readCount * sizeof(char)); total += readCount; } return(total); } #endif }
public static async Task WriteCharsAsync(TextWriter textWriter, IntPtr chars, int length) { if (length > 130 && ArrayHelper.IsSupportedOneRankValueArrayInfo) { var ends = ArrayHelper.AsTempOneRankValueArray <char>(chars, length, out var starts); await textWriter.WriteAsync(starts); await textWriter.WriteAsync(ends); unsafe { Unsafe.CopyBlock( ref Unsafe.As <char, byte>(ref ((char *)chars)[0]), ref Unsafe.As <char, byte>(ref starts[0]), (uint)(starts.Length * sizeof(char))); } } else { const int bufferLength = 128; var buffer = new char[Math.Min(bufferLength, length)]; for (int index = 0, count = buffer.Length; index < length; index += count, count = Math.Min(buffer.Length, length - index)) { unsafe { Unsafe.CopyBlock( ref Unsafe.As <char, byte>(ref buffer[0]), ref Unsafe.As <char, byte>(ref ((char *)chars)[index]), (uint)(count * sizeof(char))); } textWriter.Write(buffer, 0, count); } } }
public static async Task WriteBytesAsync(Stream stream, IntPtr bytes, int length) { if (length > 130 && ArrayHelper.IsSupportedOneRankValueArrayInfo) { var ends = ArrayHelper.AsTempOneRankValueArray <byte>(bytes, length, out var starts); await stream.WriteAsync(starts, 0, starts.Length); await stream.WriteAsync(ends, 0, ends.Length); unsafe { Unsafe.CopyBlock( ref ((byte *)bytes)[0], ref starts[0], (uint)starts.Length); } } else { const int bufferLength = 128; var buffer = new byte[Math.Min(bufferLength, length)]; for (int index = 0, count = buffer.Length; index < length; index += count, count = Math.Min(buffer.Length, length - index)) { unsafe { Unsafe.CopyBlock( ref buffer[0], ref ((byte *)bytes)[index], (uint)count); } await stream.WriteAsync(buffer, 0, count); } } }
public static unsafe void WriteBytes(Stream stream, byte *bytes, int length) { #if NETCOREAPP && !NETCOREAPP2_0 stream.Write(new ReadOnlySpan <byte>(bytes, length)); #else if (length > 130 && ArrayHelper.IsSupportedOneRankValueArrayInfo) { var ends = ArrayHelper.AsTempOneRankValueArray <byte>(bytes, length, out var starts); stream.Write(starts, 0, starts.Length); stream.Write(ends, 0, ends.Length); Unsafe.CopyBlock( ref bytes[0], ref starts[0], (uint)starts.Length); } else { const int bufferLength = 128; var buffer = new byte[Math.Min(bufferLength, length)]; for (int index = 0, count = buffer.Length; index < length; index += count, count = Math.Min(buffer.Length, length - index)) { Unsafe.CopyBlock( ref buffer[0], ref bytes[index], (uint)count); stream.Write(buffer, 0, count); } } #endif }
public static unsafe void WriteChars(TextWriter textWriter, char *chars, int length) { #if NETCOREAPP && !NETCOREAPP2_0 textWriter.Write(new ReadOnlySpan <char>(chars, length)); #else if (length > 130 && ArrayHelper.IsSupportedOneRankValueArrayInfo) { var ends = ArrayHelper.AsTempOneRankValueArray <char>(chars, length, out var starts); textWriter.Write(starts); textWriter.Write(ends); Unsafe.CopyBlock( ref Unsafe.As <char, byte>(ref chars[0]), ref Unsafe.As <char, byte>(ref starts[0]), (uint)(starts.Length * sizeof(char))); } else { const int bufferLength = 128; var buffer = new char[Math.Min(bufferLength, length)]; for (int index = 0, count = buffer.Length; index < length; index += count, count = Math.Min(buffer.Length, length - index)) { Unsafe.CopyBlock( ref Unsafe.As <char, byte>(ref buffer[0]), ref Unsafe.As <char, byte>(ref chars[index]), (uint)(count * sizeof(char))); textWriter.Write(buffer, 0, count); } } #endif }