LdapEncoder { public static String nameEncode(String value) { if (value == null) return null; StringBuilder encodedValue = new StringBuilder(value.length() * 2); int length = value.length(); int last = length - 1; for (int i = 0; i < length; i++) { char c = value.charAt(i); if (c == ' ' && (i == 0 || i == last)) { encodedValue.append("\\ "); continue; } if (c < NAME_ESCAPE_TABLE.length) { String esc = NAME_ESCAPE_TABLE[c]; if (esc != null) { encodedValue.append(esc); continue; } } encodedValue.append(c); } return encodedValue.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 testNameEncode() { String res = LdapEncoder.nameEncode("# foo ,+\"\\<>; "); assertThat(res).isEqualTo("\\# foo \\,\\+\\\"\\\\\\<\\>\\;\\ "); }