// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. #if NETSTANDARD2_1_OR_GREATER using System; using System.IO; using System.Threading; using System.Threading.Tasks; namespace CommunityToolkit.HighPerformance.Streams; /// internal sealed partial class IBufferWriterStream { /// public override void CopyTo(Stream destination, int bufferSize) { throw MemoryStream.GetNotSupportedException(); } /// public override ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default) { throw MemoryStream.GetNotSupportedException(); } /// public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default) { if (cancellationToken.IsCancellationRequested) { return new ValueTask(Task.FromCanceled(cancellationToken)); } try { Write(buffer.Span); return default; } catch (OperationCanceledException e) { return new ValueTask(Task.FromCanceled(e.CancellationToken)); } catch (Exception e) { return new ValueTask(Task.FromException(e)); } } /// public override int Read(Span buffer) { throw MemoryStream.GetNotSupportedException(); } /// public override void Write(ReadOnlySpan buffer) { MemoryStream.ValidateDisposed(this.disposed); Span destination = this.bufferWriter.GetSpan(buffer.Length); if (!buffer.TryCopyTo(destination)) { MemoryStream.ThrowArgumentExceptionForEndOfStreamOnWrite(); } this.bufferWriter.Advance(buffer.Length); } } #endif