< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.XmlByteStreamWriter
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/XmlByteStreamWriter.cs
Line coverage
42%
Covered lines: 41
Uncovered lines: 56
Coverable lines: 97
Total lines: 252
Line coverage: 42.2%
Branch coverage
50%
Covered branches: 18
Total branches: 36
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/XmlByteStreamWriter.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.Threading;
 7using System.Threading.Tasks;
 8using System.Xml;
 9using CoreWCF.Runtime;
 10
 11namespace CoreWCF.Channels
 12{
 13    internal sealed class XmlByteStreamWriter : XmlDictionaryWriter
 14    {
 15        private readonly bool _ownsStream;
 16        private ByteStreamWriterState _state;
 17        private Stream _stream;
 18        private XmlWriterSettings _settings;
 19
 620        public XmlByteStreamWriter(Stream stream, bool ownsStream)
 21        {
 22            Fx.Assert(stream != null, "stream is null");
 23
 624            _stream = stream;
 625            _ownsStream = ownsStream;
 626            _state = ByteStreamWriterState.Start;
 627        }
 28
 1429        public override WriteState WriteState => ByteStreamWriterStateToWriteState(_state);
 30
 31        public override XmlWriterSettings Settings
 32        {
 33            get
 34            {
 035                if (_settings == null)
 36                {
 037                    XmlWriterSettings newSettings = new XmlWriterSettings()
 038                    {
 039                        Async = true
 040                    };
 41
 042                    Interlocked.CompareExchange(ref _settings, newSettings, null);
 43                }
 44
 045                return _settings;
 46            }
 47        }
 48
 49        public override void Close()
 50        {
 651            if (_state != ByteStreamWriterState.Closed)
 52            {
 53                try
 54                {
 655                    if (_ownsStream)
 56                    {
 657                        _stream.Close();
 58                    }
 659                    _stream = null;
 660                }
 61                finally
 62                {
 663                    _state = ByteStreamWriterState.Closed;
 664                }
 65            }
 666        }
 67
 68        private void EnsureWriteBase64State(byte[] buffer, int index, int count)
 69        {
 670            ThrowIfClosed();
 671            ByteStreamMessageUtility.EnsureByteBoundaries(buffer, index, count, false);
 72
 673            if (_state != ByteStreamWriterState.Content && _state != ByteStreamWriterState.StartElement)
 74            {
 075                throw Fx.Exception.AsError(
 076                    new InvalidOperationException(SR.Format(SR.XmlWriterMustBeInElement, ByteStreamWriterStateToWriteSta
 77            }
 678        }
 79
 80        public override void Flush()
 81        {
 1482            ThrowIfClosed();
 1483            _stream.Flush();
 1484        }
 85
 86        public override Task FlushAsync()
 87        {
 088            ThrowIfClosed();
 089            return _stream.FlushAsync();
 90        }
 91
 92        private void InternalWriteEndElement()
 93        {
 294            ThrowIfClosed();
 295            if (_state != ByteStreamWriterState.StartElement && _state != ByteStreamWriterState.Content)
 96            {
 097                throw Fx.Exception.AsError(
 098                    new InvalidOperationException(SR.XmlUnexpectedEndElement));
 99            }
 100
 2101            _state = ByteStreamWriterState.EndElement;
 2102        }
 103
 104        public override string LookupPrefix(string ns)
 105        {
 0106            if (ns == string.Empty)
 107            {
 0108                return string.Empty;
 109            }
 0110            else if (ns == ByteStreamMessageUtility.XmlNamespace)
 111            {
 0112                return "xml";
 113            }
 0114            else if (ns == ByteStreamMessageUtility.XmlNamespaceNamespace)
 115            {
 0116                return "xmlns";
 117            }
 118            else
 119            {
 0120                return null;
 121            }
 122        }
 123
 124        public override void WriteBase64(byte[] buffer, int index, int count)
 125        {
 6126            EnsureWriteBase64State(buffer, index, count);
 6127            _stream.Write(buffer, index, count);
 6128            _state = ByteStreamWriterState.Content;
 6129        }
 130
 131        public override async Task WriteBase64Async(byte[] buffer, int index, int count)
 132        {
 133
 0134            EnsureWriteBase64State(buffer, index, count);
 0135            await _stream.WriteAsync(buffer, index, count);
 0136            _state = ByteStreamWriterState.Content;
 0137        }
 138
 0139        public override void WriteCData(string text) => throw Fx.Exception.AsError(new NotSupportedException());
 140
 0141        public override void WriteCharEntity(char ch) => throw Fx.Exception.AsError(new NotSupportedException());
 142
 0143        public override void WriteChars(char[] buffer, int index, int count) => throw Fx.Exception.AsError(new NotSuppor
 144
 0145        public override void WriteComment(string text) => throw Fx.Exception.AsError(new NotSupportedException());
 146
 0147        public override void WriteDocType(string name, string pubid, string sysid, string subset) => throw Fx.Exception.
 148
 0149        public override void WriteEndAttribute() => throw Fx.Exception.AsError(new NotSupportedException());
 150
 151        public override void WriteEndDocument()
 152        {
 0153        }
 154
 155        public override void WriteEndElement()
 156        {
 2157            InternalWriteEndElement();
 2158        }
 159
 0160        public override void WriteEntityRef(string name) => throw Fx.Exception.AsError(new NotSupportedException());
 161
 162        public override void WriteFullEndElement()
 163        {
 0164            InternalWriteEndElement();
 0165        }
 166
 0167        public override void WriteProcessingInstruction(string name, string text) => throw Fx.Exception.AsError(new NotS
 168
 0169        public override void WriteRaw(string data) => throw Fx.Exception.AsError(new NotSupportedException());
 170
 0171        public override void WriteRaw(char[] buffer, int index, int count) => throw Fx.Exception.AsError(new NotSupporte
 172
 0173        public override void WriteStartAttribute(string prefix, string localName, string ns) => throw Fx.Exception.AsErr
 174
 175        public override void WriteStartDocument(bool standalone)
 176        {
 0177            ThrowIfClosed();
 0178        }
 179
 180        public override void WriteStartDocument()
 181        {
 0182            ThrowIfClosed();
 0183        }
 184
 185        public override void WriteStartElement(string prefix, string localName, string ns)
 186        {
 6187            ThrowIfClosed();
 6188            if (_state != ByteStreamWriterState.Start)
 189            {
 0190                throw Fx.Exception.AsError(
 0191                    new InvalidOperationException(SR.ByteStreamWriteStartElementAlreadyCalled));
 192            }
 193
 6194            if (!string.IsNullOrEmpty(prefix) || !string.IsNullOrEmpty(ns) || localName != ByteStreamMessageUtility.Stre
 195            {
 0196                throw Fx.Exception.AsError(
 0197                    new XmlException(SR.Format(SR.XmlStartElementNameExpected, ByteStreamMessageUtility.StreamElementNam
 198            }
 199
 6200            _state = ByteStreamWriterState.StartElement;
 6201        }
 202
 203        public override void WriteString(string text)
 204        {
 205            // no state checks here - WriteBase64 will take care of this.
 0206            byte[] buffer = Convert.FromBase64String(text);
 0207            WriteBase64(buffer, 0, buffer.Length);
 0208        }
 209
 0210        public override void WriteSurrogateCharEntity(char lowChar, char highChar) => throw Fx.Exception.AsError(new Not
 211
 0212        public override void WriteWhitespace(string ws) => throw Fx.Exception.AsError(new NotSupportedException());
 213
 214        private void ThrowIfClosed()
 215        {
 28216            if (_state == ByteStreamWriterState.Closed)
 217            {
 0218                throw Fx.Exception.AsError(
 0219                    new InvalidOperationException(SR.XmlWriterClosed));
 220            }
 28221        }
 222
 223        private static WriteState ByteStreamWriterStateToWriteState(ByteStreamWriterState byteStreamWriterState)
 224        {
 225            // Converts the internal ByteStreamWriterState to an Xml WriteState
 226            switch (byteStreamWriterState)
 227            {
 228                case ByteStreamWriterState.Start:
 8229                    return WriteState.Start;
 230                case ByteStreamWriterState.StartElement:
 0231                    return WriteState.Element;
 232                case ByteStreamWriterState.Content:
 4233                    return WriteState.Content;
 234                case ByteStreamWriterState.EndElement:
 2235                    return WriteState.Element;
 236                case ByteStreamWriterState.Closed:
 0237                    return WriteState.Closed;
 238                default:
 0239                    return WriteState.Error;
 240            }
 241        }
 242
 243        private enum ByteStreamWriterState
 244        {
 245            Start,
 246            StartElement,
 247            Content,
 248            EndElement,
 249            Closed
 250        }
 251    }
 252}