public override IAsyncResult BeginWrite(byte[] array, int offset, int count, AsyncCallback asyncCallback, object asyncState)
        {
            this.EnsureCompressionMode();
            if (this.asyncOperations != 0)
            {
                throw new InvalidOperationException(SR.GetString("Invalid begin call"));
            }
            this.ValidateParameters(array, offset, count);
            this.EnsureNotDisposed();
            Interlocked.Increment(ref this.asyncOperations);
            IAsyncResult result;

            try
            {
                DeflateStreamAsyncResult deflateStreamAsyncResult = new DeflateStreamAsyncResult(this, asyncState, asyncCallback, array, offset, count);
                deflateStreamAsyncResult.isWrite = true;
                this.m_AsyncWriterDelegate.BeginInvoke(array, offset, count, true, this.m_CallBack, deflateStreamAsyncResult);
                deflateStreamAsyncResult.m_CompletedSynchronously &= deflateStreamAsyncResult.IsCompleted;
                result = deflateStreamAsyncResult;
            }
            catch
            {
                Interlocked.Decrement(ref this.asyncOperations);
                throw;
            }
            return(result);
        }
Example #2
0
        }         // Dispose

#if !NETFX_CORE
        public override IAsyncResult BeginWrite(byte[] array, int offset, int count, AsyncCallback asyncCallback, object asyncState)
        {
            EnsureCompressionMode();

            // We use this checking order for compat to earlier versions:
            if (asyncOperations != 0)
            {
                throw new InvalidOperationException(SR.GetString(SR.InvalidBeginCall));
            }

            ValidateParameters(array, offset, count);
            EnsureNotDisposed();

            Interlocked.Increment(ref asyncOperations);

            try {
                DeflateStreamAsyncResult userResult = new DeflateStreamAsyncResult(
                    this, asyncState, asyncCallback, array, offset, count);
                userResult.isWrite = true;

                m_AsyncWriterDelegate.BeginInvoke(array, offset, count, true, m_CallBack, userResult);
                userResult.m_CompletedSynchronously &= userResult.IsCompleted;

                return(userResult);
            } catch {
                Interlocked.Decrement(ref asyncOperations);
                throw;
            }
        }
        private void ReadCallback(IAsyncResult baseStreamResult)
        {
            DeflateStreamAsyncResult deflateStreamAsyncResult = (DeflateStreamAsyncResult)baseStreamResult.AsyncState;

            deflateStreamAsyncResult.m_CompletedSynchronously &= baseStreamResult.CompletedSynchronously;
            try
            {
                this.EnsureNotDisposed();
                int num = this._stream.EndRead(baseStreamResult);
                if (num <= 0)
                {
                    deflateStreamAsyncResult.InvokeCallback(0);
                }
                else
                {
                    this.inflater.SetInput(this.buffer, 0, num);
                    num = this.inflater.Inflate(deflateStreamAsyncResult.buffer, deflateStreamAsyncResult.offset, deflateStreamAsyncResult.count);
                    if (num == 0 && !this.inflater.Finished())
                    {
                        this._stream.BeginRead(this.buffer, 0, this.buffer.Length, this.m_CallBack, deflateStreamAsyncResult);
                    }
                    else
                    {
                        deflateStreamAsyncResult.InvokeCallback(num);
                    }
                }
            }
            catch (Exception result)
            {
                deflateStreamAsyncResult.InvokeCallback(result);
            }
        }
Example #4
0
 private void AwaitAsyncResultCompletion(DeflateStreamAsyncResult asyncResult)
 {
     try {
         if (!asyncResult.IsCompleted)
         {
             asyncResult.AsyncWaitHandle.WaitOne();
         }
     } finally {
         Interlocked.Decrement(ref asyncOperations);
         asyncResult.Close();  // this will just close the wait handle
     }
 }
Example #5
0
		public override IAsyncResult BeginRead(byte[] array, int offset, int count, AsyncCallback asyncCallback,
			object asyncState)
		{
			EnsureDecompressionMode();

			// We use this checking order for compat to earlier versions:
			if (asyncOperations != 0)
				throw new InvalidOperationException(SR.GetString(SR.InvalidBeginCall));

			ValidateParameters(array, offset, count);
			EnsureNotDisposed();

			Interlocked.Increment(ref asyncOperations);

			try
			{
				var userResult = new DeflateStreamAsyncResult(
					this, asyncState, asyncCallback, array, offset, count);
				userResult.isWrite = false;

				// Try to read decompressed data in output buffer
				var bytesRead = inflater.Inflate(array, offset, count);
				if (bytesRead != 0)
				{
					// If decompression output buffer is not empty, return immediately.
					// 'true' means we complete synchronously.
					userResult.InvokeCallback(true, bytesRead);
					return userResult;
				}

				if (inflater.Finished())
				{
					// end of compression stream
					userResult.InvokeCallback(true, 0);
					return userResult;
				}

				// If there is no data on the output buffer and we are not at 
				// the end of the stream, we need to get more data from the base stream
				BaseStream.BeginRead(buffer, 0, buffer.Length, m_CallBack, userResult);
				userResult.m_CompletedSynchronously &= userResult.IsCompleted;

				return userResult;
			}
			catch
			{
				Interlocked.Decrement(ref asyncOperations);
				throw;
			}
		}
        public override void EndWrite(IAsyncResult asyncResult)
        {
            this.EnsureCompressionMode();
            this.CheckEndXxxxLegalStateAndParams(asyncResult);
            DeflateStreamAsyncResult deflateStreamAsyncResult = (DeflateStreamAsyncResult)asyncResult;

            this.AwaitAsyncResultCompletion(deflateStreamAsyncResult);
            Exception ex = deflateStreamAsyncResult.Result as Exception;

            if (ex != null)
            {
                throw ex;
            }
        }
Example #7
0
        // Callback function for asynchrous reading on base stream
        private void WriteCallback(IAsyncResult asyncResult)
        {
            DeflateStreamAsyncResult outerResult = (DeflateStreamAsyncResult)asyncResult.AsyncState;

            outerResult.m_CompletedSynchronously &= asyncResult.CompletedSynchronously;

            try {
                m_AsyncWriterDelegate.EndInvoke(asyncResult);
            } catch (Exception exc) {
                // Defer throwing this until EndWrite where there is user code on the stack:
                outerResult.InvokeCallback(exc);
                return;
            }
            outerResult.InvokeCallback(null);
        }
        public override int EndRead(IAsyncResult asyncResult)
        {
            this.EnsureDecompressionMode();
            this.CheckEndXxxxLegalStateAndParams(asyncResult);
            DeflateStreamAsyncResult deflateStreamAsyncResult = (DeflateStreamAsyncResult)asyncResult;

            this.AwaitAsyncResultCompletion(deflateStreamAsyncResult);
            Exception ex = deflateStreamAsyncResult.Result as Exception;

            if (ex != null)
            {
                throw ex;
            }
            return((int)deflateStreamAsyncResult.Result);
        }
        private void WriteCallback(IAsyncResult asyncResult)
        {
            DeflateStreamAsyncResult deflateStreamAsyncResult = (DeflateStreamAsyncResult)asyncResult.AsyncState;

            deflateStreamAsyncResult.m_CompletedSynchronously &= asyncResult.CompletedSynchronously;
            try
            {
                this.m_AsyncWriterDelegate.EndInvoke(asyncResult);
            }
            catch (Exception result)
            {
                deflateStreamAsyncResult.InvokeCallback(result);
                return;
            }
            deflateStreamAsyncResult.InvokeCallback(null);
        }
Example #10
0
        public override void EndWrite(IAsyncResult asyncResult)
        {
            EnsureCompressionMode();
            CheckEndXxxxLegalStateAndParams(asyncResult);

            // We checked that this will work in CheckEndXxxxLegalStateAndParams:
            DeflateStreamAsyncResult deflateStrmAsyncResult = (DeflateStreamAsyncResult)asyncResult;

            AwaitAsyncResultCompletion(deflateStrmAsyncResult);

            Exception previousException = deflateStrmAsyncResult.Result as Exception;

            if (previousException != null)
            {
                // Rethrowing will delete the stack trace. Let's help future debuggers:
                //previousException.Data.Add(OrigStackTrace_ExceptionDataKey, previousException.StackTrace);
                throw previousException;
            }
        }
        public override IAsyncResult BeginRead(byte[] array, int offset, int count, AsyncCallback asyncCallback, object asyncState)
        {
            this.EnsureDecompressionMode();
            if (this.asyncOperations != 0)
            {
                throw new InvalidOperationException(SR.GetString("Invalid begin call"));
            }
            this.ValidateParameters(array, offset, count);
            this.EnsureNotDisposed();
            Interlocked.Increment(ref this.asyncOperations);
            IAsyncResult result;

            try
            {
                DeflateStreamAsyncResult deflateStreamAsyncResult = new DeflateStreamAsyncResult(this, asyncState, asyncCallback, array, offset, count);
                deflateStreamAsyncResult.isWrite = false;
                int num = this.inflater.Inflate(array, offset, count);
                if (num != 0)
                {
                    deflateStreamAsyncResult.InvokeCallback(true, num);
                    result = deflateStreamAsyncResult;
                }
                else if (this.inflater.Finished())
                {
                    deflateStreamAsyncResult.InvokeCallback(true, 0);
                    result = deflateStreamAsyncResult;
                }
                else
                {
                    this._stream.BeginRead(this.buffer, 0, this.buffer.Length, this.m_CallBack, deflateStreamAsyncResult);
                    deflateStreamAsyncResult.m_CompletedSynchronously &= deflateStreamAsyncResult.IsCompleted;
                    result = deflateStreamAsyncResult;
                }
            }
            catch
            {
                Interlocked.Decrement(ref this.asyncOperations);
                throw;
            }
            return(result);
        }
Example #12
0
        // callback function for asynchrous reading on base stream
        private void ReadCallback(IAsyncResult baseStreamResult)
        {
            DeflateStreamAsyncResult outerResult = (DeflateStreamAsyncResult)baseStreamResult.AsyncState;

            outerResult.m_CompletedSynchronously &= baseStreamResult.CompletedSynchronously;
            int bytesRead = 0;

            try {
                EnsureNotDisposed();

                bytesRead = _stream.EndRead(baseStreamResult);

                if (bytesRead <= 0)
                {
                    // This indicates the base stream has received EOF
                    outerResult.InvokeCallback((object)0);
                    return;
                }

                // Feed the data from base stream into decompression engine
                inflater.SetInput(buffer, 0, bytesRead);
                bytesRead = inflater.Inflate(outerResult.buffer, outerResult.offset, outerResult.count);

                if (bytesRead == 0 && !inflater.Finished())
                {
                    // We could have read in head information and didn't get any data.
                    // Read from the base stream again.
                    // Need to solve recusion.
                    _stream.BeginRead(buffer, 0, buffer.Length, m_CallBack, outerResult);
                }
                else
                {
                    outerResult.InvokeCallback((object)bytesRead);
                }
            } catch (Exception exc) {
                // Defer throwing this until EndRead where we will likely have user code on the stack.
                outerResult.InvokeCallback(exc);
                return;
            }
        }
Example #13
0
        private void CheckEndXxxxLegalStateAndParams(IAsyncResult asyncResult)
        {
            if (asyncOperations != 1)
            {
                throw new InvalidOperationException(SR.GetString(SR.InvalidEndCall));
            }

            if (asyncResult == null)
            {
                throw new ArgumentNullException("asyncResult");
            }

            EnsureNotDisposed();

            DeflateStreamAsyncResult myResult = asyncResult as DeflateStreamAsyncResult;

            // This should really be an ArgumentException, but we keep this for compat to previous versions:
            if (myResult == null)
            {
                throw new ArgumentNullException("asyncResult");
            }
        }