< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.StreamBodyWriter
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/StreamBodyWriter.cs
Line coverage
42%
Covered lines: 18
Uncovered lines: 24
Coverable lines: 42
Total lines: 153
Line coverage: 42.8%
Branch coverage
66%
Covered branches: 4
Total branches: 6
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
OnCreateBufferedCopy(...)100%110%
OnWriteBodyContents(...)100%11100%
.ctor(...)50%2280%
Flush()100%11100%
Read(...)100%110%
BeginRead(...)100%110%
EndRead(...)100%110%
ReadByte()100%110%
Seek(...)100%110%
SetLength(...)100%110%
Write(...)75%4483.33%
.ctor(...)100%110%
OnWriteBodyContents(...)100%110%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/StreamBodyWriter.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.Xml;
 7
 8namespace CoreWCF.Channels
 9{
 10    public abstract class StreamBodyWriter : BodyWriter
 11    {
 12        protected StreamBodyWriter(bool isBuffered)
 413            : base(isBuffered)
 414        { }
 15
 16        protected abstract void OnWriteBodyContents(Stream stream);
 17
 18        protected override BodyWriter OnCreateBufferedCopy(int maxBufferSize)
 19        {
 020            using (BufferManagerOutputStream bufferedStream = new BufferManagerOutputStream(SR.MaxReceivedMessageSizeExc
 21            {
 022                OnWriteBodyContents(bufferedStream);
 023                byte[] bytesArray = bufferedStream.ToArray(out int size);
 024                return new BufferedBytesStreamBodyWriter(bytesArray, size);
 25            }
 026        }
 27
 28        protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
 29        {
 430            using (XmlWriterBackedStream stream = new XmlWriterBackedStream(writer))
 31            {
 432                OnWriteBodyContents(stream);
 433            }
 434        }
 35
 36        internal class XmlWriterBackedStream : Stream
 37        {
 38            private const string StreamElementName = "Binary";
 39            private readonly XmlWriter _writer;
 40
 441            public XmlWriterBackedStream(XmlWriter writer)
 42            {
 443                if (writer == null)
 44                {
 045                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer));
 46                }
 47
 448                _writer = writer;
 449            }
 50
 51            public override bool CanRead
 52            {
 053                get { return false; }
 54            }
 55
 56            public override bool CanSeek
 57            {
 458                get { return false; }
 59            }
 60
 61            public override bool CanWrite
 62            {
 063                get { return true; }
 64            }
 65
 66            public override void Flush()
 67            {
 868                _writer.Flush();
 869            }
 70
 71            public override long Length
 72            {
 73                get
 74                {
 075                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format(
 76                }
 77            }
 78
 79            public override long Position
 80            {
 81                get
 82                {
 083                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format(
 84                }
 85                set
 86                {
 087                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format(
 88                }
 89            }
 90
 91            public override int Read(byte[] buffer, int offset, int count)
 92            {
 093                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format(SR.X
 94            }
 95
 96            public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object 
 97            {
 098                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format(SR.X
 99            }
 100
 101            public override int EndRead(IAsyncResult asyncResult)
 102            {
 0103                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format(SR.X
 104            }
 105
 106            public override int ReadByte()
 107            {
 0108                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format(SR.X
 109            }
 110
 111            public override long Seek(long offset, SeekOrigin origin)
 112            {
 0113                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format(SR.X
 114            }
 115
 116            public override void SetLength(long value)
 117            {
 0118                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format(SR.X
 119            }
 120
 121            public override void Write(byte[] buffer, int offset, int count)
 122            {
 4123                if (_writer.WriteState == WriteState.Content)
 124                {
 0125                    _writer.WriteBase64(buffer, offset, count);
 126                }
 4127                else if (_writer.WriteState == WriteState.Start)
 128                {
 4129                    _writer.WriteStartElement(StreamElementName, string.Empty);
 4130                    _writer.WriteBase64(buffer, offset, count);
 131                }
 4132            }
 133        }
 134
 135        internal class BufferedBytesStreamBodyWriter : StreamBodyWriter
 136        {
 137            private readonly byte[] _array;
 138            private readonly int _size;
 139
 140            public BufferedBytesStreamBodyWriter(byte[] array, int size)
 0141                : base(true)
 142            {
 0143                _array = array;
 0144                _size = size;
 0145            }
 146
 147            protected override void OnWriteBodyContents(Stream stream)
 148            {
 0149                stream.Write(_array, 0, _size);
 0150            }
 151        }
 152    }
 153}