StreamBodyWriter.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx35 / System.ServiceModel.Web / System / ServiceModel / Channels / StreamBodyWriter.cs / 1305376 / StreamBodyWriter.cs

                            //---------------------------------------------------------------------------- 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//---------------------------------------------------------------------------
namespace System.ServiceModel.Channels
{ 
    using System;
    using System.IO; 
    using System.Runtime; 
    using System.ServiceModel;
    using System.Xml; 
    using DiagnosticUtility = System.ServiceModel.DiagnosticUtility;

    public abstract class StreamBodyWriter : BodyWriter
    { 
        protected StreamBodyWriter(bool isBuffered)
            : base(isBuffered) 
        { } 

        internal static StreamBodyWriter CreateStreamBodyWriter(Action streamAction) 
        {
            if (streamAction == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("actionOfStream"); 
            }
            return new ActionOfStreamBodyWriter(streamAction); 
        } 

        protected abstract void OnWriteBodyContents(Stream stream); 

        protected override BodyWriter OnCreateBufferedCopy(int maxBufferSize)
        {
            using (BufferManagerOutputStream bufferedStream = new BufferManagerOutputStream(SR2.MaxReceivedMessageSizeExceeded, maxBufferSize)) 
            {
                this.OnWriteBodyContents(bufferedStream); 
                int size; 
                byte[] bytesArray = bufferedStream.ToArray(out size);
                return new BufferedBytesStreamBodyWriter(bytesArray, size); 
            }
        }

        protected override void OnWriteBodyContents(XmlDictionaryWriter writer) 
        {
            using (XmlWriterBackedStream stream = new XmlWriterBackedStream(writer)) 
            { 
                OnWriteBodyContents(stream);
            } 
        }

        class XmlWriterBackedStream : Stream
        { 
            XmlWriter writer;
 
            public XmlWriterBackedStream(XmlWriter writer) 
            {
                if (writer == null) 
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer");
                }
                this.writer = writer; 
            }
 
            public override bool CanRead 
            {
                get { return false; } 
            }

            public override bool CanSeek
            { 
                get { return false; }
            } 
 
            public override bool CanWrite
            { 
                get { return true; }
            }

            public override void Flush() 
            {
                this.writer.Flush(); 
            } 

            public override long Length 
            {
                get
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR2.GetString(SR2.XmlWriterBackedStreamPropertyGetNotSupported, "Length"))); 
                }
            } 
 
            public override long Position
            { 
                get
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR2.GetString(SR2.XmlWriterBackedStreamPropertyGetNotSupported, "Position")));
                } 
                set
                { 
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR2.GetString(SR2.XmlWriterBackedStreamPropertySetNotSupported, "Position"))); 
                }
            } 

            public override int Read(byte[] buffer, int offset, int count)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR2.GetString(SR2.XmlWriterBackedStreamMethodNotSupported, "Read"))); 
            }
 
            public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR2.GetString(SR2.XmlWriterBackedStreamMethodNotSupported, "BeginRead"))); 
            }

            public override int EndRead(IAsyncResult asyncResult)
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR2.GetString(SR2.XmlWriterBackedStreamMethodNotSupported, "EndRead")));
            } 
 
            public override int ReadByte()
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR2.GetString(SR2.XmlWriterBackedStreamMethodNotSupported, "ReadByte")));
            }

            public override long Seek(long offset, SeekOrigin origin) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR2.GetString(SR2.XmlWriterBackedStreamMethodNotSupported, "Seek"))); 
            } 

            public override void SetLength(long value) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR2.GetString(SR2.XmlWriterBackedStreamMethodNotSupported, "SetLength")));
            }
 
            public override void Write(byte[] buffer, int offset, int count)
            { 
                this.writer.WriteBase64(buffer, offset, count); 
            }
        } 

        class BufferedBytesStreamBodyWriter : StreamBodyWriter
        {
            byte[] array; 
            int size;
 
            public BufferedBytesStreamBodyWriter(byte[] array, int size) 
                : base(true)
            { 
                this.array = array;
                this.size = size;
            }
 
            protected override void  OnWriteBodyContents(Stream stream)
            { 
                stream.Write(this.array, 0, this.size); 
            }
        } 

        class ActionOfStreamBodyWriter : StreamBodyWriter
        {
            Action actionOfStream; 

            public ActionOfStreamBodyWriter(Action actionOfStream) 
                : base(false) 
            {
                this.actionOfStream = actionOfStream; 
            }

            protected override void OnWriteBodyContents(Stream stream)
            { 
                actionOfStream(stream);
            } 
        } 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK