MessageUtil { public static String unescapeUnicode(final String message) { if (message == null) { return null; } final StringBuilder res = new StringBuilder(); int processedUntil = 0; final Matcher m = UNESCAPE_UNICODE_PATTERN.matcher(message); while (m.find()) { res.append(message.substring(processedUntil, m.start())); if (m.group(1) != null) { res.append('\\'); } else { final String substr = message.substring(m.start(3), m.end(3)); if (m.end(3) - m.start(3) != 4) { throw new IllegalArgumentException("Unfinished Unicode-Sequence in `" + message + "` (" + substr + ")"); } final char c = (char) Integer.parseInt(substr, 16); res.append(c); } processedUntil = m.end(); } res.append(message.substring(processedUntil)); return res.toString(); } private  MessageUtil(); static String unescapeUnicode(final String message); static String filterNonPrintableCharacters(String text);  }
@Test public void testUnescaping() throws Exception { String msg = "12345\\u001E00\\\\u001e11\\u0004"; String processed = MessageUtil.unescapeUnicode(msg); assertEquals("12345\u001E00\\u001e11\u0004", processed); msg = "1\\u00\\x"; try { processed = MessageUtil.unescapeUnicode(msg); fail("Expected an IllegalArgumentException for an unfinished Unicode escape sequence"); } catch (IllegalArgumentException e) { } msg = "1\\\\u001E"; processed = MessageUtil.unescapeUnicode(msg); assertEquals("1\\u001E", processed); }