private static String getTruncatedFilename(
      String path, String part1, String middle, String part2, String extension) {
    final var maxFilename = 250;

    int length =
        path.length() + part1.length() + middle.length() + part2.length() + extension.length();
    if (length <= maxFilename) {
      return path + part1 + middle + part2 + extension;
    }

    int overflow = length - maxFilename;

    // First, shorten the longer of the two strings.
    String one = part1;
    String two = part2;
    if (part1.length() > part2.length()) {
      one =
          part1.substring(
              0, max(part2.length(), part1.length() > overflow ? part1.length() - overflow : 0));
      overflow -= part1.length() - one.length();
    } else if (part2.length() > part1.length()) {
      two =
          part2.substring(
              0, max(part1.length(), part2.length() > overflow ? part2.length() - overflow : 0));
      overflow -= part2.length() - two.length();
    }
    if (overflow == 0) {
      return path + one + middle + two + extension;
    }

    // Second, if that still wasn't enough, shorten both strings equally.
    verify(one.length() == two.length());
    if (overflow / 2 >= one.length()) {
      throw new IllegalArgumentException(
          "Cannot create a valid filename, choose shorter input names/directories: "
              + path
              + part1
              + middle
              + part2
              + extension);
    }
    return path
        + part1.substring(0, one.length() - overflow / 2)
        + middle
        + part2.substring(0, two.length() - overflow / 2)
        + extension;
  }
Creates a fully specified filename no longer than 250 characters.

<p>It should behave exactly the same as the engine's main_portable.cc version. Note: path must
include a trailing slash, extension needs to start with a dot.
java/ui/src/main/java/com/google/security/zynamics/bindiff/processes/DiffProcess.java