Search in sources :

Example 1 with PooledByteBufferOutputStream

use of com.facebook.common.memory.PooledByteBufferOutputStream in project fresco by facebook.

the class NetworkFetchProducer method onResponse.

protected void onResponse(FetchState fetchState, InputStream responseData, int responseContentLength) throws IOException {
    final PooledByteBufferOutputStream pooledOutputStream;
    if (responseContentLength > 0) {
        pooledOutputStream = mPooledByteBufferFactory.newOutputStream(responseContentLength);
    } else {
        pooledOutputStream = mPooledByteBufferFactory.newOutputStream();
    }
    final byte[] ioArray = mByteArrayPool.get(READ_SIZE);
    try {
        int length;
        while ((length = responseData.read(ioArray)) >= 0) {
            if (length > 0) {
                pooledOutputStream.write(ioArray, 0, length);
                maybeHandleIntermediateResult(pooledOutputStream, fetchState);
                float progress = calculateProgress(pooledOutputStream.size(), responseContentLength);
                fetchState.getConsumer().onProgressUpdate(progress);
            }
        }
        mNetworkFetcher.onFetchCompletion(fetchState, pooledOutputStream.size());
        handleFinalResult(pooledOutputStream, fetchState);
    } finally {
        mByteArrayPool.release(ioArray);
        pooledOutputStream.close();
    }
}
Also used : PooledByteBufferOutputStream(com.facebook.common.memory.PooledByteBufferOutputStream)

Example 2 with PooledByteBufferOutputStream

use of com.facebook.common.memory.PooledByteBufferOutputStream in project fresco by facebook.

the class WebpTranscodeProducer method transcodeLastResult.

private void transcodeLastResult(final EncodedImage originalResult, final Consumer<EncodedImage> consumer, final ProducerContext producerContext) {
    Preconditions.checkNotNull(originalResult);
    final EncodedImage encodedImageCopy = EncodedImage.cloneOrNull(originalResult);
    final StatefulProducerRunnable<EncodedImage> runnable = new StatefulProducerRunnable<EncodedImage>(consumer, producerContext.getProducerListener(), producerContext, PRODUCER_NAME) {

        @Override
        protected EncodedImage getResult() throws Exception {
            PooledByteBufferOutputStream outputStream = mPooledByteBufferFactory.newOutputStream();
            try {
                // NULLSAFE_FIXME[Parameter Not Nullable]
                doTranscode(encodedImageCopy, outputStream);
                CloseableReference<PooledByteBuffer> ref = CloseableReference.of(outputStream.toByteBuffer());
                try {
                    EncodedImage encodedImage = new EncodedImage(ref);
                    // NULLSAFE_FIXME[Parameter Not Nullable]
                    encodedImage.copyMetaDataFrom(encodedImageCopy);
                    return encodedImage;
                } finally {
                    CloseableReference.closeSafely(ref);
                }
            } finally {
                outputStream.close();
            }
        }

        @Override
        protected void disposeResult(EncodedImage result) {
            EncodedImage.closeSafely(result);
        }

        @Override
        protected void onSuccess(EncodedImage result) {
            EncodedImage.closeSafely(encodedImageCopy);
            super.onSuccess(result);
        }

        @Override
        protected void onFailure(Exception e) {
            EncodedImage.closeSafely(encodedImageCopy);
            super.onFailure(e);
        }

        @Override
        protected void onCancellation() {
            EncodedImage.closeSafely(encodedImageCopy);
            super.onCancellation();
        }
    };
    mExecutor.execute(runnable);
}
Also used : PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) PooledByteBufferOutputStream(com.facebook.common.memory.PooledByteBufferOutputStream) EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 3 with PooledByteBufferOutputStream

use of com.facebook.common.memory.PooledByteBufferOutputStream in project fresco by facebook.

the class EmptyJpegGenerator method generate.

public CloseableReference<PooledByteBuffer> generate(short width, short height) {
    PooledByteBufferOutputStream os = null;
    try {
        os = mPooledByteBufferFactory.newOutputStream(EMPTY_JPEG_PREFIX.length + EMPTY_JPEG_SUFFIX.length + 4);
        os.write(EMPTY_JPEG_PREFIX);
        os.write((byte) (height >> 8));
        os.write((byte) (height & 0x00ff));
        os.write((byte) (width >> 8));
        os.write((byte) (width & 0x00ff));
        os.write(EMPTY_JPEG_SUFFIX);
        return CloseableReference.of(os.toByteBuffer());
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (os != null) {
            os.close();
        }
    }
}
Also used : PooledByteBufferOutputStream(com.facebook.common.memory.PooledByteBufferOutputStream) IOException(java.io.IOException)

Aggregations

PooledByteBufferOutputStream (com.facebook.common.memory.PooledByteBufferOutputStream)3 PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)1 EncodedImage (com.facebook.imagepipeline.image.EncodedImage)1 IOException (java.io.IOException)1