public static URI getRelativeOutputPath(URI baseInputDir, URI inputFile, URI outputDir) {
    URI relativePath = baseInputDir.relativize(inputFile);
    Preconditions.checkState(relativePath.getPath().length() > 0 && !relativePath.equals(inputFile),
        "Unable to extract out the relative path for input file '" + inputFile + "', based on base input path: "
            + baseInputDir);
    String outputDirStr = sanitizeURIString(outputDir.toString());
    outputDir = !outputDirStr.endsWith("/") ? URI.create(outputDirStr.concat("/")) : outputDir;
    URI relativeOutputURI = outputDir.resolve(relativePath).resolve(".");
    return relativeOutputURI;
  }
@Test
  public void testInvalidRelativeURIs()
      throws URISyntaxException {
    URI inputDirURI = new URI("hdfs://namenode1:9999/path/to/");
    URI inputFileURI = new URI("hdfs://namenode1:9999/path/to/subdir/file");
    URI outputDirURI = new URI("hdfs://namenode2/output/dir/");
    try {
      SegmentGenerationUtils.getRelativeOutputPath(inputDirURI, inputFileURI, outputDirURI)
          .resolve(new URI("table_OFFLINE_2021-02-01_09:39:00.000_2021-02-01_11:59:00.000_2.tar.gz"));
      Assert.fail("Expected an error thrown for uri resolve with space in segment name");
    } catch (Exception e) {
      Assert.assertTrue(e instanceof URISyntaxException);
    }
    URI outputSegmentTarURI = SegmentGenerationUtils.getRelativeOutputPath(inputDirURI, inputFileURI, outputDirURI)
        .resolve(new URI(URIUtils.encode("table_OFFLINE_2021-02-01_09:39:00.000_2021-02-01_11:59:00.000_2.tar.gz")));
    Assert.assertEquals(outputSegmentTarURI.toString(),
        "hdfs://namenode2/output/dir/subdir/table_OFFLINE_2021-02-01_09%3A39%3A00.000_2021-02-01_11%3A59%3A00.000_2"
            + ".tar.gz");
  }