Esempio n. 1
0
        /// <summary>
        /// Copy all bytes from the input stream to the output stream.
        /// </summary>
        /// <param name="from">The intput stream.</param>
        /// <param name="to">The output stream.</param>
        /// <returns>Number of copied bytes.</returns>
        /// <exception cref="IOException">
        /// An I/O error occurs.
        /// </exception>
        public long Copy(Stream from, Stream to)
        {
            long count = 0;

            byte[] tmp = _byteArrayPool.Get(_tempBufSize);

            try
            {
                while (true)
                {
                    int read = from.Read(tmp, 0, _tempBufSize);
                    if (read == 0)
                    {
                        return(count);
                    }

                    to.Write(tmp, 0, read);
                    count += read;
                }
            }
            finally
            {
                _byteArrayPool.Release(tmp);
            }
        }
        private void OnResponse(
            FetchState fetchState,
            Stream responseData,
            int responseContentLength)
        {
            PooledByteBufferOutputStream pooledOutputStream;

            if (responseContentLength > 0)
            {
                pooledOutputStream = _pooledByteBufferFactory.NewOutputStream(responseContentLength);
            }
            else
            {
                pooledOutputStream = _pooledByteBufferFactory.NewOutputStream();
            }

            byte[] ioArray = _byteArrayPool.Get(READ_SIZE);

            try
            {
                int length;
                while ((length = responseData.Read(ioArray, 0, ioArray.Length)) > 0)
                {
                    pooledOutputStream.Write(ioArray, 0, length);
                    MaybeHandleIntermediateResult(pooledOutputStream, fetchState);
                    float progress = CalculateProgress(pooledOutputStream.Size, responseContentLength);
                    fetchState.Consumer.OnProgressUpdate(progress);
                }

                _networkFetcher.OnFetchCompletion(fetchState, pooledOutputStream.Size);
                HandleFinalResult(pooledOutputStream, fetchState);
            }
            finally
            {
                _byteArrayPool.Release(ioArray);
                pooledOutputStream.Dispose();
            }
        }