class Util {

  public static boolean inflate(
      ParsableByteArray input, ParsableByteArray output, @Nullable Inflater inflater) {
    if (input.bytesLeft() <= 0) {
      return false;
    }
    byte[] outputData = output.data;
    if (outputData.length < input.bytesLeft()) {
      outputData = new byte[2 * input.bytesLeft()];
    }
    if (inflater == null) {
      inflater = new Inflater();
    }
    inflater.setInput(input.data, input.getPosition(), input.bytesLeft());
    try {
      int outputSize = 0;
      while (true) {
        outputSize += inflater.inflate(outputData, outputSize, outputData.length - outputSize);
        if (inflater.finished()) {
          output.reset(outputData, outputSize);
          return true;
        }
        if (inflater.needsDictionary() || inflater.needsInput()) {
          return false;
        }
        if (outputSize == outputData.length) {
          outputData = Arrays.copyOf(outputData, outputData.length * 2);
        }
      }
    } catch (DataFormatException e) {
      return false;
    } finally {
      inflater.reset();
    }
  }

  private  Util();

}