本文整理汇总了Java中com.facebook.common.references.CloseableReference类的典型用法代码示例。如果您正苦于以下问题:Java CloseableReference类的具体用法?Java CloseableReference怎么用?Java CloseableReference使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CloseableReference类属于com.facebook.common.references包,在下文中一共展示了CloseableReference类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getDataFetchSequence
import com.facebook.common.references.CloseableReference; //导入依赖的package包/类
/**
* bitmap cache get ->
* background thread hand-off -> bitmap cache -> decode -> resize and rotate -> (webp transcode)
* -> data fetch.
*/
private synchronized Producer<CloseableReference<CloseableImage>> getDataFetchSequence() {
if (mDataFetchSequence == null) {
Producer<EncodedImage> inputProducer = mProducerFactory.newDataFetchProducer();
if (WebpSupportStatus.sIsWebpSupportRequired &&
(!mWebpSupportEnabled || WebpSupportStatus.sWebpBitmapFactory == null)) {
inputProducer = mProducerFactory.newWebpTranscodeProducer(inputProducer);
}
inputProducer = mProducerFactory.newAddImageTransformMetaDataProducer(inputProducer);
inputProducer = mProducerFactory.newResizeAndRotateProducer(
inputProducer,
true,
mUseDownsamplingRatio);
mDataFetchSequence = newBitmapCacheGetToDecodeSequence(inputProducer);
}
return mDataFetchSequence;
}
示例2: getByteBufferBackedEncodedImage
import com.facebook.common.references.CloseableReference; //导入依赖的package包/类
/** Creates a memory-backed encoded image from the stream. The stream is closed. */
protected EncodedImage getByteBufferBackedEncodedImage(
InputStream inputStream,
int length) throws IOException {
CloseableReference<PooledByteBuffer> ref = null;
try {
if (length <= 0) {
ref = CloseableReference.of(mPooledByteBufferFactory.newByteBuffer(inputStream));
} else {
ref = CloseableReference.of(mPooledByteBufferFactory.newByteBuffer(inputStream, length));
}
return new EncodedImage(ref);
} finally {
Closeables.closeQuietly(inputStream);
CloseableReference.closeSafely(ref);
}
}
示例3: testFetchFromBitmapCacheDueToMethodCall
import com.facebook.common.references.CloseableReference; //导入依赖的package包/类
@Test
public void testFetchFromBitmapCacheDueToMethodCall() {
Producer<CloseableReference<CloseableImage>> bitmapCacheSequence = mock(Producer.class);
when(mProducerSequenceFactory.getDecodedImageProducerSequence(mImageRequest))
.thenReturn(bitmapCacheSequence);
mImagePipeline.fetchImageFromBitmapCache(mImageRequest, mCallerContext);
verify(mRequestListener1).onRequestStart(mImageRequest, mCallerContext, "0", false);
verify(mRequestListener2).onRequestStart(mImageRequest, mCallerContext, "0", false);
ArgumentCaptor<ProducerContext> producerContextArgumentCaptor =
ArgumentCaptor.forClass(ProducerContext.class);
verify(bitmapCacheSequence)
.produceResults(any(Consumer.class), producerContextArgumentCaptor.capture());
assertTrue(producerContextArgumentCaptor.getValue().isIntermediateResultExpected());
assertEquals(producerContextArgumentCaptor.getValue().getPriority(), Priority.HIGH);
assertEquals(
producerContextArgumentCaptor.getValue().getLowestPermittedRequestLevel(),
ImageRequest.RequestLevel.BITMAP_MEMORY_CACHE);
}
示例4: testInputProducerSuccessButResultNotCacheableDueToStatusFlags
import com.facebook.common.references.CloseableReference; //导入依赖的package包/类
private void testInputProducerSuccessButResultNotCacheableDueToStatusFlags(
final @Consumer.Status int statusFlags) {
setupInputProducerStreamingSuccessWithStatusFlags(statusFlags);
mEncodedMemoryCacheProducer.produceResults(mConsumer, mProducerContext);
verify(mMemoryCache, never()).cache(any(CacheKey.class), any(CloseableReference.class));
verify(mConsumer)
.onNewResult(mIntermediateEncodedImage, statusFlags);
verify(mConsumer)
.onNewResult(mFinalEncodedImage, Consumer.IS_LAST | statusFlags);
Assert.assertTrue(EncodedImage.isValid(mFinalEncodedImageClone));
verify(mProducerListener).onProducerStart(mRequestId, PRODUCER_NAME);
Map<String, String> extraMap =
ImmutableMap.of(EncodedMemoryCacheProducer.EXTRA_CACHED_VALUE_FOUND, "false");
verify(mProducerListener).onProducerFinishWithSuccess(mRequestId, PRODUCER_NAME, extraMap);
verify(mProducerListener, never())
.onUltimateProducerReached(anyString(), anyString(), anyBoolean());
}
示例5: decodeJpeg
import com.facebook.common.references.CloseableReference; //导入依赖的package包/类
/**
* Decodes a partial jpeg.
*
* @param encodedImage input image (encoded bytes plus meta data)
* @param length amount of currently available data in bytes
* @param qualityInfo quality info for the image
* @return a CloseableStaticBitmap
*/
public CloseableStaticBitmap decodeJpeg(
final EncodedImage encodedImage,
int length,
QualityInfo qualityInfo,
ImageDecodeOptions options) {
CloseableReference<Bitmap> bitmapReference =
mPlatformDecoder.decodeJPEGFromEncodedImage(
encodedImage, options.bitmapConfig, null, length);
try {
return new CloseableStaticBitmap(
bitmapReference,
qualityInfo,
encodedImage.getRotationAngle(),
encodedImage.getExifOrientation());
} finally {
bitmapReference.close();
}
}
示例6: testDoNotCacheStatefulImage
import com.facebook.common.references.CloseableReference; //导入依赖的package包/类
/**
* Verify that stateful image results, both intermediate and final, are never cached.
*/
@Test
public void testDoNotCacheStatefulImage() {
when(mCloseableImage1.isStateful()).thenReturn(true);
when(mCloseableImage2.isStateful()).thenReturn(true);
setupBitmapMemoryCacheGetNotFound();
setupInputProducerStreamingSuccess();
when(mMemoryCache.get(mBitmapMemoryCacheKey)).thenReturn(null);
mBitmapMemoryCacheProducer.produceResults(mConsumer, mProducerContext);
verify(mConsumer).onNewResult(mIntermediateImageReference, Consumer.NO_FLAGS);
verify(mConsumer).onNewResult(mFinalImageReference, Consumer.IS_LAST);
verify(mMemoryCache, never()).cache(
any(BitmapMemoryCacheKey.class),
any(CloseableReference.class));
}
示例7: process
import com.facebook.common.references.CloseableReference; //导入依赖的package包/类
@Override
public CloseableReference<Bitmap> process(Bitmap sourceBitmap, PlatformBitmapFactory bitmapFactory) {
CloseableReference<Bitmap> bitmapRef;
if (isFull) {
bitmapRef = bitmapFactory.createBitmap(
sourceBitmap.getWidth() / 2,
sourceBitmap.getHeight());
} else {
bitmapRef = bitmapFactory.createBitmap(
sourceBitmap.getWidth(),
sourceBitmap.getHeight());
}
try {
Bitmap destBitmap = bitmapRef.get();
Canvas canvas2d = new Canvas(destBitmap);
canvas2d.drawBitmap(sourceBitmap,
new Rect(0, 0, sourceBitmap.getWidth() / 2, sourceBitmap.getHeight()),
new Rect(0, 0, destBitmap.getWidth(), destBitmap.getHeight()), null);
return CloseableReference.cloneOrNull(bitmapRef);
} finally {
CloseableReference.closeSafely(bitmapRef);
}
}
示例8: onFrameRendered
import com.facebook.common.references.CloseableReference; //导入依赖的package包/类
@Override
public synchronized void onFrameRendered(
int frameNumber,
CloseableReference<Bitmap> bitmapReference,
@BitmapAnimationBackend.FrameType int frameType) {
Preconditions.checkNotNull(bitmapReference);
// Close up prepared references.
removePreparedReference(frameNumber);
// Create the new image reference and cache it.
CloseableReference<CloseableImage> closableReference = null;
try {
closableReference = createImageReference(bitmapReference);
if (closableReference != null) {
CloseableReference.closeSafely(mLastRenderedItem);
mLastRenderedItem = mAnimatedFrameCache.cache(frameNumber, closableReference);
}
} finally {
CloseableReference.closeSafely(closableReference);
}
}
示例9: verifyState
import com.facebook.common.references.CloseableReference; //导入依赖的package包/类
private void verifyState(
boolean isFinished,
boolean hasResult,
CloseableReference<Object> resultRef,
boolean hasFailed,
Throwable failureCause) {
DataSource<CloseableReference<Object>> dataSource = mDataSource;
assertEquals("isFinished", isFinished, dataSource.isFinished());
assertEquals("hasResult", hasResult, dataSource.hasResult());
CloseableReference<Object> dataSourceRef = dataSource.getResult();
assertReferencesSame("getResult", resultRef, dataSourceRef);
CloseableReference.closeSafely(dataSourceRef);
assertEquals("hasFailed", hasFailed, dataSource.hasFailed());
if (failureCause == NPE) {
assertNotNull("failure", dataSource.getFailureCause());
assertSame("failure", NullPointerException.class, dataSource.getFailureCause().getClass());
} else {
assertSame("failure", failureCause, dataSource.getFailureCause());
}
}
示例10: cloneOrNull
import com.facebook.common.references.CloseableReference; //导入依赖的package包/类
public EncodedImage cloneOrNull() {
EncodedImage encodedImage;
if (mInputStreamSupplier != null) {
encodedImage = new EncodedImage(mInputStreamSupplier, mStreamSize);
} else {
CloseableReference<PooledByteBuffer> pooledByteBufferRef =
CloseableReference.cloneOrNull(mPooledByteBufferRef);
try {
encodedImage = (pooledByteBufferRef == null) ? null : new EncodedImage(pooledByteBufferRef);
} finally {
// Close the recently created reference since it will be cloned again in the constructor.
CloseableReference.closeSafely(pooledByteBufferRef);
}
}
if (encodedImage != null) {
encodedImage.copyMetaDataFrom(this);
}
return encodedImage;
}
示例11: decodeJPEGFromEncodedImage
import com.facebook.common.references.CloseableReference; //导入依赖的package包/类
/**
* Creates a bitmap from encoded JPEG bytes. Supports a partial JPEG image.
*
* @param encodedImage the encoded image with reference to the encoded bytes
* @param bitmapConfig the {@link android.graphics.Bitmap.Config} used to create the decoded
* Bitmap
* @param regionToDecode optional image region to decode. currently not supported.
* @param length the number of encoded bytes in the buffer
* @return the bitmap
* @throws TooManyBitmapsException if the pool is full
* @throws java.lang.OutOfMemoryError if the Bitmap cannot be allocated
*/
@Override
public CloseableReference<Bitmap> decodeJPEGFromEncodedImage(
final EncodedImage encodedImage,
Bitmap.Config bitmapConfig,
@Nullable Rect regionToDecode,
int length) {
BitmapFactory.Options options = getBitmapFactoryOptions(
encodedImage.getSampleSize(),
bitmapConfig);
final CloseableReference<PooledByteBuffer> bytesRef = encodedImage.getByteBufferRef();
Preconditions.checkNotNull(bytesRef);
try {
Bitmap bitmap = decodeJPEGByteArrayAsPurgeable(bytesRef, length, options);
return pinBitmap(bitmap);
} finally {
CloseableReference.closeSafely(bytesRef);
}
}
示例12: testReuseExclusive_CacheSameItem
import com.facebook.common.references.CloseableReference; //导入依赖的package包/类
@Test
public void testReuseExclusive_CacheSameItem() {
CloseableReference<Integer> cachedRef =
mCache.cache(KEY, newReference(100), mEntryStateObserver);
cachedRef.close();
verify(mEntryStateObserver).onExclusivityChanged(KEY, true);
assertTotalSize(1, 100);
assertExclusivelyOwnedSize(1, 100);
cachedRef = mCache.reuse(KEY);
assertNotNull(cachedRef);
verify(mEntryStateObserver).onExclusivityChanged(KEY, false);
assertTotalSize(0, 0);
assertExclusivelyOwnedSize(0, 0);
CloseableReference<Integer> newItem = mCache.cache(KEY, cachedRef);
cachedRef.close();
assertTotalSize(1, 100);
assertExclusivelyOwnedSize(0, 0);
newItem.close();
verify(mEntryStateObserver).onExclusivityChanged(KEY, true);
assertTotalSize(1, 100);
assertExclusivelyOwnedSize(1, 100);
}
示例13: decodeByteArrayAsPurgeable
import com.facebook.common.references.CloseableReference; //导入依赖的package包/类
/**
* Decodes a byteArray into a purgeable bitmap
*
* @param bytesRef the byte buffer that contains the encoded bytes
* @return
*/
@Override
protected Bitmap decodeByteArrayAsPurgeable(
CloseableReference<PooledByteBuffer> bytesRef,
BitmapFactory.Options options) {
final PooledByteBuffer pooledByteBuffer = bytesRef.get();
final int length = pooledByteBuffer.size();
final CloseableReference<byte[]> encodedBytesArrayRef = mFlexByteArrayPool.get(length);
try {
final byte[] encodedBytesArray = encodedBytesArrayRef.get();
pooledByteBuffer.read(0, encodedBytesArray, 0, length);
Bitmap bitmap = BitmapFactory.decodeByteArray(
encodedBytesArray,
0,
length,
options);
return Preconditions.checkNotNull(bitmap, "BitmapFactory returned null");
} finally {
CloseableReference.closeSafely(encodedBytesArrayRef);
}
}
示例14: testNewResult
import com.facebook.common.references.CloseableReference; //导入依赖的package包/类
private void testNewResult(
CloseableReference<Object> resultRef,
boolean isLast,
int numSubscribers) {
mInternalConsumer.onNewResult(resultRef, BaseConsumer.simpleStatusForIsLast(isLast));
if (isLast) {
verify(mRequestListener).onRequestSuccess(
mSettableProducerContext.getImageRequest(),
mRequestId,
mSettableProducerContext.isPrefetch());
}
if (numSubscribers >= 1) {
verify(mDataSubscriber1).onNewResult(mDataSource);
}
if (numSubscribers >= 2) {
verify(mDataSubscriber2).onNewResult(mDataSource);
}
verifyWithResult(resultRef, isLast);
}
示例15: fetchDecodedImage
import com.facebook.common.references.CloseableReference; //导入依赖的package包/类
/**
* Submits a request for execution and returns a DataSource representing the pending decoded
* image(s).
* <p>The returned DataSource must be closed once the client has finished with it.
*
* @param imageRequest the request to submit
* @param callerContext the caller context for image request
* @param lowestPermittedRequestLevelOnSubmit the lowest request level permitted for image request
* @return a DataSource representing the pending decoded image(s)
*/
public DataSource<CloseableReference<CloseableImage>> fetchDecodedImage(
ImageRequest imageRequest,
Object callerContext,
ImageRequest.RequestLevel lowestPermittedRequestLevelOnSubmit) {
try {
Producer<CloseableReference<CloseableImage>> producerSequence =
mProducerSequenceFactory.getDecodedImageProducerSequence(imageRequest);
return submitFetchRequest(
producerSequence,
imageRequest,
lowestPermittedRequestLevelOnSubmit,
callerContext);
} catch (Exception exception) {
return DataSources.immediateFailedDataSource(exception);
}
}