< Summary

Information
Class: System.Net.Http.HttpBaseStream
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\HttpBaseStream.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 34
Coverable lines: 34
Total lines: 91
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
BeginRead(...)100%110%
EndRead(...)100%110%
BeginWrite(...)100%110%
EndWrite(...)100%110%
Seek(...)100%110%
SetLength(...)100%110%
ReadByte()0%220%
Read(...)100%110%
ReadAsync(...)100%110%
Write(...)100%110%
WriteByte(...)100%110%
WriteAsync(...)100%110%
Flush()100%110%
FlushAsync(...)100%110%
NopAsync(...)0%220%

File(s)

D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\HttpBaseStream.cs

#LineLine coverage
 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
 4using System.IO;
 5using System.Runtime.InteropServices;
 6using System.Threading;
 7using System.Threading.Tasks;
 8
 9namespace System.Net.Http
 10{
 11    internal abstract class HttpBaseStream : Stream
 12    {
 013        public sealed override bool CanSeek => false;
 14
 15        public sealed override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, obj
 016            TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, default), callback, state);
 17
 18        public sealed override int EndRead(IAsyncResult asyncResult) =>
 019            TaskToAsyncResult.End<int>(asyncResult);
 20
 21        public sealed override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, ob
 022            TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, default), callback, state);
 23
 24        public sealed override void EndWrite(IAsyncResult asyncResult) =>
 025            TaskToAsyncResult.End(asyncResult);
 26
 027        public sealed override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
 28
 029        public sealed override void SetLength(long value) => throw new NotSupportedException();
 30
 031        public sealed override long Length => throw new NotSupportedException();
 32
 33        public sealed override long Position
 34        {
 035            get { throw new NotSupportedException(); }
 036            set { throw new NotSupportedException(); }
 37        }
 38
 39        public sealed override int ReadByte()
 040        {
 041            byte b = 0;
 042            return Read(new Span<byte>(ref b)) == 1 ? b : -1;
 043        }
 44
 45        public sealed override int Read(byte[] buffer, int offset, int count)
 046        {
 047            ValidateBufferArguments(buffer, offset, count);
 048            return Read(buffer.AsSpan(offset, count));
 049        }
 50
 51        public sealed override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationT
 052        {
 053            ValidateBufferArguments(buffer, offset, count);
 054            return ReadAsync(new Memory<byte>(buffer, offset, count), cancellationToken).AsTask();
 055        }
 56
 57        public override void Write(byte[] buffer, int offset, int count)
 058        {
 59            // This does sync-over-async, but it also should only end up being used in strange
 60            // situations.  Either a derived stream overrides this anyway, so the implementation won't be used,
 61            // or it's being called as part of HttpContent.SerializeToStreamAsync, which means custom
 62            // content is explicitly choosing to make a synchronous call as part of an asynchronous method.
 063            WriteAsync(buffer, offset, count, CancellationToken.None).GetAwaiter().GetResult();
 064        }
 65
 66        public sealed override void WriteByte(byte value) =>
 067            Write(new ReadOnlySpan<byte>(in value));
 68
 69        public sealed override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken
 070        {
 071            ValidateBufferArguments(buffer, offset, count);
 072            return WriteAsync(new ReadOnlyMemory<byte>(buffer, offset, count), cancellationToken).AsTask();
 073        }
 74
 075        public override void Flush() => FlushAsync(default).GetAwaiter().GetResult();
 76
 077        public override Task FlushAsync(CancellationToken cancellationToken) => NopAsync(cancellationToken);
 78
 79        protected static Task NopAsync(CancellationToken cancellationToken) =>
 080            cancellationToken.IsCancellationRequested ? Task.FromCanceled(cancellationToken) :
 081            Task.CompletedTask;
 82
 83        //
 84        // Methods which must be implemented by derived classes
 85        //
 86
 87        public abstract override int Read(Span<byte> buffer);
 88        public abstract override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken);
 89        public abstract override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken);
 90    }
 91}