IOUtils { static String shortenFilenameBytesLength(String input, int maxBytesLength, Charset charset) { if (input.getBytes(charset).length > maxBytesLength) { String baseName = getBaseName(input); String ext = getExtension(input); baseName = baseName.substring(0, baseName.length() - 1); String shorterFilename = String.format("%s.%s", baseName, ext); while (shorterFilename.getBytes(charset).length > maxBytesLength) { baseName = baseName.substring(0, baseName.length() - 1); shorterFilename = String.format("%s.%s", baseName, ext); } return shorterFilename; } return input; } private  IOUtils(); static File createTemporaryBuffer(TaskOutput taskOut); static File createTemporaryBuffer(); static File createTemporaryBuffer(String extension); static File createTemporaryBufferWithName(String filename); static File createTemporaryFolder(); static File findNewNameThatDoesNotExist(File output); static String shortenFilename(String name); static String toSafeFilename(String input); static String toStrictFilename(String input);  }
@Test public void shortenFilenameBytesLength() { String shorter = shortenFilenameBytesLength(largeFilename); assertThat(shorter, endsWith("aวaวa.pdf")); assertThat(shorter, startsWith("aวaว")); assertThat(shorter.getBytes(StandardCharsets.UTF_8).length, is(253)); }