private @Nullable CloseableReference<Bitmap> decodeFromStream(
      InputStream inputStream,
      BitmapFactory.Options options,
      @Nullable Rect regionToDecode,
      @Nullable final ColorSpace colorSpace) {
    Preconditions.checkNotNull(inputStream);
    int targetWidth = options.outWidth;
    int targetHeight = options.outHeight;
    if (regionToDecode != null) {
      targetWidth = regionToDecode.width() / options.inSampleSize;
      targetHeight = regionToDecode.height() / options.inSampleSize;
    }
    @Nullable Bitmap bitmapToReuse = null;
    boolean shouldUseHardwareBitmapConfig = false;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      shouldUseHardwareBitmapConfig =
          mPreverificationHelper != null
              && mPreverificationHelper.shouldUseHardwareBitmapConfig(options.inPreferredConfig);
    }
    if (regionToDecode == null && shouldUseHardwareBitmapConfig) {
      // Cannot reuse bitmaps with Bitmap.Config.HARDWARE
      options.inMutable = false;
    } else {
      if (regionToDecode != null && shouldUseHardwareBitmapConfig) {
        // If region decoding was requested we need to fallback to default config
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
      }
      if (!mAvoidPoolGet) {
        final int sizeInBytes = getBitmapSize(targetWidth, targetHeight, options);
        bitmapToReuse = mBitmapPool.get(sizeInBytes);
        if (bitmapToReuse == null) {
          throw new NullPointerException("BitmapPool.get returned null");
        }
      }
    }
    // inBitmap can be nullable
    //noinspection ConstantConditions
    options.inBitmap = bitmapToReuse;

    // Performs transformation at load time to target color space.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      options.inPreferredColorSpace =
          colorSpace == null ? ColorSpace.get(ColorSpace.Named.SRGB) : colorSpace;
    }

    Bitmap decodedBitmap = null;
    ByteBuffer byteBuffer = mDecodeBuffers.acquire();
    if (byteBuffer == null) {
      byteBuffer = ByteBuffer.allocate(DecodeBufferHelper.getRecommendedDecodeBufferSize());
    }
    try {
      options.inTempStorage = byteBuffer.array();
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
          && regionToDecode != null
          && bitmapToReuse != null
          && options.inPreferredConfig != null) {
        BitmapRegionDecoder bitmapRegionDecoder = null;
        try {
          bitmapToReuse.reconfigure(targetWidth, targetHeight, options.inPreferredConfig);
          bitmapRegionDecoder = BitmapRegionDecoder.newInstance(inputStream, true);
          if (bitmapRegionDecoder != null) {
            decodedBitmap = bitmapRegionDecoder.decodeRegion(regionToDecode, options);
          }
        } catch (IOException e) {
          FLog.e(TAG, "Could not decode region %s, decoding full bitmap instead.", regionToDecode);
        } finally {
          if (bitmapRegionDecoder != null) {
            bitmapRegionDecoder.recycle();
          }
        }
      }
      if (decodedBitmap == null) {
        decodedBitmap = BitmapFactory.decodeStream(inputStream, null, options);
      }
    } catch (IllegalArgumentException e) {
      if (bitmapToReuse != null) {
        mBitmapPool.release(bitmapToReuse);
      }
      // This is thrown if the Bitmap options are invalid, so let's just try to decode the bitmap
      // as-is, which might be inefficient - but it works.
      try {
        // We need to reset the stream first
        inputStream.reset();

        Bitmap naiveDecodedBitmap = BitmapFactory.decodeStream(inputStream);
        if (naiveDecodedBitmap == null) {
          throw e;
        }
        return CloseableReference.of(naiveDecodedBitmap, SimpleBitmapReleaser.getInstance());
      } catch (IOException re) {
        // We throw the original exception instead since it's the one causing this workaround in the
        // first place.
        throw e;
      }
    } catch (RuntimeException re) {
      if (bitmapToReuse != null) {
        mBitmapPool.release(bitmapToReuse);
      }
      throw re;
    } finally {
      mDecodeBuffers.release(byteBuffer);
    }

    // If bitmap with Bitmap.Config.HARDWARE was used, `bitmapToReuse` will be null and it's
    // expected
    if (bitmapToReuse != null && bitmapToReuse != decodedBitmap) {
      mBitmapPool.release(bitmapToReuse);
      if (decodedBitmap != null) {
        decodedBitmap.recycle();
      }
      throw new IllegalStateException();
    }

    if (mAvoidPoolRelease) {
      return CloseableReference.of(decodedBitmap, NoOpResourceReleaser.INSTANCE);
    } else {
      return CloseableReference.of(decodedBitmap, mBitmapPool);
    }
  }
Create a bitmap from an input stream.

@param inputStream the InputStream
@param options the {@link android.graphics.BitmapFactory.Options} used to decode the stream
@param regionToDecode optional image region to decode or null to decode the whole image
@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
imagepipeline/src/main/java/com/facebook/imagepipeline/platform/DefaultDecoder.java