Ejemplo n.º 1
0
            bool HandleWriteCharacters(IAsyncResult result)
            {
                int byteCount = Math.Min(bufferLength / 4 * 3, count - count % 3);
                int charCount = byteCount / 3 * 4;
                int charOffset;

                if (result == null)
                {
                    result = writer.BeginGetBuffer(charCount, onWriteCharacters, this);
                    if (!result.CompletedSynchronously)
                    {
                        return(false);
                    }
                }

                byte[] chars = writer.EndGetBuffer(result, out charOffset);

                writer.Advance(encoding.GetChars(this.buffer, this.offset, byteCount, chars, charOffset));
                this.offset += byteCount;
                this.count  -= byteCount;

                return(true);
            }
Ejemplo n.º 2
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);
            }