Java Code Examples for com.facebook.common.memory.PooledByteBuffer#size()

The following examples show how to use com.facebook.common.memory.PooledByteBuffer#size() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EncodedCountingMemoryCacheFactory.java    From fresco with MIT License 6 votes vote down vote up
public static CountingMemoryCache<CacheKey, PooledByteBuffer> get(
    Supplier<MemoryCacheParams> encodedMemoryCacheParamsSupplier,
    MemoryTrimmableRegistry memoryTrimmableRegistry) {

  ValueDescriptor<PooledByteBuffer> valueDescriptor =
      new ValueDescriptor<PooledByteBuffer>() {
        @Override
        public int getSizeInBytes(PooledByteBuffer value) {
          return value.size();
        }
      };

  CountingMemoryCache.CacheTrimStrategy trimStrategy = new NativeMemoryCacheTrimStrategy();

  CountingMemoryCache<CacheKey, PooledByteBuffer> countingCache =
      new CountingMemoryCache<>(
          valueDescriptor, trimStrategy, encodedMemoryCacheParamsSupplier, null);

  memoryTrimmableRegistry.registerMemoryTrimmable(countingCache);

  return countingCache;
}
 
Example 2
Source File: KitKatPurgeableDecoder.java    From fresco with MIT License 6 votes vote down vote up
/**
 * 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);
  }
}