TextSanitizer { public String filterInvalidChars(String s) { if (s == null) return null; StringBuilder result = new StringBuilder(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (isValidChar(c)) { result.append(c); } } return result.toString(); }  String filterInvalidChars(String s); List<String> filterInvalidChars(List<String> list);  }
@Test public void testFilterString() throws Exception { String s = new String(new byte[] {-53}); System.out.println("char: "+s); Assert.assertEquals("", textSanitizer.filterInvalidChars(s)); List<String> list = new ArrayList<String>(); list.add("normal"); list.add("string with some cool unicode\u2603"); list.add("broken"+new String(new byte[]{-30, -3, -123})); list.add("string \u0000with\u0000 NUL\u0000"); Assert.assertEquals("normal", textSanitizer.filterInvalidChars(list).get(0)); Assert.assertEquals("string with some cool unicode\u2603", textSanitizer.filterInvalidChars(list).get(1)); Assert.assertEquals("broken", textSanitizer.filterInvalidChars(list).get(2)); Assert.assertEquals("string with NUL", textSanitizer.filterInvalidChars(list).get(3)); }