| | | 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.IO; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using System.Xml; |
| | | 9 | | using CoreWCF.Runtime; |
| | | 10 | | |
| | | 11 | | namespace 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 | | |
| | 6 | 20 | | public XmlByteStreamWriter(Stream stream, bool ownsStream) |
| | | 21 | | { |
| | | 22 | | Fx.Assert(stream != null, "stream is null"); |
| | | 23 | | |
| | 6 | 24 | | _stream = stream; |
| | 6 | 25 | | _ownsStream = ownsStream; |
| | 6 | 26 | | _state = ByteStreamWriterState.Start; |
| | 6 | 27 | | } |
| | | 28 | | |
| | 14 | 29 | | public override WriteState WriteState => ByteStreamWriterStateToWriteState(_state); |
| | | 30 | | |
| | | 31 | | public override XmlWriterSettings Settings |
| | | 32 | | { |
| | | 33 | | get |
| | | 34 | | { |
| | 0 | 35 | | if (_settings == null) |
| | | 36 | | { |
| | 0 | 37 | | XmlWriterSettings newSettings = new XmlWriterSettings() |
| | 0 | 38 | | { |
| | 0 | 39 | | Async = true |
| | 0 | 40 | | }; |
| | | 41 | | |
| | 0 | 42 | | Interlocked.CompareExchange(ref _settings, newSettings, null); |
| | | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | return _settings; |
| | | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | public override void Close() |
| | | 50 | | { |
| | 6 | 51 | | if (_state != ByteStreamWriterState.Closed) |
| | | 52 | | { |
| | | 53 | | try |
| | | 54 | | { |
| | 6 | 55 | | if (_ownsStream) |
| | | 56 | | { |
| | 6 | 57 | | _stream.Close(); |
| | | 58 | | } |
| | 6 | 59 | | _stream = null; |
| | 6 | 60 | | } |
| | | 61 | | finally |
| | | 62 | | { |
| | 6 | 63 | | _state = ByteStreamWriterState.Closed; |
| | 6 | 64 | | } |
| | | 65 | | } |
| | 6 | 66 | | } |
| | | 67 | | |
| | | 68 | | private void EnsureWriteBase64State(byte[] buffer, int index, int count) |
| | | 69 | | { |
| | 6 | 70 | | ThrowIfClosed(); |
| | 6 | 71 | | ByteStreamMessageUtility.EnsureByteBoundaries(buffer, index, count, false); |
| | | 72 | | |
| | 6 | 73 | | if (_state != ByteStreamWriterState.Content && _state != ByteStreamWriterState.StartElement) |
| | | 74 | | { |
| | 0 | 75 | | throw Fx.Exception.AsError( |
| | 0 | 76 | | new InvalidOperationException(SR.Format(SR.XmlWriterMustBeInElement, ByteStreamWriterStateToWriteSta |
| | | 77 | | } |
| | 6 | 78 | | } |
| | | 79 | | |
| | | 80 | | public override void Flush() |
| | | 81 | | { |
| | 14 | 82 | | ThrowIfClosed(); |
| | 14 | 83 | | _stream.Flush(); |
| | 14 | 84 | | } |
| | | 85 | | |
| | | 86 | | public override Task FlushAsync() |
| | | 87 | | { |
| | 0 | 88 | | ThrowIfClosed(); |
| | 0 | 89 | | return _stream.FlushAsync(); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | private void InternalWriteEndElement() |
| | | 93 | | { |
| | 2 | 94 | | ThrowIfClosed(); |
| | 2 | 95 | | if (_state != ByteStreamWriterState.StartElement && _state != ByteStreamWriterState.Content) |
| | | 96 | | { |
| | 0 | 97 | | throw Fx.Exception.AsError( |
| | 0 | 98 | | new InvalidOperationException(SR.XmlUnexpectedEndElement)); |
| | | 99 | | } |
| | | 100 | | |
| | 2 | 101 | | _state = ByteStreamWriterState.EndElement; |
| | 2 | 102 | | } |
| | | 103 | | |
| | | 104 | | public override string LookupPrefix(string ns) |
| | | 105 | | { |
| | 0 | 106 | | if (ns == string.Empty) |
| | | 107 | | { |
| | 0 | 108 | | return string.Empty; |
| | | 109 | | } |
| | 0 | 110 | | else if (ns == ByteStreamMessageUtility.XmlNamespace) |
| | | 111 | | { |
| | 0 | 112 | | return "xml"; |
| | | 113 | | } |
| | 0 | 114 | | else if (ns == ByteStreamMessageUtility.XmlNamespaceNamespace) |
| | | 115 | | { |
| | 0 | 116 | | return "xmlns"; |
| | | 117 | | } |
| | | 118 | | else |
| | | 119 | | { |
| | 0 | 120 | | return null; |
| | | 121 | | } |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | public override void WriteBase64(byte[] buffer, int index, int count) |
| | | 125 | | { |
| | 6 | 126 | | EnsureWriteBase64State(buffer, index, count); |
| | 6 | 127 | | _stream.Write(buffer, index, count); |
| | 6 | 128 | | _state = ByteStreamWriterState.Content; |
| | 6 | 129 | | } |
| | | 130 | | |
| | | 131 | | public override async Task WriteBase64Async(byte[] buffer, int index, int count) |
| | | 132 | | { |
| | | 133 | | |
| | 0 | 134 | | EnsureWriteBase64State(buffer, index, count); |
| | 0 | 135 | | await _stream.WriteAsync(buffer, index, count); |
| | 0 | 136 | | _state = ByteStreamWriterState.Content; |
| | 0 | 137 | | } |
| | | 138 | | |
| | 0 | 139 | | public override void WriteCData(string text) => throw Fx.Exception.AsError(new NotSupportedException()); |
| | | 140 | | |
| | 0 | 141 | | public override void WriteCharEntity(char ch) => throw Fx.Exception.AsError(new NotSupportedException()); |
| | | 142 | | |
| | 0 | 143 | | public override void WriteChars(char[] buffer, int index, int count) => throw Fx.Exception.AsError(new NotSuppor |
| | | 144 | | |
| | 0 | 145 | | public override void WriteComment(string text) => throw Fx.Exception.AsError(new NotSupportedException()); |
| | | 146 | | |
| | 0 | 147 | | public override void WriteDocType(string name, string pubid, string sysid, string subset) => throw Fx.Exception. |
| | | 148 | | |
| | 0 | 149 | | public override void WriteEndAttribute() => throw Fx.Exception.AsError(new NotSupportedException()); |
| | | 150 | | |
| | | 151 | | public override void WriteEndDocument() |
| | | 152 | | { |
| | 0 | 153 | | } |
| | | 154 | | |
| | | 155 | | public override void WriteEndElement() |
| | | 156 | | { |
| | 2 | 157 | | InternalWriteEndElement(); |
| | 2 | 158 | | } |
| | | 159 | | |
| | 0 | 160 | | public override void WriteEntityRef(string name) => throw Fx.Exception.AsError(new NotSupportedException()); |
| | | 161 | | |
| | | 162 | | public override void WriteFullEndElement() |
| | | 163 | | { |
| | 0 | 164 | | InternalWriteEndElement(); |
| | 0 | 165 | | } |
| | | 166 | | |
| | 0 | 167 | | public override void WriteProcessingInstruction(string name, string text) => throw Fx.Exception.AsError(new NotS |
| | | 168 | | |
| | 0 | 169 | | public override void WriteRaw(string data) => throw Fx.Exception.AsError(new NotSupportedException()); |
| | | 170 | | |
| | 0 | 171 | | public override void WriteRaw(char[] buffer, int index, int count) => throw Fx.Exception.AsError(new NotSupporte |
| | | 172 | | |
| | 0 | 173 | | public override void WriteStartAttribute(string prefix, string localName, string ns) => throw Fx.Exception.AsErr |
| | | 174 | | |
| | | 175 | | public override void WriteStartDocument(bool standalone) |
| | | 176 | | { |
| | 0 | 177 | | ThrowIfClosed(); |
| | 0 | 178 | | } |
| | | 179 | | |
| | | 180 | | public override void WriteStartDocument() |
| | | 181 | | { |
| | 0 | 182 | | ThrowIfClosed(); |
| | 0 | 183 | | } |
| | | 184 | | |
| | | 185 | | public override void WriteStartElement(string prefix, string localName, string ns) |
| | | 186 | | { |
| | 6 | 187 | | ThrowIfClosed(); |
| | 6 | 188 | | if (_state != ByteStreamWriterState.Start) |
| | | 189 | | { |
| | 0 | 190 | | throw Fx.Exception.AsError( |
| | 0 | 191 | | new InvalidOperationException(SR.ByteStreamWriteStartElementAlreadyCalled)); |
| | | 192 | | } |
| | | 193 | | |
| | 6 | 194 | | if (!string.IsNullOrEmpty(prefix) || !string.IsNullOrEmpty(ns) || localName != ByteStreamMessageUtility.Stre |
| | | 195 | | { |
| | 0 | 196 | | throw Fx.Exception.AsError( |
| | 0 | 197 | | new XmlException(SR.Format(SR.XmlStartElementNameExpected, ByteStreamMessageUtility.StreamElementNam |
| | | 198 | | } |
| | | 199 | | |
| | 6 | 200 | | _state = ByteStreamWriterState.StartElement; |
| | 6 | 201 | | } |
| | | 202 | | |
| | | 203 | | public override void WriteString(string text) |
| | | 204 | | { |
| | | 205 | | // no state checks here - WriteBase64 will take care of this. |
| | 0 | 206 | | byte[] buffer = Convert.FromBase64String(text); |
| | 0 | 207 | | WriteBase64(buffer, 0, buffer.Length); |
| | 0 | 208 | | } |
| | | 209 | | |
| | 0 | 210 | | public override void WriteSurrogateCharEntity(char lowChar, char highChar) => throw Fx.Exception.AsError(new Not |
| | | 211 | | |
| | 0 | 212 | | public override void WriteWhitespace(string ws) => throw Fx.Exception.AsError(new NotSupportedException()); |
| | | 213 | | |
| | | 214 | | private void ThrowIfClosed() |
| | | 215 | | { |
| | 28 | 216 | | if (_state == ByteStreamWriterState.Closed) |
| | | 217 | | { |
| | 0 | 218 | | throw Fx.Exception.AsError( |
| | 0 | 219 | | new InvalidOperationException(SR.XmlWriterClosed)); |
| | | 220 | | } |
| | 28 | 221 | | } |
| | | 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: |
| | 8 | 229 | | return WriteState.Start; |
| | | 230 | | case ByteStreamWriterState.StartElement: |
| | 0 | 231 | | return WriteState.Element; |
| | | 232 | | case ByteStreamWriterState.Content: |
| | 4 | 233 | | return WriteState.Content; |
| | | 234 | | case ByteStreamWriterState.EndElement: |
| | 2 | 235 | | return WriteState.Element; |
| | | 236 | | case ByteStreamWriterState.Closed: |
| | 0 | 237 | | return WriteState.Closed; |
| | | 238 | | default: |
| | 0 | 239 | | return WriteState.Error; |
| | | 240 | | } |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | private enum ByteStreamWriterState |
| | | 244 | | { |
| | | 245 | | Start, |
| | | 246 | | StartElement, |
| | | 247 | | Content, |
| | | 248 | | EndElement, |
| | | 249 | | Closed |
| | | 250 | | } |
| | | 251 | | } |
| | | 252 | | } |