internal virtual AsyncCompletionResult WriteBase64TextAsync(AsyncEventArgs<XmlNodeWriterWriteBase64TextArgs> state)
 {
     // We do not guard this invocation. The caller of the NodeWriter should ensure that that 
     // they override the FastAsync guard clause for the XmlDictionaryWriter and that the 
     // nodeWriter has an implemenation for WriteBase64TextAsync.
     throw Fx.AssertAndThrow("WriteBase64TextAsync not implemented.");
 }
            AsyncCompletionResult InternalWriteBase64TextAsync(AsyncEventArgs<XmlWriteBase64AsyncArguments> writerState)
            {
                GetBufferAsyncEventArgs bufferState = this.getBufferState;
                GetBufferArgs bufferArgs = this.getBufferArgs;
                XmlWriteBase64AsyncArguments writerArgs = writerState.Arguments;

                if (bufferState == null)
                {
                    // Need to initialize the cached getBufferState 
                    // used to call GetBufferAsync() multiple times.
                    bufferState = new GetBufferAsyncEventArgs();
                    bufferArgs = new GetBufferArgs();
                    this.getBufferState = bufferState;
                    this.getBufferArgs = bufferArgs;
                }

                Base64Encoding encoding = XmlConverter.Base64Encoding;

                while (writerArgs.Count >= 3)
                {
                    int byteCount = Math.Min(bufferLength / 4 * 3, writerArgs.Count - writerArgs.Count % 3);
                    int charCount = byteCount / 3 * 4;

                    bufferArgs.Count = charCount;
                    bufferState.Set(onGetBufferComplete, bufferArgs, this);
                    if (writer.GetBufferAsync(bufferState) == AsyncCompletionResult.Completed)
                    {
                        GetBufferEventResult getbufferResult = bufferState.Result;
                        bufferState.Complete(true);
                        writer.Advance(encoding.GetChars(
                            writerArgs.Buffer,
                            writerArgs.Offset,
                            byteCount,
                            getbufferResult.Buffer,
                            getbufferResult.Offset));
                        writerArgs.Offset += byteCount;
                        writerArgs.Count -= byteCount;
                    }
                    else
                    {
                        return AsyncCompletionResult.Queued;
                    }
                }

                if (writerArgs.Count > 0)
                {
                    bufferArgs.Count = 4;
                    bufferState.Set(onGetBufferComplete, bufferArgs, this);
                    if (writer.GetBufferAsync(bufferState) == AsyncCompletionResult.Completed)
                    {
                        GetBufferEventResult getbufferResult = bufferState.Result;
                        bufferState.Complete(true);
                        writer.Advance(encoding.GetChars(
                            writerArgs.Buffer,
                            writerArgs.Offset,
                            writerArgs.Count,
                            getbufferResult.Buffer,
                            getbufferResult.Offset));
                    }
                    else
                    {
                        return AsyncCompletionResult.Queued;
                    }
                }

                return AsyncCompletionResult.Completed;
            }
            internal AsyncCompletionResult StartAsync(AsyncEventArgs<XmlNodeWriterWriteBase64TextArgs> xmlNodeWriterState)
            {
                Fx.Assert(xmlNodeWriterState != null, "xmlNodeWriterState cannot be null.");
                Fx.Assert(this.nodeState == null, "nodeState is not null.");

                this.nodeState = xmlNodeWriterState;
                XmlNodeWriterWriteBase64TextArgs nodeWriterArgs = xmlNodeWriterState.Arguments;

                if (nodeWriterArgs.TrailCount > 0)
                {
                    this.writerArgs.Buffer = nodeWriterArgs.TrailBuffer;
                    this.writerArgs.Offset = 0;
                    this.writerArgs.Count = nodeWriterArgs.TrailCount;

                    this.writerState.Set(onTrailByteComplete, this.writerArgs, this);
                    if (this.InternalWriteBase64TextAsync(this.writerState) != AsyncCompletionResult.Completed)
                    {
                        return AsyncCompletionResult.Queued;
                    }
                    this.writerState.Complete(true);
                }

                if (this.WriteBufferAsync() == AsyncCompletionResult.Completed)
                {
                    this.nodeState = null;
                    return AsyncCompletionResult.Completed;
                }

                return AsyncCompletionResult.Queued;
            }
 public InternalWriteBase64TextAsyncWriter(XmlUTF8NodeWriter writer)
 {
     this.writer = writer;
     this.writerState = new AsyncEventArgs<XmlWriteBase64AsyncArguments>();
     this.writerArgs = new XmlWriteBase64AsyncArguments();
 }
        internal override AsyncCompletionResult WriteBase64TextAsync(AsyncEventArgs<XmlNodeWriterWriteBase64TextArgs> xmlNodeWriterState)
        {
            if (internalWriteBase64TextAsyncWriter == null)
            {
                internalWriteBase64TextAsyncWriter = new InternalWriteBase64TextAsyncWriter(this);
            }

            return this.internalWriteBase64TextAsyncWriter.StartAsync(xmlNodeWriterState);
        }
            AsyncCompletionResult WriteAsync()
            {
                if (this.writerAsyncState == null)
                {
                    this.writerAsyncArgs = new XmlWriteBase64AsyncArguments();
                    this.writerAsyncState = new AsyncEventArgs<XmlWriteBase64AsyncArguments>();
                }

                if (onWriteComplete == null)
                {
                    onWriteComplete = new AsyncEventArgsCallback(OnWriteComplete);
                }

                this.writerAsyncArgs.Buffer = this.block;
                this.writerAsyncArgs.Offset = 0;
                this.writerAsyncArgs.Count = this.bytesRead;

                this.writerAsyncState.Set(onWriteComplete, this.writerAsyncArgs, this);
                if (this.writer.WriteBase64Async(this.writerAsyncState) == AsyncCompletionResult.Completed)
                {
                    this.HandleWriteComplete();
                    this.writerAsyncState.Complete(true);
                    return AsyncCompletionResult.Completed;
                }

                return AsyncCompletionResult.Queued;
            }
 internal virtual AsyncCompletionResult WriteBase64Async(AsyncEventArgs<XmlWriteBase64AsyncArguments> state)
 {
     throw FxTrace.Exception.AsError(new NotSupportedException());
 }
 void Clear()
 {
     this.inputState = null;
     this.buffer = null;
     this.offset = 0;
     this.count = 0;
     this.actualByteCount = 0;
     this.totalByteCount = 0;
 }
            bool HandleWriteBase64Text(bool isAsyncCallback)
            {
                Fx.Assert(this.count > 0 && this.actualByteCount >= 3, "HandleWriteBase64Text cannot be invoked with less than 3 bytes.");
                if (!writer.isXmlnsAttribute)
                {
                    if (!isAsyncCallback)
                    {
                        if (this.nodeWriterAsyncState == null)
                        {
                            this.nodeWriterAsyncState = new AsyncEventArgs<XmlNodeWriterWriteBase64TextArgs>();
                            this.nodeWriterArgs = new XmlNodeWriterWriteBase64TextArgs();
                        }
                        if (onWriteComplete == null)
                        {
                            onWriteComplete = new AsyncEventArgsCallback(OnWriteComplete);
                        }

                        this.writer.StartContent();
                        this.nodeWriterArgs.TrailBuffer = this.writer.trailBytes;
                        this.nodeWriterArgs.TrailCount = this.writer.trailByteCount;
                        this.nodeWriterArgs.Buffer = this.buffer;
                        this.nodeWriterArgs.Offset = this.offset;
                        this.nodeWriterArgs.Count = this.actualByteCount - this.writer.trailByteCount;

                        this.nodeWriterAsyncState.Set(onWriteComplete, this.nodeWriterArgs, this);
                        if (this.writer.writer.WriteBase64TextAsync(this.nodeWriterAsyncState) != AsyncCompletionResult.Completed)
                        {
                            return false;
                        }

                        this.nodeWriterAsyncState.Complete(true);
                    }

                    this.writer.EndContent();
                }

                this.writer.trailByteCount = (this.totalByteCount - this.actualByteCount);
                if (this.writer.trailByteCount > 0)
                {
                    int trailOffset = offset + count - this.writer.trailByteCount;
                    for (int i = 0; i < this.writer.trailByteCount; i++)
                        this.writer.trailBytes[i] = this.buffer[trailOffset++];
                }

                return true;
            }
 public void SetArguments(AsyncEventArgs<XmlWriteBase64AsyncArguments> inputState)
 {
     Fx.Assert(inputState != null, "InputState cannot be null.");
     this.inputState = inputState;
     this.buffer = inputState.Arguments.Buffer;
     this.offset = inputState.Arguments.Offset;
     this.count = inputState.Arguments.Count;
 }
        internal override AsyncCompletionResult WriteBase64Async(AsyncEventArgs<XmlWriteBase64AsyncArguments> state)
        {
            if (this.nodeWriterAsyncHelper == null)
            {
                this.nodeWriterAsyncHelper = new XmlBaseWriterNodeWriterAsyncHelper(this);
            }

            this.nodeWriterAsyncHelper.SetArguments(state);

            if (this.nodeWriterAsyncHelper.StartAsync() == AsyncCompletionResult.Completed)
            {                
                return AsyncCompletionResult.Completed;
            }

            return AsyncCompletionResult.Queued;
        }