private void HandleFinalResult(
            PooledByteBufferOutputStream pooledOutputStream,
            FetchState fetchState)
        {
            IDictionary <string, string> extraMap = GetExtraMap(fetchState, pooledOutputStream.Size);

            fetchState.Listener.OnProducerFinishWithSuccess(fetchState.Id, PRODUCER_NAME, extraMap);
            NotifyConsumer(pooledOutputStream, true, fetchState.Consumer);
        }
        private void MaybeHandleIntermediateResult(
            PooledByteBufferOutputStream pooledOutputStream,
            FetchState fetchState)
        {
            long nowMs = SystemClock.UptimeMillis;

            if (ShouldPropagateIntermediateResults(fetchState) &&
                nowMs - fetchState.LastIntermediateResultTimeMs >= TIME_BETWEEN_PARTIAL_RESULTS_MS)
            {
                fetchState.LastIntermediateResultTimeMs = nowMs;
                fetchState.Listener.OnProducerEvent(
                    fetchState.Id, PRODUCER_NAME, INTERMEDIATE_RESULT_PRODUCER_EVENT);
                NotifyConsumer(pooledOutputStream, false, fetchState.Consumer);
            }
        }
        private void NotifyConsumer(
            PooledByteBufferOutputStream pooledOutputStream,
            bool isFinal,
            IConsumer <EncodedImage> consumer)
        {
            CloseableReference <IPooledByteBuffer> result =
                CloseableReference <IPooledByteBuffer> .of(pooledOutputStream.ToByteBuffer());

            EncodedImage encodedImage = null;

            try
            {
                encodedImage = new EncodedImage(result);
                encodedImage.ParseMetaDataAsync().Wait();
                consumer.OnNewResult(encodedImage, isFinal);
            }
            finally
            {
                EncodedImage.CloseSafely(encodedImage);
                CloseableReference <IPooledByteBuffer> .CloseSafely(result);
            }
        }
Exemple #4
0
            private async Task DoTransform(EncodedImage encodedImage, bool isLast)
            {
                _producerContext.Listener.OnProducerStart(_producerContext.Id, PRODUCER_NAME);
                ImageRequest imageRequest = _producerContext.ImageRequest;
                PooledByteBufferOutputStream outputStream =
                    _parent._pooledByteBufferFactory.NewOutputStream();

                IDictionary <string, string> extraMap = default(IDictionary <string, string>);
                EncodedImage ret         = default(EncodedImage);
                Stream       inputStream = default(Stream);

                try
                {
                    int numerator = GetScaleNumerator(imageRequest, encodedImage);
                    extraMap    = GetExtraMap(encodedImage, imageRequest, numerator);
                    inputStream = encodedImage.GetInputStream();
#if HAS_LIBJPEGTURBO
                    JpegTranscoder.TranscodeJpeg(
                        inputStream.AsIStream(),
                        outputStream.AsIStream(),
                        GetRotationAngle(imageRequest, encodedImage),
                        numerator,
                        DEFAULT_JPEG_QUALITY);
#else // HAS_LIBJPEGTURBO
                    inputStream.CopyTo(outputStream);
#endif // HAS_LIBJPEGTURBO

                    CloseableReference <IPooledByteBuffer> reference =
                        CloseableReference <IPooledByteBuffer> .of(outputStream.ToByteBuffer());

                    try
                    {
                        ret        = new EncodedImage(reference);
                        ret.Format = ImageFormat.JPEG;

                        try
                        {
                            await ret.ParseMetaDataAsync().ConfigureAwait(false);

                            _producerContext.Listener.OnProducerFinishWithSuccess(
                                _producerContext.Id, PRODUCER_NAME, extraMap);

                            Consumer.OnNewResult(ret, isLast);
                        }
                        finally
                        {
                            EncodedImage.CloseSafely(ret);
                        }
                    }
                    finally
                    {
                        CloseableReference <IPooledByteBuffer> .CloseSafely(reference);
                    }
                }
                catch (Exception e)
                {
                    _producerContext.Listener.OnProducerFinishWithFailure(
                        _producerContext.Id, PRODUCER_NAME, e, extraMap);

                    Consumer.OnFailure(e);
                    return;
                }
                finally
                {
                    Closeables.CloseQuietly(inputStream);
                    outputStream.Dispose();
                }
            }