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

The following examples show how to use com.facebook.common.memory.PooledByteBuffer#read() . 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: EncodedImage.java    From fresco with MIT License 6 votes vote down vote up
/**
 * Returns first n bytes of encoded image as hexbytes
 *
 * @param length the number of bytes to return
 */
public String getFirstBytesAsHexString(int length) {
  CloseableReference<PooledByteBuffer> imageBuffer = getByteBufferRef();
  if (imageBuffer == null) {
    return "";
  }
  int imageSize = getSize();
  int resultSampleSize = Math.min(imageSize, length);
  byte[] bytesBuffer = new byte[resultSampleSize];
  try {
    PooledByteBuffer pooledByteBuffer = imageBuffer.get();
    if (pooledByteBuffer == null) {
      return "";
    }
    pooledByteBuffer.read(0, bytesBuffer, 0, resultSampleSize);
  } finally {
    imageBuffer.close();
  }
  StringBuilder stringBuilder = new StringBuilder(bytesBuffer.length * 2);
  for (byte b : bytesBuffer) {
    stringBuilder.append(String.format("%02X", b));
  }
  return stringBuilder.toString();
}
 
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);
  }
}
 
Example 3
Source File: KitKatPurgeableDecoder.java    From fresco with MIT License 6 votes vote down vote up
/**
 * Decodes a byteArray containing jpeg encoded bytes into a purgeable bitmap
 *
 * <p>Adds a JFIF End-Of-Image marker if needed before decoding.
 *
 * @param bytesRef the byte buffer that contains the encoded bytes
 * @return
 */
@Override
protected Bitmap decodeJPEGByteArrayAsPurgeable(
    CloseableReference<PooledByteBuffer> bytesRef, int length, BitmapFactory.Options options) {
  byte[] suffix = endsWithEOI(bytesRef, length) ? null : EOI;
  final PooledByteBuffer pooledByteBuffer = bytesRef.get();
  Preconditions.checkArgument(length <= pooledByteBuffer.size());
  // allocate bigger array in case EOI needs to be added
  final CloseableReference<byte[]> encodedBytesArrayRef = mFlexByteArrayPool.get(length + 2);
  try {
    byte[] encodedBytesArray = encodedBytesArrayRef.get();
    pooledByteBuffer.read(0, encodedBytesArray, 0, length);
    if (suffix != null) {
      putEOI(encodedBytesArray, length);
      length += 2;
    }
    Bitmap bitmap = BitmapFactory.decodeByteArray(encodedBytesArray, 0, length, options);
    return Preconditions.checkNotNull(bitmap, "BitmapFactory returned null");
  } finally {
    CloseableReference.closeSafely(encodedBytesArrayRef);
  }
}
 
Example 4
Source File: EncodedImage.java    From fresco with MIT License 5 votes vote down vote up
/**
 * Returns true if the image is a JPEG or DNG and its data is already complete at the specified
 * length, false otherwise.
 */
public boolean isCompleteAt(int length) {
  if (mImageFormat != DefaultImageFormats.JPEG && mImageFormat != DefaultImageFormats.DNG) {
    return true;
  }
  // If the image is backed by FileInputStreams return true since they will always be complete.
  if (mInputStreamSupplier != null) {
    return true;
  }
  // The image should be backed by a ByteBuffer
  Preconditions.checkNotNull(mPooledByteBufferRef);
  PooledByteBuffer buf = mPooledByteBufferRef.get();
  return (buf.read(length - 2) == (byte) JfifUtil.MARKER_FIRST_BYTE)
      && (buf.read(length - 1) == (byte) JfifUtil.MARKER_EOI);
}
 
Example 5
Source File: HoneycombBitmapCreator.java    From fresco with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@Override
public Bitmap createNakedBitmap(int width, int height, Bitmap.Config bitmapConfig) {
  CloseableReference<PooledByteBuffer> jpgRef =
      mJpegGenerator.generate((short) width, (short) height);
  EncodedImage encodedImage = null;
  CloseableReference<byte[]> encodedBytesArrayRef = null;
  try {
    encodedImage = new EncodedImage(jpgRef);
    encodedImage.setImageFormat(DefaultImageFormats.JPEG);
    BitmapFactory.Options options =
        getBitmapFactoryOptions(encodedImage.getSampleSize(), bitmapConfig);
    int length = jpgRef.get().size();
    final PooledByteBuffer pooledByteBuffer = jpgRef.get();
    encodedBytesArrayRef = mFlexByteArrayPool.get(length + 2);
    byte[] encodedBytesArray = encodedBytesArrayRef.get();
    pooledByteBuffer.read(0, encodedBytesArray, 0, length);
    Bitmap bitmap = BitmapFactory.decodeByteArray(encodedBytesArray, 0, length, options);
    bitmap.setHasAlpha(true);
    bitmap.eraseColor(Color.TRANSPARENT);
    return bitmap;
  } finally {
    CloseableReference.closeSafely(encodedBytesArrayRef);
    EncodedImage.closeSafely(encodedImage);
    CloseableReference.closeSafely(jpgRef);
  }
}
 
Example 6
Source File: DalvikPurgeableDecoder.java    From fresco with MIT License 5 votes vote down vote up
@VisibleForTesting
public static boolean endsWithEOI(CloseableReference<PooledByteBuffer> bytesRef, int length) {
  PooledByteBuffer buffer = bytesRef.get();
  return length >= 2
      && buffer.read(length - 2) == (byte) JfifUtil.MARKER_FIRST_BYTE
      && buffer.read(length - 1) == (byte) JfifUtil.MARKER_EOI;
}