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
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Channels
{
using System.Xml;
using System.Diagnostics;
using System.Runtime;
using System.Threading;
public abstract class BodyWriter
{
bool isBuffered;
bool canWrite;
object thisLock;
protected BodyWriter(bool isBuffered)
{
this.isBuffered = isBuffered;
this.canWrite = true;
if (!this.isBuffered)
{
this.thisLock = new object();
}
}
public bool IsBuffered
{
get { return this.isBuffered; }
}
internal virtual bool IsEmpty
{
get { return false; }
}
internal virtual bool IsFault
{
get { return false; }
}
public BodyWriter CreateBufferedCopy(int maxBufferSize)
{
if (maxBufferSize < 0)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("maxBufferSize", maxBufferSize,
SR.GetString(SR.ValueMustBeNonNegative)));
if (this.isBuffered)
{
return this;
}
else
{
lock (this.thisLock)
{
if (!this.canWrite)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.BodyWriterCanOnlyBeWrittenOnce)));
this.canWrite = false;
}
BodyWriter bodyWriter = OnCreateBufferedCopy(maxBufferSize);
if (!bodyWriter.IsBuffered)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.BodyWriterReturnedIsNotBuffered)));
return bodyWriter;
}
}
protected virtual BodyWriter OnCreateBufferedCopy(int maxBufferSize)
{
return OnCreateBufferedCopy(maxBufferSize, XmlDictionaryReaderQuotas.Max);
}
internal BodyWriter OnCreateBufferedCopy(int maxBufferSize, XmlDictionaryReaderQuotas quotas)
{
XmlBuffer buffer = new XmlBuffer(maxBufferSize);
using (XmlDictionaryWriter writer = buffer.OpenSection(quotas))
{
writer.WriteStartElement("a");
OnWriteBodyContents(writer);
writer.WriteEndElement();
}
buffer.CloseSection();
buffer.Close();
return new BufferedBodyWriter(buffer);
}
protected abstract void OnWriteBodyContents(XmlDictionaryWriter writer);
protected virtual IAsyncResult OnBeginWriteBodyContents(XmlDictionaryWriter writer, AsyncCallback callback, object state)
{
return new OnWriteBodyContentsAsyncResult(writer, this, callback, state);
}
protected virtual void OnEndWriteBodyContents(IAsyncResult result)
{
OnWriteBodyContentsAsyncResult.End(result);
}
void EnsureWriteBodyContentsState(XmlDictionaryWriter writer)
{
if (writer == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("writer"));
if (!this.isBuffered)
{
lock (this.thisLock)
{
if (!this.canWrite)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.BodyWriterCanOnlyBeWrittenOnce)));
this.canWrite = false;
}
}
}
public void WriteBodyContents(XmlDictionaryWriter writer)
{
EnsureWriteBodyContentsState(writer);
OnWriteBodyContents(writer);
}
public IAsyncResult BeginWriteBodyContents(XmlDictionaryWriter writer, AsyncCallback callback, object state)
{
EnsureWriteBodyContentsState(writer);
return OnBeginWriteBodyContents(writer, callback, state);
}
public void EndWriteBodyContents(IAsyncResult result)
{
OnEndWriteBodyContents(result);
}
class BufferedBodyWriter : BodyWriter
{
XmlBuffer buffer;
public BufferedBodyWriter(XmlBuffer buffer)
: base(true)
{
this.buffer = buffer;
}
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
XmlDictionaryReader reader = this.buffer.GetReader(0);
using (reader)
{
reader.ReadStartElement();
while (reader.NodeType != XmlNodeType.EndElement)
{
writer.WriteNode(reader, false);
}
reader.ReadEndElement();
}
}
}
class OnWriteBodyContentsAsyncResult : ScheduleActionItemAsyncResult
{
BodyWriter bodyWriter;
XmlDictionaryWriter writer;
public OnWriteBodyContentsAsyncResult(XmlDictionaryWriter writer, BodyWriter bodyWriter, AsyncCallback callback, object state)
: base(callback, state)
{
Fx.Assert(bodyWriter != null, "bodyWriter should never be null");
this.writer = writer;
this.bodyWriter = bodyWriter;
Schedule();
}
protected override void OnDoWork()
{
this.bodyWriter.OnWriteBodyContents(this.writer);
}
}
}
}
|