| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | |
| | | 8 | | namespace System.Net.Http |
| | | 9 | | { |
| | | 10 | | public sealed class ReadOnlyMemoryContent : HttpContent |
| | | 11 | | { |
| | | 12 | | private readonly ReadOnlyMemory<byte> _content; |
| | | 13 | | |
| | 0 | 14 | | public ReadOnlyMemoryContent(ReadOnlyMemory<byte> content) => |
| | 0 | 15 | | _content = content; |
| | | 16 | | |
| | | 17 | | protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellati |
| | 0 | 18 | | { |
| | 0 | 19 | | stream.Write(_content.Span); |
| | 0 | 20 | | } |
| | | 21 | | |
| | | 22 | | protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) => |
| | 0 | 23 | | stream.WriteAsync(_content).AsTask(); |
| | | 24 | | |
| | | 25 | | protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cance |
| | 0 | 26 | | stream.WriteAsync(_content, cancellationToken).AsTask(); |
| | | 27 | | |
| | | 28 | | protected internal override bool TryComputeLength(out long length) |
| | 0 | 29 | | { |
| | 0 | 30 | | length = _content.Length; |
| | 0 | 31 | | return true; |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | protected override Stream CreateContentReadStream(CancellationToken cancellationToken) => |
| | 0 | 35 | | new ReadOnlyMemoryStream(_content); |
| | | 36 | | |
| | | 37 | | protected override Task<Stream> CreateContentReadStreamAsync() => |
| | 0 | 38 | | Task.FromResult<Stream>(new ReadOnlyMemoryStream(_content)); |
| | | 39 | | |
| | | 40 | | internal override Stream TryCreateContentReadStream() => |
| | 0 | 41 | | new ReadOnlyMemoryStream(_content); |
| | | 42 | | |
| | 0 | 43 | | internal override bool AllowDuplex => false; |
| | | 44 | | } |
| | | 45 | | } |