| | | 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 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using System.Xml; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Channels |
| | | 9 | | { |
| | | 10 | | public abstract class BodyWriter |
| | | 11 | | { |
| | | 12 | | private bool _canWrite; |
| | | 13 | | private readonly object _thisLock; |
| | | 14 | | |
| | 1607 | 15 | | protected BodyWriter(bool isBuffered) |
| | | 16 | | { |
| | 1607 | 17 | | IsBuffered = isBuffered; |
| | 1607 | 18 | | _canWrite = true; |
| | 1607 | 19 | | if (!IsBuffered) |
| | | 20 | | { |
| | 229 | 21 | | _thisLock = new object(); |
| | | 22 | | } |
| | 1607 | 23 | | } |
| | | 24 | | |
| | 3390 | 25 | | public bool IsBuffered { get; } |
| | | 26 | | |
| | | 27 | | internal virtual bool IsEmpty |
| | | 28 | | { |
| | 2 | 29 | | get { return false; } |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | internal virtual bool IsFault |
| | | 33 | | { |
| | 405 | 34 | | get { return false; } |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | public BodyWriter CreateBufferedCopy(int maxBufferSize) |
| | | 38 | | { |
| | 1 | 39 | | if (maxBufferSize < 0) |
| | | 40 | | { |
| | 0 | 41 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(maxBuff |
| | 0 | 42 | | SRCommon.ValueMustBeNonNegative)); |
| | | 43 | | } |
| | | 44 | | |
| | 1 | 45 | | if (IsBuffered) |
| | | 46 | | { |
| | 0 | 47 | | return this; |
| | | 48 | | } |
| | | 49 | | else |
| | | 50 | | { |
| | 1 | 51 | | lock (_thisLock) |
| | | 52 | | { |
| | 1 | 53 | | if (!_canWrite) |
| | | 54 | | { |
| | 0 | 55 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.BodyW |
| | | 56 | | } |
| | | 57 | | |
| | 1 | 58 | | _canWrite = false; |
| | 1 | 59 | | } |
| | 1 | 60 | | BodyWriter bodyWriter = OnCreateBufferedCopy(maxBufferSize); |
| | 1 | 61 | | if (!bodyWriter.IsBuffered) |
| | | 62 | | { |
| | 0 | 63 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.BodyWrite |
| | | 64 | | } |
| | | 65 | | |
| | 1 | 66 | | return bodyWriter; |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | protected virtual BodyWriter OnCreateBufferedCopy(int maxBufferSize) |
| | | 71 | | { |
| | 0 | 72 | | return OnCreateBufferedCopy(maxBufferSize, XmlDictionaryReaderQuotas.Max); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | internal BodyWriter OnCreateBufferedCopy(int maxBufferSize, XmlDictionaryReaderQuotas quotas) |
| | | 76 | | { |
| | 0 | 77 | | XmlBuffer buffer = new XmlBuffer(maxBufferSize); |
| | 0 | 78 | | using (XmlDictionaryWriter writer = buffer.OpenSection(quotas)) |
| | | 79 | | { |
| | 0 | 80 | | writer.WriteStartElement("a"); |
| | 0 | 81 | | OnWriteBodyContents(writer); |
| | 0 | 82 | | writer.WriteEndElement(); |
| | 0 | 83 | | } |
| | 0 | 84 | | buffer.CloseSection(); |
| | 0 | 85 | | buffer.Close(); |
| | 0 | 86 | | return new BufferedBodyWriter(buffer); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | protected abstract void OnWriteBodyContents(XmlDictionaryWriter writer); |
| | | 90 | | |
| | | 91 | | protected virtual Task OnWriteBodyContentsAsync(XmlDictionaryWriter writer) |
| | | 92 | | { |
| | 496 | 93 | | OnWriteBodyContents(writer); |
| | 496 | 94 | | return Task.CompletedTask; |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | private void EnsureWriteBodyContentsState(XmlDictionaryWriter writer) |
| | | 98 | | { |
| | 1511 | 99 | | if (writer == null) |
| | | 100 | | { |
| | 0 | 101 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | | 102 | | } |
| | | 103 | | |
| | 1511 | 104 | | if (!IsBuffered) |
| | | 105 | | { |
| | 223 | 106 | | lock (_thisLock) |
| | | 107 | | { |
| | 223 | 108 | | if (!_canWrite) |
| | | 109 | | { |
| | 0 | 110 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.BodyW |
| | | 111 | | } |
| | | 112 | | |
| | 223 | 113 | | _canWrite = false; |
| | 223 | 114 | | } |
| | | 115 | | } |
| | 1511 | 116 | | } |
| | | 117 | | |
| | | 118 | | public void WriteBodyContents(XmlDictionaryWriter writer) |
| | | 119 | | { |
| | 995 | 120 | | EnsureWriteBodyContentsState(writer); |
| | 995 | 121 | | OnWriteBodyContents(writer); |
| | 995 | 122 | | } |
| | | 123 | | |
| | | 124 | | public Task WriteBodyContentsAsync(XmlDictionaryWriter writer) |
| | | 125 | | { |
| | 516 | 126 | | EnsureWriteBodyContentsState(writer); |
| | 516 | 127 | | return OnWriteBodyContentsAsync(writer); |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | private class BufferedBodyWriter : BodyWriter |
| | | 131 | | { |
| | | 132 | | private readonly XmlBuffer _buffer; |
| | | 133 | | |
| | | 134 | | public BufferedBodyWriter(XmlBuffer buffer) |
| | 0 | 135 | | : base(true) |
| | | 136 | | { |
| | 0 | 137 | | _buffer = buffer; |
| | 0 | 138 | | } |
| | | 139 | | |
| | | 140 | | protected override void OnWriteBodyContents(XmlDictionaryWriter writer) |
| | | 141 | | { |
| | 0 | 142 | | XmlDictionaryReader reader = _buffer.GetReader(0); |
| | 0 | 143 | | using (reader) |
| | | 144 | | { |
| | 0 | 145 | | reader.ReadStartElement(); |
| | 0 | 146 | | while (reader.NodeType != XmlNodeType.EndElement) |
| | | 147 | | { |
| | 0 | 148 | | writer.WriteNode(reader, false); |
| | | 149 | | } |
| | 0 | 150 | | reader.ReadEndElement(); |
| | 0 | 151 | | } |
| | 0 | 152 | | } |
| | | 153 | | } |
| | | 154 | | } |
| | | 155 | | } |