1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Channels
{
using System;
using System.IO;
using System.Runtime;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
sealed class XmlByteStreamWriter : XmlDictionaryWriter
{
bool ownsStream;
ByteStreamWriterState state;
Stream stream;
XmlWriterSettings settings;
public XmlByteStreamWriter(Stream stream, bool ownsStream)
{
Fx.Assert(stream != null, "stream is null");
this.stream = stream;
this.ownsStream = ownsStream;
this.state = ByteStreamWriterState.Start;
}
public override WriteState WriteState
{
get { return ByteStreamWriterStateToWriteState(this.state); }
}
public override XmlWriterSettings Settings
{
get
{
if (settings == null)
{
XmlWriterSettings newSettings = new XmlWriterSettings()
{
Async = true
};
Interlocked.CompareExchange<XmlWriterSettings>(ref this.settings, newSettings, null);
}
return this.settings;
}
}
public override void Close()
{
if (this.state != ByteStreamWriterState.Closed)
{
try
{
if (ownsStream)
{
this.stream.Close();
}
this.stream = null;
}
finally
{
this.state = ByteStreamWriterState.Closed;
}
}
}
void EnsureWriteBase64State(byte[] buffer, int index, int count)
{
ThrowIfClosed();
ByteStreamMessageUtility.EnsureByteBoundaries(buffer, index, count, false);
if (this.state != ByteStreamWriterState.Content && this.state != ByteStreamWriterState.StartElement)
{
throw FxTrace.Exception.AsError(
new InvalidOperationException(SR.XmlWriterMustBeInElement(ByteStreamWriterStateToWriteState(this.state))));
}
}
public override void Flush()
{
ThrowIfClosed();
this.stream.Flush();
}
void InternalWriteEndElement()
{
ThrowIfClosed();
if (this.state != ByteStreamWriterState.StartElement && this.state != ByteStreamWriterState.Content)
{
throw FxTrace.Exception.AsError(
new InvalidOperationException(SR.XmlUnexpectedEndElement));
}
this.state = ByteStreamWriterState.EndElement;
}
public override string LookupPrefix(string ns)
{
if (ns == string.Empty)
{
return string.Empty;
}
else if (ns == ByteStreamMessageUtility.XmlNamespace)
{
return "xml";
}
else if (ns == ByteStreamMessageUtility.XmlNamespaceNamespace)
{
return "xmlns";
}
else
{
return null;
}
}
public override void WriteBase64(byte[] buffer, int index, int count)
{
EnsureWriteBase64State(buffer, index, count);
this.stream.Write(buffer, index, count);
this.state = ByteStreamWriterState.Content;
}
public override Task WriteBase64Async(byte[] buffer, int index, int count)
{
return Task.Factory.FromAsync(this.BeginWriteBase64, this.EndWriteBase64, buffer, index, count, null);
}
internal IAsyncResult BeginWriteBase64(byte[] buffer, int index, int count, AsyncCallback callback, object state)
{
EnsureWriteBase64State(buffer, index, count);
return new WriteBase64AsyncResult(buffer, index, count, this, callback, state);
}
internal void EndWriteBase64(IAsyncResult result)
{
WriteBase64AsyncResult.End(result);
}
class WriteBase64AsyncResult : AsyncResult
{
XmlByteStreamWriter writer;
public WriteBase64AsyncResult(byte[] buffer, int index, int count, XmlByteStreamWriter writer, AsyncCallback callback, object state)
: base(callback, state)
{
this.writer = writer;
IAsyncResult result = writer.stream.BeginWrite(buffer, index, count, PrepareAsyncCompletion(HandleWriteBase64), this);
bool completeSelf = SyncContinue(result);
if (completeSelf)
{
this.Complete(true);
}
}
static bool HandleWriteBase64(IAsyncResult result)
{
WriteBase64AsyncResult thisPtr = (WriteBase64AsyncResult)result.AsyncState;
thisPtr.writer.stream.EndWrite(result);
thisPtr.writer.state = ByteStreamWriterState.Content;
return true;
}
public static void End(IAsyncResult result)
{
AsyncResult.End<WriteBase64AsyncResult>(result);
}
}
public override void WriteCData(string text)
{
throw FxTrace.Exception.AsError(new NotSupportedException());
}
public override void WriteCharEntity(char ch)
{
throw FxTrace.Exception.AsError(new NotSupportedException());
}
public override void WriteChars(char[] buffer, int index, int count)
{
throw FxTrace.Exception.AsError(new NotSupportedException());
}
public override void WriteComment(string text)
{
throw FxTrace.Exception.AsError(new NotSupportedException());
}
public override void WriteDocType(string name, string pubid, string sysid, string subset)
{
throw FxTrace.Exception.AsError(new NotSupportedException());
}
public override void WriteEndAttribute()
{
throw FxTrace.Exception.AsError(new NotSupportedException());
}
public override void WriteEndDocument()
{
return;
}
public override void WriteEndElement()
{
this.InternalWriteEndElement();
}
public override void WriteEntityRef(string name)
{
throw FxTrace.Exception.AsError(new NotSupportedException());
}
public override void WriteFullEndElement()
{
this.InternalWriteEndElement();
}
public override void WriteProcessingInstruction(string name, string text)
{
throw FxTrace.Exception.AsError(new NotSupportedException());
}
public override void WriteRaw(string data)
{
throw FxTrace.Exception.AsError(new NotSupportedException());
}
public override void WriteRaw(char[] buffer, int index, int count)
{
throw FxTrace.Exception.AsError(new NotSupportedException());
}
public override void WriteStartAttribute(string prefix, string localName, string ns)
{
throw FxTrace.Exception.AsError(new NotSupportedException());
}
public override void WriteStartDocument(bool standalone)
{
ThrowIfClosed();
}
public override void WriteStartDocument()
{
ThrowIfClosed();
}
public override void WriteStartElement(string prefix, string localName, string ns)
{
ThrowIfClosed();
if (this.state != ByteStreamWriterState.Start)
{
throw FxTrace.Exception.AsError(
new InvalidOperationException(SR.ByteStreamWriteStartElementAlreadyCalled));
}
if (!string.IsNullOrEmpty(prefix) || !string.IsNullOrEmpty(ns) || localName != ByteStreamMessageUtility.StreamElementName)
{
throw FxTrace.Exception.AsError(
new XmlException(SR.XmlStartElementNameExpected(ByteStreamMessageUtility.StreamElementName, localName)));
}
this.state = ByteStreamWriterState.StartElement;
}
public override void WriteString(string text)
{
// no state checks here - WriteBase64 will take care of this.
byte[] buffer = Convert.FromBase64String(text);
WriteBase64(buffer, 0, buffer.Length);
}
public override void WriteSurrogateCharEntity(char lowChar, char highChar)
{
throw FxTrace.Exception.AsError(new NotSupportedException());
}
public override void WriteWhitespace(string ws)
{
throw FxTrace.Exception.AsError(new NotSupportedException());
}
void ThrowIfClosed()
{
if (this.state == ByteStreamWriterState.Closed)
{
throw FxTrace.Exception.AsError(
new InvalidOperationException(SR.XmlWriterClosed));
}
}
static WriteState ByteStreamWriterStateToWriteState(ByteStreamWriterState byteStreamWriterState)
{
// Converts the internal ByteStreamWriterState to an Xml WriteState
switch (byteStreamWriterState)
{
case ByteStreamWriterState.Start:
return WriteState.Start;
case ByteStreamWriterState.StartElement:
return WriteState.Element;
case ByteStreamWriterState.Content:
return WriteState.Content;
case ByteStreamWriterState.EndElement:
return WriteState.Element;
case ByteStreamWriterState.Closed:
return WriteState.Closed;
default:
return WriteState.Error;
}
}
enum ByteStreamWriterState
{
Start,
StartElement,
Content,
EndElement,
Closed
}
}
}
|