use of com.facebook.common.streams.LimitedInputStream in project fresco by facebook.
the class ArtDecoder method decodeJPEGFromEncodedImage.
/**
* 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 length the number of encoded bytes in the buffer
* @return the bitmap
* @exception java.lang.OutOfMemoryError if the Bitmap cannot be allocated
*/
@Override
public CloseableReference<Bitmap> decodeJPEGFromEncodedImage(EncodedImage encodedImage, Bitmap.Config bitmapConfig, int length) {
boolean isJpegComplete = encodedImage.isCompleteAt(length);
final BitmapFactory.Options options = getDecodeOptionsForStream(encodedImage, bitmapConfig);
InputStream jpegDataStream = encodedImage.getInputStream();
// At this point the InputStream from the encoded image should not be null since in the
// pipeline,this comes from a call stack where this was checked before. Also this method needs
// the InputStream to decode the image so this can't be null.
Preconditions.checkNotNull(jpegDataStream);
if (encodedImage.getSize() > length) {
jpegDataStream = new LimitedInputStream(jpegDataStream, length);
}
if (!isJpegComplete) {
jpegDataStream = new TailAppendingInputStream(jpegDataStream, EOI_TAIL);
}
boolean retryOnFail = options.inPreferredConfig != Bitmap.Config.ARGB_8888;
try {
return decodeStaticImageFromStream(jpegDataStream, options);
} catch (RuntimeException re) {
if (retryOnFail) {
return decodeFromEncodedImage(encodedImage, Bitmap.Config.ARGB_8888);
}
throw re;
}
}
use of com.facebook.common.streams.LimitedInputStream in project fresco by facebook.
the class DefaultDecoder method decodeJPEGFromEncodedImageWithColorSpace.
/**
* 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 or null to decode the whole image
* @param length the number of encoded bytes in the buffer
* @param colorSpace the target color space of the decoded bitmap, must be one of the named color
* space in {@link android.graphics.ColorSpace.Named}. If null, then SRGB color space is
* assumed if the SDK version >= 26.
* @return the bitmap
* @exception java.lang.OutOfMemoryError if the Bitmap cannot be allocated
*/
@Override
public CloseableReference<Bitmap> decodeJPEGFromEncodedImageWithColorSpace(EncodedImage encodedImage, Bitmap.Config bitmapConfig, @Nullable Rect regionToDecode, int length, @Nullable final ColorSpace colorSpace) {
boolean isJpegComplete = encodedImage.isCompleteAt(length);
final BitmapFactory.Options options = getDecodeOptionsForStream(encodedImage, bitmapConfig);
InputStream jpegDataStream = encodedImage.getInputStream();
// At this point the InputStream from the encoded image should not be null since in the
// pipeline,this comes from a call stack where this was checked before. Also this method needs
// the InputStream to decode the image so this can't be null.
Preconditions.checkNotNull(jpegDataStream);
if (encodedImage.getSize() > length) {
jpegDataStream = new LimitedInputStream(jpegDataStream, length);
}
if (!isJpegComplete) {
jpegDataStream = new TailAppendingInputStream(jpegDataStream, EOI_TAIL);
}
boolean retryOnFail = options.inPreferredConfig != Bitmap.Config.ARGB_8888;
try {
return decodeFromStream(jpegDataStream, options, regionToDecode, colorSpace);
} catch (RuntimeException re) {
if (retryOnFail) {
return decodeJPEGFromEncodedImageWithColorSpace(encodedImage, Bitmap.Config.ARGB_8888, regionToDecode, length, colorSpace);
}
throw re;
} finally {
try {
jpegDataStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
use of com.facebook.common.streams.LimitedInputStream in project fresco by facebook.
the class GingerbreadPurgeableDecoder method copyToMemoryFile.
private static MemoryFile copyToMemoryFile(CloseableReference<PooledByteBuffer> bytesRef, int inputLength, @Nullable byte[] suffix) throws IOException {
int outputLength = inputLength + (suffix == null ? 0 : suffix.length);
MemoryFile memoryFile = new MemoryFile(null, outputLength);
memoryFile.allowPurging(false);
PooledByteBufferInputStream pbbIs = null;
LimitedInputStream is = null;
OutputStream os = null;
try {
pbbIs = new PooledByteBufferInputStream(bytesRef.get());
is = new LimitedInputStream(pbbIs, inputLength);
os = memoryFile.getOutputStream();
ByteStreams.copy(is, os);
if (suffix != null) {
memoryFile.writeBytes(suffix, 0, inputLength, suffix.length);
}
return memoryFile;
} finally {
CloseableReference.closeSafely(bytesRef);
Closeables.closeQuietly(pbbIs);
Closeables.closeQuietly(is);
Closeables.close(os, true);
}
}
Aggregations