< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.XmlBuffer
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/XmlBuffer.cs
Line coverage
70%
Covered lines: 42
Uncovered lines: 18
Coverable lines: 60
Total lines: 165
Line coverage: 70%
Branch coverage
42%
Covered branches: 6
Total branches: 14
Branch coverage: 42.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
.ctor(...)50%2277.77%
OpenSection(...)50%4463.63%
CloseSection()50%2288.88%
Close()50%2285.71%
CreateInvalidStateException()100%110%
GetReader(...)50%2283.33%
WriteTo(...)0%220%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/XmlBuffer.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.Collections.Generic;
 6using System.Xml;
 7using CoreWCF.Channels;
 8using CoreWCF.Runtime;
 9
 10namespace CoreWCF
 11{
 12    internal class XmlBuffer
 13    {
 14        private readonly List<Section> _sections;
 15        private byte[] _buffer;
 16        private int _offset;
 17        private BufferedOutputStream _stream;
 18        private BufferState _bufferState;
 19        private XmlDictionaryWriter _writer;
 20        private XmlDictionaryReaderQuotas _quotas;
 21
 22        private enum BufferState
 23        {
 24            Created,
 25            Writing,
 26            Reading,
 27        }
 28
 29        private struct Section
 30        {
 31            public Section(int offset, int size, XmlDictionaryReaderQuotas quotas)
 32            {
 1848433                Offset = offset;
 1848434                Size = size;
 1848435                Quotas = quotas;
 1848436            }
 37
 3806838            public int Offset { get; }
 39
 3806840            public int Size { get; }
 41
 3806842            public XmlDictionaryReaderQuotas Quotas { get; }
 43        }
 44
 313245        public XmlBuffer(int maxBufferSize)
 46        {
 313247            if (maxBufferSize < 0)
 48            {
 049                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(maxBuff
 050                                                    SRCommon.ValueMustBeNonNegative));
 51            }
 52
 313253            int initialBufferSize = Math.Min(512, maxBufferSize);
 313254            _stream = new BufferManagerOutputStream(SRCommon.XmlBufferQuotaExceeded, initialBufferSize, maxBufferSize,
 313255                BufferManager.CreateBufferManager(0, int.MaxValue));
 313256            _sections = new List<Section>(1);
 313257        }
 58
 59        public int BufferSize
 60        {
 61            get
 62            {
 63                Fx.Assert(_bufferState == BufferState.Reading, "Buffer size should only be retrieved during Reading stat
 293764                return _buffer.Length;
 65            }
 66        }
 67
 68        public int SectionCount
 69        {
 1828970            get { return _sections.Count; }
 71        }
 72
 73        public XmlDictionaryWriter OpenSection(XmlDictionaryReaderQuotas quotas)
 74        {
 1848475            if (_bufferState != BufferState.Created)
 76            {
 077                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateInvalidStateException());
 78            }
 79
 1848480            _bufferState = BufferState.Writing;
 1848481            _quotas = new XmlDictionaryReaderQuotas();
 1848482            quotas.CopyTo(_quotas);
 1848483            if (_writer != null)
 84
 85            {
 86                // We always want to Dispose of the writer now; previously, writers could be reassigned
 87
 88                // to a new stream, with a new dictionary and session.
 89
 090                XmlDictionaryWriter thisWriter = _writer;
 91
 092                thisWriter.Dispose();
 93
 094                _writer = null;
 95            }
 96
 1848497            _writer = XmlDictionaryWriter.CreateBinaryWriter(_stream, XD.Dictionary, null, true);
 1848498            return _writer;
 99        }
 100
 101        public void CloseSection()
 102        {
 18484103            if (_bufferState != BufferState.Writing)
 104            {
 0105                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateInvalidStateException());
 106            }
 107
 18484108            _writer.Dispose();
 18484109            _writer = null;
 18484110            _bufferState = BufferState.Created;
 18484111            int size = (int)_stream.Length - _offset;
 18484112            _sections.Add(new Section(_offset, size, _quotas));
 18484113            _offset += size;
 18484114        }
 115
 116        public void Close()
 117        {
 3132118            if (_bufferState != BufferState.Created)
 119            {
 0120                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateInvalidStateException());
 121            }
 122
 3132123            _bufferState = BufferState.Reading;
 3132124            _buffer = _stream.ToArray(out int bufferSize);
 3132125            _writer = null;
 3132126            _stream = null;
 3132127        }
 128
 129        private Exception CreateInvalidStateException()
 130        {
 0131            return new InvalidOperationException(SR.XmlBufferInInvalidState);
 132        }
 133
 134        public XmlDictionaryReader GetReader(int sectionIndex)
 135        {
 38068136            if (_bufferState != BufferState.Reading)
 137            {
 0138                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateInvalidStateException());
 139            }
 140
 38068141            Section section = _sections[sectionIndex];
 38068142            XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(_buffer, section.Offset, section.Size, X
 38068143            reader.MoveToContent();
 38068144            return reader;
 145        }
 146
 147        public void WriteTo(int sectionIndex, XmlWriter writer)
 148        {
 0149            if (_bufferState != BufferState.Reading)
 150            {
 0151                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateInvalidStateException());
 152            }
 153
 0154            XmlDictionaryReader reader = GetReader(sectionIndex);
 155            try
 156            {
 0157                writer.WriteNode(reader, false);
 0158            }
 159            finally
 160            {
 0161                reader.Dispose();
 0162            }
 0163        }
 164    }
 165}