Exemple #1
0
        // Private Methods (1) 

        private void ReadAsync_Callback(IAsyncResult result)
        {
            StreamAsyncEventArgs e = (StreamAsyncEventArgs)result.AsyncState;
            int bytesRead          = 0;

            bytesRead = _stream.EndRead(result);
            e.SetBuffer(e.Buffer, 0, bytesRead);

            e.Complete(e);
        }
Exemple #2
0
        public void ReadAsync(StreamAsyncEventArgs e)
        {
            byte[] buffer = e.Buffer;
            int    howManyBufferBytesToUse = 0;
            int    howManyStreamBytesToUse = e.Count;

            if (_bufferToPrepend != null &&
                _bufferToPrependPosition < _bufferToPrepend.Length)
            {
                // If we get here, we need to do some reading from _buffer, but how much?

                // This gives the remaining length of _buffer (the available frame)
                howManyBufferBytesToUse = (int)((long)_bufferToPrepend.Length - _bufferToPrependPosition);
                // Trim the frame if it is larger than we wanted
                if (e.Count <= howManyBufferBytesToUse)
                {
                    howManyBufferBytesToUse = e.Count;
                }
                // Whatever is left, give to stream
                howManyStreamBytesToUse = e.Count - howManyBufferBytesToUse;

                // Blockcopy it in
                _bufferToPrepend.CopyTo(buffer, e.Offset, howManyBufferBytesToUse);

                // Reset our buffer
                e.SetBuffer(buffer, e.Offset + howManyBufferBytesToUse, howManyStreamBytesToUse);

                if (howManyStreamBytesToUse <= 0)
                {
                    // This could be improved to always return this method before calling Complete
                    // but consumers should not be depending on that anyway.
                    e.Complete(e);
                    return;
                }
            }

            _stream.BeginRead(e.Buffer, e.Offset, e.Count, new AsyncCallback(ReadAsync_Callback), e);
        }
        public void ReadAsync(StreamAsyncEventArgs e)
        {
            byte[] buffer = e.Buffer;
            int howManyBufferBytesToUse = 0;
            int howManyStreamBytesToUse = e.Count;

            if (_bufferToPrepend != null &&
                _bufferToPrependPosition < _bufferToPrepend.Length)
            {
                // If we get here, we need to do some reading from _buffer, but how much?

                // This gives the remaining length of _buffer (the available frame)
                howManyBufferBytesToUse = (int)((long)_bufferToPrepend.Length - _bufferToPrependPosition);
                // Trim the frame if it is larger than we wanted
                if (e.Count <= howManyBufferBytesToUse) howManyBufferBytesToUse = e.Count;
                // Whatever is left, give to stream
                howManyStreamBytesToUse = e.Count - howManyBufferBytesToUse;

                // Blockcopy it in
                _bufferToPrepend.CopyTo(buffer, e.Offset, howManyBufferBytesToUse);

                // Reset our buffer
                e.SetBuffer(buffer, e.Offset + howManyBufferBytesToUse, howManyStreamBytesToUse);

                if (howManyStreamBytesToUse <= 0)
                {
                    // This could be improved to always return this method before calling Complete
                    // but consumers should not be depending on that anyway.
                    e.Complete(e);
                    return;
                }
            }

            _stream.BeginRead(e.Buffer, e.Offset, e.Count, new AsyncCallback(ReadAsync_Callback), e);
        }