Java源码示例:com.facebook.common.internal.Closeables
示例1
/** 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);
}
}
示例2
@Override
public CloseableImage decode(
EncodedImage encodedImage,
int length,
QualityInfo qualityInfo,
ImageDecodeOptions options) {
InputStream encodedInputStream = null;
try {
encodedInputStream = encodedImage.getInputStream();
return new CloseableKeyframesImage(KFImageDeserializer.deserialize(encodedInputStream));
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
Closeables.closeQuietly(encodedInputStream);
}
}
示例3
/**
* If this is the first time calling this method, the buffer will be checked to make sure it
* starts with SOI marker (0xffd8). If the image has been identified as a non-JPEG, data will be
* ignored and false will be returned immediately on all subsequent calls.
*
* This object maintains state of the position of the last read byte. On repeated calls to this
* method, it will continue from where it left off.
*
* @param dataBufferRef Next set of bytes received by the caller
* @return true if a new full scan has been found
*/
public boolean parseMoreData(final CloseableReference<PooledByteBuffer> dataBufferRef) {
if (mParserState == NOT_A_JPEG) {
return false;
}
final PooledByteBuffer dataBuffer = dataBufferRef.get();
final int dataBufferSize = dataBuffer.size();
// Is there any new data to parse?
// mBytesParsed might be greater than size of dataBuffer - that happens when
// we skip more data than is available to read inside doParseMoreData method
if (dataBufferSize <= mBytesParsed) {
return false;
}
final InputStream bufferedDataStream = new PooledByteArrayBufferedInputStream(
dataBuffer.getStream(),
mByteArrayPool.get(BUFFER_SIZE),
mByteArrayPool);
try {
StreamUtil.skip(bufferedDataStream, mBytesParsed);
return doParseMoreData(bufferedDataStream);
} catch (IOException ioe) {
// Does not happen - streams returned by PooledByteBuffers do not throw IOExceptions
Throwables.propagate(ioe);
return false;
} finally {
Closeables.closeQuietly(bufferedDataStream);
}
}
示例4
@Override
public void release(Closeable value) {
try {
Closeables.close(value, true);
} catch (IOException ioe) {
// This will not happen, Closeable.close swallows and logs IOExceptions
}
}
示例5
@Override
public void release(Closeable value) {
try {
Closeables.close(value, true);
} catch (IOException ioe) {
// This will not happen, Closeable.close swallows and logs IOExceptions
}
}
示例6
@Override
public void release(Thing value) {
try {
Closeables.close(value, true);
} catch (IOException ioe) {
// this should not happen
Assert.fail();
}
}
示例7
/** Verify that using a ByteArrayInputStream does not allocate a new byte array. */
@Test
public void testByteArrayInputStream() throws Exception {
byte[] bytes = new byte[8];
InputStream input = new ByteArrayInputStream(bytes);
try {
byte[] bytesRead = StreamUtil.getBytesFromStream(input);
assertTrue(Arrays.equals(bytes, bytesRead));
} finally {
Closeables.close(input, true);
}
}
示例8
/** Verify that using an offset with ByteArrayInputStream still produces correct output. */
@Test
public void testByteArrayInputStreamWithOffset() throws Exception {
byte[] bytes = new byte[] {0, 1, 2, 3, 4};
InputStream input = new ByteArrayInputStream(bytes, 1, 4);
try {
byte[] bytesRead = StreamUtil.getBytesFromStream(input);
byte[] expectedBytes = new byte[] {1, 2, 3, 4};
assertTrue(Arrays.equals(expectedBytes, bytesRead));
} finally {
Closeables.close(input, true);
}
}
示例9
/**
* If this is the first time calling this method, the buffer will be checked to make sure it
* starts with SOI marker (0xffd8). If the image has been identified as a non-JPEG, data will be
* ignored and false will be returned immediately on all subsequent calls.
*
* <p>This object maintains state of the position of the last read byte. On repeated calls to this
* method, it will continue from where it left off.
*
* @param encodedImage Next set of bytes received by the caller
* @return true if a new full scan has been found
*/
public boolean parseMoreData(final EncodedImage encodedImage) {
if (mParserState == NOT_A_JPEG) {
return false;
}
final int dataBufferSize = encodedImage.getSize();
// Is there any new data to parse?
// mBytesParsed might be greater than size of dataBuffer - that happens when
// we skip more data than is available to read inside doParseMoreData method
if (dataBufferSize <= mBytesParsed) {
return false;
}
final InputStream bufferedDataStream =
new PooledByteArrayBufferedInputStream(
encodedImage.getInputStream(), mByteArrayPool.get(BUFFER_SIZE), mByteArrayPool);
try {
StreamUtil.skip(bufferedDataStream, mBytesParsed);
return doParseMoreData(bufferedDataStream);
} catch (IOException ioe) {
// Does not happen - streams returned by PooledByteBuffers do not throw IOExceptions
Throwables.propagate(ioe);
return false;
} finally {
Closeables.closeQuietly(bufferedDataStream);
}
}
示例10
@Override
public ImageTranscodeResult transcode(
final EncodedImage encodedImage,
final OutputStream outputStream,
@Nullable RotationOptions rotationOptions,
@Nullable final ResizeOptions resizeOptions,
@Nullable ImageFormat outputFormat,
@Nullable Integer quality)
throws IOException {
if (quality == null) {
quality = DEFAULT_JPEG_QUALITY;
}
if (rotationOptions == null) {
rotationOptions = RotationOptions.autoRotate();
}
final int downsampleRatio =
DownsampleUtil.determineSampleSize(
rotationOptions, resizeOptions, encodedImage, mMaxBitmapSize);
InputStream is = null;
try {
final int softwareNumerator =
JpegTranscoderUtils.getSoftwareNumerator(
rotationOptions, resizeOptions, encodedImage, mResizingEnabled);
final int downsampleNumerator =
JpegTranscoderUtils.calculateDownsampleNumerator(downsampleRatio);
final int numerator;
if (mUseDownsamplingRatio) {
numerator = downsampleNumerator;
} else {
numerator = softwareNumerator;
}
is = encodedImage.getInputStream();
if (INVERTED_EXIF_ORIENTATIONS.contains(encodedImage.getExifOrientation())) {
// Use exif orientation to rotate since we can't use the rotation angle for
// inverted exif orientations
final int exifOrientation =
JpegTranscoderUtils.getForceRotatedInvertedExifOrientation(
rotationOptions, encodedImage);
transcodeJpegWithExifOrientation(is, outputStream, exifOrientation, numerator, quality);
} else {
// Use actual rotation angle in degrees to rotate
final int rotationAngle =
JpegTranscoderUtils.getRotationAngle(rotationOptions, encodedImage);
transcodeJpeg(is, outputStream, rotationAngle, numerator, quality);
}
} finally {
Closeables.closeQuietly(is);
}
return new ImageTranscodeResult(
downsampleRatio == DownsampleUtil.DEFAULT_SAMPLE_SIZE
? TranscodeStatus.TRANSCODING_NO_RESIZING
: TranscodeStatus.TRANSCODING_SUCCESS);
}