< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.XmlStreamedByteStreamReader
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/XmlStreamedByteStreamReader.cs
Line coverage
51%
Covered lines: 30
Uncovered lines: 28
Coverable lines: 58
Total lines: 178
Line coverage: 51.7%
Branch coverage
27%
Covered branches: 5
Total branches: 18
Branch coverage: 27.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
Create(...)100%11100%
Create(...)100%110%
Create(...)100%110%
OnClose()100%11100%
ReadContentAsBase64(...)50%4477.77%
OnToByteArray()100%11100%
OnToStream()100%11100%
TryGetBase64ContentLength(...)50%6675%
.ctor(...)100%11100%
GetStream()100%11100%
ReleaseStream()100%11100%
.ctor(...)100%110%
GetStream()0%440%
ReleaseStream()100%110%
.ctor(...)100%110%
GetStream()0%440%
ReleaseStream()100%110%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/XmlStreamedByteStreamReader.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;
 5using System.IO;
 6using System.Net.Http;
 7using System.Xml;
 8using CoreWCF.Runtime;
 9
 10namespace CoreWCF.Channels
 11{
 12    internal abstract class XmlStreamedByteStreamReader : XmlByteStreamReader
 13    {
 14        protected XmlStreamedByteStreamReader(XmlDictionaryReaderQuotas quotas)
 715            : base(quotas)
 16        {
 717        }
 18
 719        public static XmlStreamedByteStreamReader Create(Stream stream, XmlDictionaryReaderQuotas quotas) => new StreamX
 20
 021        public static XmlStreamedByteStreamReader Create(HttpRequestMessage httpRequestMessage, XmlDictionaryReaderQuota
 22
 023        public static XmlStreamedByteStreamReader Create(HttpResponseMessage httpResponseMessage, XmlDictionaryReaderQuo
 24
 25        protected override void OnClose()
 26        {
 327            ReleaseStream();
 328            base.OnClose();
 329        }
 30
 31        public override int ReadContentAsBase64(byte[] buffer, int index, int count)
 32        {
 333            EnsureInContent();
 334            ByteStreamMessageUtility.EnsureByteBoundaries(buffer, index, count, true);
 35
 336            if (count == 0)
 37            {
 038                return 0;
 39            }
 40
 341            Stream stream = GetStream();
 342            int numBytesRead = stream.Read(buffer, index, count);
 343            if (numBytesRead == 0)
 44            {
 045                position = ReaderPosition.EndElement;
 46            }
 47
 348            return numBytesRead;
 49        }
 50
 51        protected override byte[] OnToByteArray()
 52        {
 153            throw Fx.Exception.AsError(
 154                  new InvalidOperationException(SR.GetByteArrayFromStreamContentNotAllowed));
 55        }
 56
 57        protected override Stream OnToStream()
 58        {
 159            Stream result = GetStream();
 60
 61            Fx.Assert(result != null, "The inner stream is null. Please check if the reader is closed or the ToStream me
 62
 163            ReleaseStream();
 164            return result;
 65        }
 66
 67        protected abstract Stream GetStream();
 68
 69        protected abstract void ReleaseStream();
 70
 71        public override bool TryGetBase64ContentLength(out int length)
 72        {
 73            // in ByteStream encoder, we're not concerned about individual xml nodes
 74            // therefore we can just return the entire length of the stream
 375            Stream stream = GetStream();
 376            if (!IsClosed && stream.CanSeek)
 77            {
 378                long streamLength = stream.Length;
 379                if (streamLength <= int.MaxValue)
 80                {
 381                    length = (int)streamLength;
 382                    return true;
 83                }
 84            }
 85
 086            length = -1;
 087            return false;
 88        }
 89
 90        internal class StreamXmlStreamedByteStreamReader : XmlStreamedByteStreamReader
 91        {
 92            private Stream _stream;
 93
 94            public StreamXmlStreamedByteStreamReader(Stream stream, XmlDictionaryReaderQuotas quotas)
 795                : base(quotas)
 96            {
 97                Fx.Assert(stream != null, "The 'stream' parameter should not be null.");
 98
 799                _stream = stream;
 7100            }
 101
 7102            protected override Stream GetStream() => _stream;
 103
 104            protected override void ReleaseStream()
 105            {
 4106                _stream = null;
 4107            }
 108        }
 109
 110        internal class HttpRequestMessageStreamedBodyReader : XmlStreamedByteStreamReader
 111        {
 112            private HttpRequestMessage _httpRequestMessage;
 113
 114            public HttpRequestMessageStreamedBodyReader(HttpRequestMessage httpRequestMessage, XmlDictionaryReaderQuotas
 0115                : base(quotas)
 116            {
 117                Fx.Assert(httpRequestMessage != null, "The 'httpRequestMessage' parameter should not be null.");
 118
 0119                _httpRequestMessage = httpRequestMessage;
 0120            }
 121
 122            protected override Stream GetStream()
 123            {
 0124                if (_httpRequestMessage == null)
 125                {
 0126                    return null;
 127                }
 128
 0129                HttpContent content = _httpRequestMessage.Content;
 0130                if (content != null)
 131                {
 0132                    return content.ReadAsStreamAsync().Result;
 133                }
 134
 0135                return new MemoryStream(Array.Empty<byte>());
 136            }
 137
 138            protected override void ReleaseStream()
 139            {
 0140                _httpRequestMessage = null;
 0141            }
 142        }
 143
 144        internal class HttpResponseMessageStreamedBodyReader : XmlStreamedByteStreamReader
 145        {
 146            private HttpResponseMessage _httpResponseMessage;
 147
 148            public HttpResponseMessageStreamedBodyReader(HttpResponseMessage httpResponseMessage, XmlDictionaryReaderQuo
 0149                : base(quotas)
 150            {
 151                Fx.Assert(httpResponseMessage != null, "The 'httpResponseMessage' parameter should not be null.");
 152
 0153                _httpResponseMessage = httpResponseMessage;
 0154            }
 155
 156            protected override Stream GetStream()
 157            {
 0158                if (_httpResponseMessage == null)
 159                {
 0160                    return null;
 161                }
 162
 0163                HttpContent content = _httpResponseMessage.Content;
 0164                if (content != null)
 165                {
 0166                    return content.ReadAsStreamAsync().Result;
 167                }
 168
 0169                return new MemoryStream(Array.Empty<byte>());
 170            }
 171
 172            protected override void ReleaseStream()
 173            {
 0174                _httpResponseMessage = null;
 0175            }
 176        }
 177    }
 178}