예제 #1
0
            static void OnGetBufferComplete(IAsyncEventArgs state)
            {
                GetBufferEventResult result = ((GetBufferAsyncEventArgs)state).Result;
                InternalWriteBase64TextAsyncWriter thisPtr    = (InternalWriteBase64TextAsyncWriter)state.AsyncState;
                XmlWriteBase64AsyncArguments       writerArgs = thisPtr.writerState.Arguments;

                Exception completionException = null;
                bool      completeSelf        = false;

                try
                {
                    if (state.Exception != null)
                    {
                        completionException = state.Exception;
                        completeSelf        = true;
                    }
                    else
                    {
                        byte[] chars  = result.Buffer;
                        int    offset = result.Offset;

                        Base64Encoding encoding  = XmlConverter.Base64Encoding;
                        int            byteCount = Math.Min(bufferLength / 4 * 3, writerArgs.Count - writerArgs.Count % 3);
                        int            charCount = byteCount / 3 * 4;

                        thisPtr.writer.Advance(encoding.GetChars(
                                                   writerArgs.Buffer,
                                                   writerArgs.Offset,
                                                   byteCount,
                                                   chars,
                                                   offset));

                        if (byteCount >= 3)
                        {
                            writerArgs.Offset += byteCount;
                            writerArgs.Count  -= byteCount;
                        }

                        if (thisPtr.InternalWriteBase64TextAsync(thisPtr.writerState) == AsyncCompletionResult.Completed)
                        {
                            completeSelf = true;
                        }
                    }
                }
                catch (Exception exception)
                {
                    if (Fx.IsFatal(exception))
                    {
                        throw;
                    }

                    completionException = exception;
                    completeSelf        = true;
                }

                if (completeSelf)
                {
                    thisPtr.writerState.Complete(false, completionException);
                }
            }
 public InternalWriteBase64TextAsyncWriter(XmlUTF8NodeWriter writer)
 {
     this.writer = writer;
     this.writerState = new AsyncEventArgs<XmlWriteBase64AsyncArguments>();
     this.writerArgs = new XmlWriteBase64AsyncArguments();
 }
            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;
            }
예제 #4
0
            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);
            }
예제 #5
0
 public InternalWriteBase64TextAsyncWriter(XmlUTF8NodeWriter writer)
 {
     this.writer      = writer;
     this.writerState = new AsyncEventArgs <XmlWriteBase64AsyncArguments>();
     this.writerArgs  = new XmlWriteBase64AsyncArguments();
 }