IOUtils { public static String toSafeFilename(String input) { return defaultString(input).replaceAll("\\s|\\h", " ").replaceAll("[\0\f\t\n\r\\\\/:*?\\\"<>|]", "") .trim(); } 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 nullSafe() { assertEquals("", IOUtils.toSafeFilename(null)); }
@Test public void testSafeFilename() { assertEquals("1_Invoice#0001", IOUtils.toSafeFilename("1_Invoice#0001:*<>/\\")); assertEquals("..test", IOUtils.toSafeFilename("../test")); assertEquals("..test", IOUtils.toSafeFilename("..\\test")); assertEquals(".test", IOUtils.toSafeFilename("./test")); assertEquals("rest", IOUtils.toSafeFilename("\r\n\t\frest")); }
@Test public void safeFilenameWhitespaces() { assertEquals("Chuck Norris", IOUtils.toSafeFilename("Chuck\tNorris")); assertEquals("Chuck Norris", IOUtils.toSafeFilename("\u00A0Chuck\u00A0Norris\u00A0")); assertEquals("Chuck Norris", IOUtils.toSafeFilename("\u00A0\n\t\u000B\f\rChuck\nNorris\u202f")); assertEquals("This is a Chuck Norris roundkick, will Steven Segal survive Nope!", IOUtils.toSafeFilename( "This\u1680is\u180ea\u2000Chuck\u200aNorris\u202froundkick,\u205fwill\u3000Steven\fSegal\rsurvive?\u000BNope!")); }