Example #1
0
  /**
   * Decompresses data from the given stream using the configured compression algorithm. It will
   * throw an exception if the dest buffer does not have enough space to hold the decompressed data.
   *
   * @param dest the output bytes buffer
   * @param destOffset start writing position of the output buffer
   * @param bufferedBoundedStream a stream to read compressed data from, bounded to the exact amount
   *     of compressed data
   * @param compressedSize compressed data size, header not included
   * @param uncompressedSize uncompressed data size, header not included
   * @param compressAlgo compression algorithm used
   * @throws IOException
   */
  public static void decompress(
      byte[] dest,
      int destOffset,
      InputStream bufferedBoundedStream,
      int compressedSize,
      int uncompressedSize,
      Compression.Algorithm compressAlgo)
      throws IOException {

    if (dest.length - destOffset < uncompressedSize) {
      throw new IllegalArgumentException(
          "Output buffer does not have enough space to hold "
              + uncompressedSize
              + " decompressed bytes, available: "
              + (dest.length - destOffset));
    }

    Decompressor decompressor = null;
    try {
      decompressor = compressAlgo.getDecompressor();
      InputStream is =
          compressAlgo.createDecompressionStream(bufferedBoundedStream, decompressor, 0);

      IOUtils.readFully(is, dest, destOffset, uncompressedSize);
      is.close();
    } finally {
      if (decompressor != null) {
        compressAlgo.returnDecompressor(decompressor);
      }
    }
  }