@HiveDecimalWritableVersionV2
  public byte[] bigIntegerBytesCopy(
      long[] scratchLongs, byte[] scratchBuffer) {

    if (!isSet()) {
      throw new RuntimeException("no value set");
    }

    int byteLength =
        fastBigIntegerBytes(
            scratchLongs, scratchBuffer);
    if (byteLength == 0) {
      throw new RuntimeException("Couldn't convert decimal to binary");
    }
    return Arrays.copyOf(scratchBuffer, byteLength);
  }
Allocate scratchLongs with HiveDecimal.SCRATCH_LONGS_LEN longs.
And, allocate scratch scratchBuffer with HiveDecimal.SCRATCH_BUFFER_LEN_BIG_INTEGER_BYTES bytes.
storage-api/src/java/org/apache/hadoop/hive/serde2/io/HiveDecimalWritable.java