Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / System.ServiceModel.Channels / System / ServiceModel / Channels / XmlByteStreamWriter.cs / 1305376 / XmlByteStreamWriter.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.ServiceModel.Channels { using System; using System.IO; using System.Runtime; using System.Xml; sealed class XmlByteStreamWriter : XmlDictionaryWriter { bool ownsStream; ByteStreamWriterState state; Stream stream; 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 void Close() { if (this.state != ByteStreamWriterState.Closed) { try { if (ownsStream) { this.stream.Close(); } this.stream = null; } finally { this.state = ByteStreamWriterState.Closed; } } } 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) { ThrowIfClosed(); ByteStreamMessageUtility.EnsureByteBoundaries(buffer, index, count); if (this.state != ByteStreamWriterState.Content && this.state != ByteStreamWriterState.StartElement) { throw FxTrace.Exception.AsError( new InvalidOperationException(SR.XmlWriterMustBeInElement(ByteStreamWriterStateToWriteState(this.state)))); } this.stream.Write(buffer, index, count); this.state = ByteStreamWriterState.Content; } 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)); } } 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 } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Model3D.cs
- MergeFilterQuery.cs
- SpeechDetectedEventArgs.cs
- XmlCustomFormatter.cs
- ClusterRegistryConfigurationProvider.cs
- Win32.cs
- TextWriter.cs
- NativeMethods.cs
- NullableDecimalAverageAggregationOperator.cs
- EventPrivateKey.cs
- StorageSetMapping.cs
- RoutedEventValueSerializer.cs
- ShaderEffect.cs
- InkPresenter.cs
- XmlILCommand.cs
- DefaultPrintController.cs
- PrintPageEvent.cs
- ModuleBuilderData.cs
- TypeGenericEnumerableViewSchema.cs
- LongValidator.cs
- SQLDateTimeStorage.cs
- SqlNotificationEventArgs.cs
- HwndSourceKeyboardInputSite.cs
- RemotingConfigParser.cs
- ReceiveMessageAndVerifySecurityAsyncResultBase.cs
- ConfigPathUtility.cs
- DataListItemCollection.cs
- SqlNotificationEventArgs.cs
- TimeSpanMinutesOrInfiniteConverter.cs
- DefaultValueTypeConverter.cs
- Nodes.cs
- QuaternionKeyFrameCollection.cs
- SequentialUshortCollection.cs
- CompiledIdentityConstraint.cs
- COM2ComponentEditor.cs
- DesignTimeVisibleAttribute.cs
- PageCodeDomTreeGenerator.cs
- DesignerCatalogPartChrome.cs
- ForeignKeyConstraint.cs
- DataGridTablesFactory.cs
- RangeValueProviderWrapper.cs
- Int16Storage.cs
- XmlLanguageConverter.cs
- SecurityDocument.cs
- ConfigUtil.cs
- Math.cs
- RadioButtonDesigner.cs
- XPathDocumentNavigator.cs
- TextDecorationCollection.cs
- remotingproxy.cs
- ProcessDesigner.cs
- ActivationArguments.cs
- BoundsDrawingContextWalker.cs
- BindingExpression.cs
- Panel.cs
- LicenseException.cs
- ThemeableAttribute.cs
- DataServiceKeyAttribute.cs
- XhtmlBasicTextViewAdapter.cs
- DataKeyArray.cs
- XPathNodeIterator.cs
- LoginAutoFormat.cs
- storepermissionattribute.cs
- FlatButtonAppearance.cs
- AttributeCollection.cs
- baseshape.cs
- TreeNodeEventArgs.cs
- CompilerWrapper.cs
- DataTableReaderListener.cs
- Parser.cs
- UnsafeNativeMethods.cs
- ResourceAssociationTypeEnd.cs
- SynchronizingStream.cs
- NativeObjectSecurity.cs
- ParentQuery.cs
- XPathDescendantIterator.cs
- FunctionImportMapping.cs
- RequestCacheValidator.cs
- ViewSimplifier.cs
- DataGridViewRowPrePaintEventArgs.cs
- ReachSerializationCacheItems.cs
- PermissionSetTriple.cs
- DrawListViewColumnHeaderEventArgs.cs
- FileDataSourceCache.cs
- PtsHelper.cs
- DataTablePropertyDescriptor.cs
- Image.cs
- objectresult_tresulttype.cs
- WebPartManagerInternals.cs
- WebPartConnection.cs
- TreeNode.cs
- ExtendedProperty.cs
- ReachPageContentSerializer.cs
- smtpconnection.cs
- ResourcePermissionBaseEntry.cs
- JapaneseLunisolarCalendar.cs
- TextBox.cs
- DataGridViewRow.cs
- BaseWebProxyFinder.cs
- UdpConstants.cs