com.facebook.common.internal.Closeables Java Examples

The following examples show how to use com.facebook.common.internal.Closeables. 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: LocalFetchProducer.java    From fresco with MIT License 6 votes vote down vote up
/** 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);
  }
}
 
Example #2
Source File: KeyframesDecoderExample.java    From fresco with MIT License 6 votes vote down vote up
@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);
  }
}
 
Example #3
Source File: ProgressiveJpegParser.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
  }
}
 
Example #4
Source File: CloseableReference.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void release(Closeable value) {
  try {
    Closeables.close(value, true);
  } catch (IOException ioe) {
    // This will not happen, Closeable.close swallows and logs IOExceptions
  }
}
 
Example #5
Source File: CloseableReference.java    From fresco with MIT License 5 votes vote down vote up
@Override
public void release(Closeable value) {
  try {
    Closeables.close(value, true);
  } catch (IOException ioe) {
    // This will not happen, Closeable.close swallows and logs IOExceptions
  }
}
 
Example #6
Source File: SharedReferenceTest.java    From fresco with MIT License 5 votes vote down vote up
@Override
public void release(Thing value) {
  try {
    Closeables.close(value, true);
  } catch (IOException ioe) {
    // this should not happen
    Assert.fail();
  }
}
 
Example #7
Source File: StreamUtilTest.java    From fresco with MIT License 5 votes vote down vote up
/** 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);
  }
}
 
Example #8
Source File: StreamUtilTest.java    From fresco with MIT License 5 votes vote down vote up
/** 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);
  }
}
 
Example #9
Source File: ProgressiveJpegParser.java    From fresco with MIT License 5 votes vote down vote up
/**
 * 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);
  }
}
 
Example #10
Source File: NativeJpegTranscoder.java    From fresco with MIT License 4 votes vote down vote up
@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);
}