| | | 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.Diagnostics; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | |
| | | 9 | | namespace System.Net.Http |
| | | 10 | | { |
| | | 11 | | public class ByteArrayContent : HttpContent |
| | | 12 | | { |
| | | 13 | | private readonly byte[] _content; |
| | | 14 | | private readonly int _offset; |
| | | 15 | | private readonly int _count; |
| | | 16 | | |
| | 1 | 17 | | public ByteArrayContent(byte[] content) |
| | 1 | 18 | | { |
| | 1 | 19 | | ArgumentNullException.ThrowIfNull(content); |
| | | 20 | | |
| | 1 | 21 | | _content = content; |
| | 1 | 22 | | _count = content.Length; |
| | 1 | 23 | | } |
| | | 24 | | |
| | 0 | 25 | | public ByteArrayContent(byte[] content, int offset, int count) |
| | 0 | 26 | | { |
| | 0 | 27 | | ArgumentNullException.ThrowIfNull(content); |
| | | 28 | | |
| | 0 | 29 | | ArgumentOutOfRangeException.ThrowIfNegative(offset); |
| | 0 | 30 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(offset, content.Length); |
| | | 31 | | |
| | 0 | 32 | | ArgumentOutOfRangeException.ThrowIfNegative(count); |
| | 0 | 33 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(count, content.Length - offset); |
| | | 34 | | |
| | 0 | 35 | | _content = content; |
| | 0 | 36 | | _offset = offset; |
| | 0 | 37 | | _count = count; |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellati |
| | 0 | 41 | | stream.Write(_content, _offset, _count); |
| | | 42 | | |
| | | 43 | | protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) => |
| | 0 | 44 | | SerializeToStreamAsyncCore(stream, default); |
| | | 45 | | |
| | | 46 | | protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cance |
| | | 47 | | // Only skip the original protected virtual SerializeToStreamAsync if this |
| | | 48 | | // isn't a derived type that may have overridden the behavior. |
| | 0 | 49 | | GetType() == typeof(ByteArrayContent) ? SerializeToStreamAsyncCore(stream, cancellationToken) : |
| | 0 | 50 | | base.SerializeToStreamAsync(stream, context, cancellationToken); |
| | | 51 | | |
| | | 52 | | private protected Task SerializeToStreamAsyncCore(Stream stream, CancellationToken cancellationToken) => |
| | 0 | 53 | | stream.WriteAsync(_content, _offset, _count, cancellationToken); |
| | | 54 | | |
| | | 55 | | protected internal override bool TryComputeLength(out long length) |
| | 0 | 56 | | { |
| | 0 | 57 | | length = _count; |
| | 0 | 58 | | return true; |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | protected override Stream CreateContentReadStream(CancellationToken cancellationToken) => |
| | 0 | 62 | | CreateMemoryStreamForByteArray(); |
| | | 63 | | |
| | | 64 | | protected override Task<Stream> CreateContentReadStreamAsync() => |
| | 0 | 65 | | Task.FromResult<Stream>(CreateMemoryStreamForByteArray()); |
| | | 66 | | |
| | | 67 | | internal override Stream? TryCreateContentReadStream() => |
| | 0 | 68 | | GetType() == typeof(ByteArrayContent) ? CreateMemoryStreamForByteArray() : // type check ensures we use poss |
| | 0 | 69 | | null; |
| | | 70 | | |
| | 0 | 71 | | internal MemoryStream CreateMemoryStreamForByteArray() => new MemoryStream(_content, _offset, _count, writable: |
| | | 72 | | |
| | 0 | 73 | | internal override bool AllowDuplex => false; |
| | | 74 | | } |
| | | 75 | | } |