public static String concatenateWithThreshold(String a, String b, int threshold) {
    // Convert both strings to byte arrays in UTF-8 encoding
    byte[] bytesA = getUTF8Bytes(a);
    byte[] bytesB = getUTF8Bytes(b);
    if (bytesB.length > threshold) {
      throw new IllegalArgumentException(String.format(
          "Length of the Second string to concatenate exceeds the threshold (%d > %d)",
          bytesB.length, threshold));
    }

    // Calculate total bytes
    int totalBytes = bytesA.length + bytesB.length;

    // If total bytes is within the threshold, return concatenated string
    if (totalBytes <= threshold) {
      return a + b;
    }

    // Calculate the maximum bytes 'a' can take
    int bestLength = getBestLength(a, threshold - bytesB.length);

    // Concatenate the valid substring of 'a' with 'b'
    return a.substring(0, bestLength) + b;
  }
@Test
  void testConcatenateTruncateCase() {
    // 'é' is 2 bytes
    assertEquals("ad", concatenateWithThreshold("aé", "d", 3));
    // Chinese chars are 3 bytes
    assertEquals("世d", concatenateWithThreshold("世界", "d", 4));
    assertEquals("ad", concatenateWithThreshold("ab", "d", 2));
  }