mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Nicolas Capponi
30.44.2014 d0d903597fda19eeb6767071894a2ae95c94fe70
Add ByteString.toPercentHexString() method
Remove ByteString.toHexString(char) method

toPercentHexString() generates a hex string with '%' character
before every hex code
4 files modified
56 ■■■■ changed files
opendj-core/src/main/java/org/forgerock/opendj/ldap/AVA.java 6 ●●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteSequenceReader.java 19 ●●●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteString.java 23 ●●●●● patch | view | raw | blame | history
opendj-core/src/test/java/org/forgerock/opendj/ldap/ByteStringTestCase.java 8 ●●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/AVA.java
@@ -847,7 +847,7 @@
        final boolean hasAttributeName = !attributeType.getNames().isEmpty();
        final boolean isHumanReadable = attributeType.getSyntax().isHumanReadable();
        if (!hasAttributeName || !isHumanReadable) {
            builder.append(value.toHexString(AVA.HEX_STRING_SEPARATOR));
            builder.append(value.toPercentHexString());
        } else {
            // try to decode value as UTF-8 string
            final CharBuffer buffer = CharBuffer.allocate(value.length());
@@ -861,10 +861,10 @@
                    builder.append(val);
                } catch (UnsupportedEncodingException e) {
                    // should never happen
                    builder.append(value.toHexString(AVA.HEX_STRING_SEPARATOR));
                    builder.append(value.toPercentHexString());
                }
            } else {
                builder.append(value.toHexString(AVA.HEX_STRING_SEPARATOR));
                builder.append(value.toPercentHexString());
            }
        }
        return builder;
opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteSequenceReader.java
@@ -393,6 +393,25 @@
    }
    /**
     * Returns the number of bytes between the current position and the position of first occurence of provided byte.
     *
     * @param b
     *          the byte to look for
     * @return the number of bytes between current position and position of provided byte
     * @throws IndexOutOfBoundsException
     *           if the byte to look for is not present in this sequence
     */
    public int remainingUpTo(byte b)
    {
      int length = 0;
      while (peek(length) != b)
      {
        length++;
      }
      return length;
    }
    /**
     * Rewinds this reader's position to zero.
     * <p>
     * An invocation of this method of the form:
opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteString.java
@@ -756,23 +756,26 @@
     *         using hexadecimal characters.
     */
    public String toHexString() {
        return toHexString(' ');
        StringBuilder builder = new StringBuilder((length - 1) * 3 + 2);
        builder.append(StaticUtils.byteToHex(buffer[offset]));
        for (int i = 1; i < length; i++) {
            builder.append(' ');
            builder.append(StaticUtils.byteToHex(buffer[offset + i]));
        }
        return builder.toString();
    }
    /**
     * Returns a string representation of the contents of this byte sequence
     * using hexadecimal characters and the provided separator between each byte.
     * using hexadecimal characters and a percent prefix (#) before each char.
     *
     * @param separator
     *          Character used to separate each byte
     * @return A string representation of the contents of this byte sequence
     *         using hexadecimal characters.
     *         using percent + hexadecimal characters.
     */
    public String toHexString(char separator) {
        StringBuilder builder = new StringBuilder((length - 1) * 3 + 2);
        builder.append(StaticUtils.byteToHex(buffer[offset]));
        for (int i = 1; i < length; i++) {
            builder.append(separator);
    public String toPercentHexString() {
        StringBuilder builder = new StringBuilder(length * 3);
        for (int i = 0; i < length; i++) {
            builder.append('%');
            builder.append(StaticUtils.byteToHex(buffer[offset + i]));
        }
        return builder.toString();
opendj-core/src/test/java/org/forgerock/opendj/ldap/ByteStringTestCase.java
@@ -267,7 +267,13 @@
    public void testToHex() throws Exception {
        ByteString byteString = new ByteStringBuilder().append("org=example").toByteString();
        assertThat(byteString.toHexString()).isEqualTo("6F 72 67 3D 65 78 61 6D 70 6C 65");
        assertThat(byteString.toHexString('-')).isEqualTo("6F-72-67-3D-65-78-61-6D-70-6C-65");
    }
    @Test
    public void testToPercentHex() throws Exception {
        ByteString byteString = new ByteStringBuilder().append("org=example").toByteString();
        assertThat(byteString.toPercentHexString())
            .isEqualTo("%6F%72%67%3D%65%78%61%6D%70%6C%65");
    }
    @Test