LdapEncoder { static public String nameDecode(String value) throws BadLdapGrammarException { if (value == null) return null; StringBuilder decoded = new StringBuilder(value.length()); int i = 0; while (i < value.length()) { char currentChar = value.charAt(i); if (currentChar == '\\') { if (value.length() <= i + 1) { throw new BadLdapGrammarException( "Unexpected end of value " + "unterminated '\\'"); } else { char nextChar = value.charAt(i + 1); if (nextChar == ',' || nextChar == '=' || nextChar == '+' || nextChar == '<' || nextChar == '>' || nextChar == '#' || nextChar == ';' || nextChar == '\\' || nextChar == '\"' || nextChar == ' ') { decoded.append(nextChar); i += 2; } else { if (value.length() <= i + 2) { throw new BadLdapGrammarException( "Unexpected end of value " + "expected special or hex, found '" + nextChar + "'"); } else { String hexString = "" + nextChar + value.charAt(i + 2); decoded.append((char) Integer.parseInt(hexString, HEX)); i += 3; } } } } else { decoded.append(currentChar); i++; } } return decoded.toString(); } private  LdapEncoder(); static String filterEncode(String value); static String nameEncode(String value); static String nameDecode(String value); static String printBase64Binary(byte[] val); static byte[] parseBase64Binary(String val);  }
@Test public void testNameDecode() { String res = LdapEncoder .nameDecode("\\# foo \\,\\+\\\"\\\\\\<\\>\\;\\ "); assertThat(res).isEqualTo("# foo ,+\"\\<>; "); }
@Test(expected = BadLdapGrammarException.class) public void testNameDecode_slashlast() { LdapEncoder.nameDecode("\\"); }