| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.IO.Pipelines; |
| | | 4 | | |
| | | 5 | | namespace IceRpc.Internal; |
| | | 6 | | |
| | | 7 | | /// <summary>A PipeWriter that does nothing and always throws InvalidOperationException except for Complete.</summary> |
| | | 8 | | internal sealed class InvalidPipeWriter : PipeWriter |
| | | 9 | | { |
| | 0 | 10 | | public override bool CanGetUnflushedBytes => false; |
| | | 11 | | |
| | 0 | 12 | | public override long UnflushedBytes => throw new InvalidOperationException(ExceptionMessage); |
| | | 13 | | |
| | | 14 | | /// <summary>Gets the InvalidPipeWriter singleton instance.</summary> |
| | 4 | 15 | | internal static PipeWriter Instance { get; } = new InvalidPipeWriter(); |
| | | 16 | | |
| | | 17 | | private const string ExceptionMessage = "Writing an invalid pipe writer is not allowed."; |
| | | 18 | | |
| | 0 | 19 | | public override void Advance(int bytes) => throw new InvalidOperationException(ExceptionMessage); |
| | | 20 | | |
| | 0 | 21 | | public override Stream AsStream(bool leaveOpen = false) => throw new InvalidOperationException(ExceptionMessage); |
| | | 22 | | |
| | 0 | 23 | | public override void CancelPendingFlush() => throw new InvalidOperationException(ExceptionMessage); |
| | | 24 | | |
| | | 25 | | public override void Complete(Exception? exception) |
| | 2 | 26 | | { |
| | | 27 | | // no-op |
| | 2 | 28 | | } |
| | | 29 | | |
| | | 30 | | public override ValueTask<FlushResult> FlushAsync(CancellationToken cancellationToken) => |
| | 2 | 31 | | throw new InvalidOperationException(ExceptionMessage); |
| | | 32 | | |
| | 0 | 33 | | public override Memory<byte> GetMemory(int sizeHint) => throw new InvalidOperationException(ExceptionMessage); |
| | | 34 | | |
| | 0 | 35 | | public override Span<byte> GetSpan(int sizeHint) => throw new InvalidOperationException(ExceptionMessage); |
| | | 36 | | |
| | | 37 | | public override ValueTask<FlushResult> WriteAsync( |
| | | 38 | | ReadOnlyMemory<byte> source, |
| | 0 | 39 | | CancellationToken cancellationToken) => throw new InvalidOperationException(ExceptionMessage); |
| | | 40 | | |
| | | 41 | | protected override Task CopyFromAsync(Stream source, CancellationToken cancellationToken) => |
| | 0 | 42 | | throw new InvalidOperationException(ExceptionMessage); |
| | | 43 | | |
| | 2 | 44 | | private InvalidPipeWriter() |
| | 2 | 45 | | { |
| | | 46 | | // ensures there is only one instance |
| | 2 | 47 | | } |
| | | 48 | | } |