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

Nicolas Capponi
08.02.2014 7372d4b38952ac3ec6b56ffa8112e42fddb9bd98
Remove toHex and hexStringToByteArray methods in StaticUtils
class in favor of equivalent methods in ByteString class
12 files modified
242 ■■■■ changed files
opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java 158 ●●●●● patch | view | raw | blame | history
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/ByteString.java 44 ●●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/DN.java 4 ●●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/Filter.java 4 ●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/controls/GenericControl.java 3 ●●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/requests/AbstractExtendedRequest.java 4 ●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/requests/GenericExtendedRequestImpl.java 3 ●●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/responses/AbstractExtendedResult.java 4 ●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/responses/AbstractIntermediateResponse.java 4 ●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/responses/GenericExtendedResultImpl.java 4 ●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/responses/GenericIntermediateResponseImpl.java 4 ●●● patch | view | raw | blame | history
opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java
@@ -28,8 +28,6 @@
package com.forgerock.opendj.util;
import static com.forgerock.opendj.ldap.CoreMessages.ERR_HEX_DECODE_INVALID_CHARACTER;
import static com.forgerock.opendj.ldap.CoreMessages.ERR_HEX_DECODE_INVALID_LENGTH;
import java.io.Closeable;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
@@ -1529,37 +1527,6 @@
    }
    /**
     * Converts the provided hexadecimal string to a byte array.
     *
     * @param hexString
     *            The hexadecimal string to convert to a byte array.
     * @return The byte array containing the binary representation of the
     *         provided hex string.
     * @throws java.text.ParseException
     *             If the provided string contains invalid hexadecimal digits or
     *             does not contain an even number of digits.
     */
    public static byte[] hexStringToByteArray(final String hexString) throws ParseException {
        int length;
        if (hexString == null || (length = hexString.length()) == 0) {
            return new byte[0];
        }
        if (length % 2 != 0) {
            final LocalizableMessage message = ERR_HEX_DECODE_INVALID_LENGTH.get(hexString);
            throw new ParseException(message.toString(), 0);
        }
        final int arrayLength = length / 2;
        final byte[] returnArray = new byte[arrayLength];
        for (int i = 0; i < arrayLength; i++) {
            returnArray[i] = hexToByte(hexString.charAt(i * 2), hexString.charAt(i * 2 + 1));
        }
        return returnArray;
    }
    /**
     * Converts the provided pair of characters to a byte.
     *
     * @param c1
@@ -1913,131 +1880,6 @@
    }
    /**
     * Returns a string representation of the contents of the provided byte
     * sequence using hexadecimal characters and a space between each byte.
     *
     * @param bytes
     *            The byte sequence.
     * @return A string representation of the contents of the provided byte
     *         sequence using hexadecimal characters.
     */
    public static String toHex(final ByteSequence bytes) {
        return toHex(bytes, new StringBuilder((bytes.length() - 1) * 3 + 2)).toString();
    }
    /**
     * Appends the string representation of the contents of the provided byte
     * sequence to a string builder using hexadecimal characters and a space
     * between each byte.
     *
     * @param bytes
     *            The byte sequence.
     * @param builder
     *            The string builder to which the hexadecimal representation of
     *            {@code bytes} should be appended.
     * @return The string builder.
     */
    public static StringBuilder toHex(final ByteSequence bytes, final StringBuilder builder) {
        final int length = bytes.length();
        builder.ensureCapacity(builder.length() + (length - 1) * 3 + 2);
        builder.append(StaticUtils.byteToHex(bytes.byteAt(0)));
        for (int i = 1; i < length; i++) {
            builder.append(" ");
            builder.append(StaticUtils.byteToHex(bytes.byteAt(i)));
        }
        return builder;
    }
    /**
     * Appends a string representation of the data in the provided byte sequence
     * to the given string builder using the specified indent.
     * <p>
     * The data will be formatted with sixteen hex bytes in a row followed by
     * the ASCII representation, then wrapping to a new line as necessary. The
     * state of the byte buffer is not changed.
     *
     * @param bytes
     *            The byte sequence.
     * @param builder
     *            The string builder to which the information is to be appended.
     * @param indent
     *            The number of spaces to indent the output.
     * @return The string builder.
     */
    public static StringBuilder toHexPlusAscii(final ByteSequence bytes,
            final StringBuilder builder, final int indent) {
        final StringBuilder indentBuf = new StringBuilder(indent);
        for (int i = 0; i < indent; i++) {
            indentBuf.append(' ');
        }
        final int length = bytes.length();
        int pos = 0;
        while (length - pos >= 16) {
            final StringBuilder asciiBuf = new StringBuilder(17);
            byte currentByte = bytes.byteAt(pos);
            builder.append(indentBuf);
            builder.append(StaticUtils.byteToHex(currentByte));
            asciiBuf.append(byteToASCII(currentByte));
            pos++;
            for (int i = 1; i < 16; i++, pos++) {
                currentByte = bytes.byteAt(pos);
                builder.append(' ');
                builder.append(StaticUtils.byteToHex(currentByte));
                asciiBuf.append(byteToASCII(currentByte));
                if (i == 7) {
                    builder.append("  ");
                    asciiBuf.append(' ');
                }
            }
            builder.append("  ");
            builder.append(asciiBuf);
            builder.append(EOL);
        }
        final int remaining = length - pos;
        if (remaining > 0) {
            final StringBuilder asciiBuf = new StringBuilder(remaining + 1);
            byte currentByte = bytes.byteAt(pos);
            builder.append(indentBuf);
            builder.append(StaticUtils.byteToHex(currentByte));
            asciiBuf.append(byteToASCII(currentByte));
            pos++;
            for (int i = 1; i < 16; i++, pos++) {
                builder.append(' ');
                if (i < remaining) {
                    currentByte = bytes.byteAt(pos);
                    builder.append(StaticUtils.byteToHex(currentByte));
                    asciiBuf.append(byteToASCII(currentByte));
                } else {
                    builder.append("  ");
                }
                if (i == 7) {
                    builder.append("  ");
                    if (i < remaining) {
                        asciiBuf.append(' ');
                    }
                }
            }
            builder.append("  ");
            builder.append(asciiBuf);
            builder.append(EOL);
        }
        return builder;
    }
    /**
     * Appends a lowercase string representation of the contents of the given
     * byte array to the provided buffer. This implementation presumes that the
     * provided string will contain only ASCII characters and is optimized for
opendj-core/src/main/java/org/forgerock/opendj/ldap/AVA.java
@@ -531,7 +531,7 @@
            // octet string.
            try {
                reader.reset();
                return ByteString.wrap(hexStringToByteArray(reader.read(length)));
                return ByteString.valueOfHex(reader.read(length));
            } catch (final Exception e) {
                final LocalizableMessage message =
                        ERR_ATTR_SYNTAX_DN_ATTR_VALUE_DECODE_FAILURE.get(reader.getString(), String
@@ -738,7 +738,7 @@
        if (!attributeType.getNames().iterator().hasNext()) {
            builder.append(attributeType.getOID());
            builder.append("=#");
            StaticUtils.toHex(attributeValue, builder);
            builder.append(attributeValue.toHexString());
        } else {
            final String name = attributeType.getNameOrOID();
            builder.append(name);
@@ -747,7 +747,7 @@
            final Syntax syntax = attributeType.getSyntax();
            if (!syntax.isHumanReadable()) {
                builder.append("#");
                StaticUtils.toHex(attributeValue, builder);
                builder.append(attributeValue.toHexString());
            } else {
                escapeAttributeValue(attributeValue.toString(), builder);
            }
opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteString.java
@@ -26,6 +26,7 @@
 */
package org.forgerock.opendj.ldap;
import static com.forgerock.opendj.ldap.CoreMessages.*;
import static com.forgerock.opendj.util.StaticUtils.*;
import java.io.IOException;
@@ -34,8 +35,10 @@
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.util.Arrays;
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.LocalizedIllegalArgumentException;
import com.forgerock.opendj.util.StaticUtils;
@@ -161,6 +164,36 @@
    }
    /**
     * Returns a byte string containing the bytes of the provided hexadecimal string.
     *
     * @param hexString
     *            The hexadecimal string to convert to a byte array.
     * @return The byte string containing the binary representation of the
     *         provided hex string.
     * @throws java.text.ParseException
     *             If the provided string contains invalid hexadecimal digits or
     *             does not contain an even number of digits.
     */
    public static ByteString valueOfHex(final String hexString) throws ParseException {
        byte[] bytes = null;
        int length = 0;
        if (hexString == null || (length = hexString.length()) == 0) {
            bytes = new byte[0];
        } else {
            if (length % 2 != 0) {
                final LocalizableMessage message = ERR_HEX_DECODE_INVALID_LENGTH.get(hexString);
                throw new ParseException(message.toString(), 0);
            }
            final int arrayLength = length / 2;
            bytes = new byte[arrayLength];
            for (int i = 0; i < arrayLength; i++) {
                bytes[i] = hexToByte(hexString.charAt(i * 2), hexString.charAt(i * 2 + 1));
            }
        }
        return valueOf(bytes);
    }
    /**
     * Returns a byte string containing the contents of the provided byte array.
     * <p>
     * This method differs from {@link #wrap(byte[])} in that it defensively
@@ -574,19 +607,19 @@
    }
    /**
     * Appends a string representation of the data in this byte sequence to the
     * given buffer using the specified indent.
     * Returns a string representation of the data in this byte sequence using
     * the specified indent.
     * <p>
     * The data will be formatted with sixteen hex bytes in a row followed by
     * the ASCII representation, then wrapping to a new line as necessary. The
     * state of the byte buffer is not changed.
     *
     * @param builder
     *            The buffer to which the information is to be appended.
     * @param indent
     *            The number of spaces to indent the output.
     * @return the string representation of this byte string
     */
    public void toHexPlusAsciiString(StringBuilder builder, int indent) {
    public String toHexPlusAsciiString(int indent) {
        StringBuilder builder = new StringBuilder();
        StringBuilder indentBuf = new StringBuilder(indent);
        for (int i = 0; i < indent; i++) {
            indentBuf.append(' ');
@@ -651,6 +684,7 @@
            builder.append(asciiBuf);
            builder.append(EOL);
        }
        return builder.toString();
    }
    /**
opendj-core/src/main/java/org/forgerock/opendj/ldap/DN.java
@@ -998,7 +998,7 @@
        if (!ava.getAttributeType().getNames().iterator().hasNext()) {
            builder.append(ava.getAttributeType().getOID());
            builder.append("=#");
            StaticUtils.toHex(value, builder);
            builder.append(value.toHexString());
        } else {
            final String name = ava.getAttributeType().getNameOrOID();
            // Normalizing.
@@ -1009,7 +1009,7 @@
            final Syntax syntax = ava.getAttributeType().getSyntax();
            if (!syntax.isHumanReadable()) {
                builder.append("#");
                StaticUtils.toHex(value, builder);
                builder.append(value.toHexString());
            } else {
                final String str = value.toString();
                if (str.length() == 0) {
opendj-core/src/main/java/org/forgerock/opendj/ldap/Filter.java
@@ -43,8 +43,6 @@
import org.forgerock.opendj.ldap.schema.Schema;
import org.forgerock.util.Reject;
import com.forgerock.opendj.util.StaticUtils;
/**
 * A search filter as defined in RFC 4511. In addition this class also provides
 * support for the absolute true and absolute false filters as defined in RFC
@@ -422,7 +420,7 @@
                    builder.append('(');
                    builder.append(byteToHex(filterTag));
                    builder.append(':');
                    StaticUtils.toHex(filterBytes, builder);
                    builder.append(filterBytes.toHexString());
                    builder.append(')');
                    return builder;
                }
opendj-core/src/main/java/org/forgerock/opendj/ldap/controls/GenericControl.java
@@ -28,7 +28,6 @@
import org.forgerock.opendj.ldap.ByteString;
import com.forgerock.opendj.util.StaticUtils;
import org.forgerock.util.Reject;
/**
@@ -168,7 +167,7 @@
        builder.append(isCritical());
        if (value != null) {
            builder.append(", value=");
            StaticUtils.toHexPlusAscii(value, builder, 4);
            builder.append(value.toHexPlusAsciiString(4));
        }
        builder.append(")");
        return builder.toString();
opendj-core/src/main/java/org/forgerock/opendj/ldap/requests/AbstractExtendedRequest.java
@@ -31,8 +31,6 @@
import org.forgerock.opendj.ldap.responses.ExtendedResult;
import org.forgerock.opendj.ldap.responses.ExtendedResultDecoder;
import com.forgerock.opendj.util.StaticUtils;
/**
 * An abstract Extended request which can be used as the basis for implementing
 * new Extended operations.
@@ -84,7 +82,7 @@
        builder.append(getOID());
        if (hasValue()) {
            builder.append(", requestValue=");
            StaticUtils.toHexPlusAscii(getValue(), builder, 4);
            builder.append(getValue().toHexPlusAsciiString(4));
        }
        builder.append(", controls=");
        builder.append(getControls());
opendj-core/src/main/java/org/forgerock/opendj/ldap/requests/GenericExtendedRequestImpl.java
@@ -38,7 +38,6 @@
import org.forgerock.opendj.ldap.responses.GenericExtendedResult;
import org.forgerock.opendj.ldap.responses.Responses;
import com.forgerock.opendj.util.StaticUtils;
import org.forgerock.util.Reject;
/**
@@ -154,7 +153,7 @@
        builder.append(getOID());
        if (hasValue()) {
            builder.append(", requestValue=");
            StaticUtils.toHexPlusAscii(getValue(), builder, 4);
            builder.append(getValue().toHexPlusAsciiString(4));
        }
        builder.append(", controls=");
        builder.append(getControls());
opendj-core/src/main/java/org/forgerock/opendj/ldap/responses/AbstractExtendedResult.java
@@ -30,8 +30,6 @@
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.ResultCode;
import com.forgerock.opendj.util.StaticUtils;
/**
 * An abstract Extended result which can be used as the basis for implementing
 * new Extended operations.
@@ -91,7 +89,7 @@
        builder.append(getOID() == null ? "" : getOID());
        if (hasValue()) {
            builder.append(", responseValue=");
            StaticUtils.toHexPlusAscii(getValue(), builder, 4);
            builder.append(getValue().toHexPlusAsciiString(4));
        }
        builder.append(", controls=");
        builder.append(getControls());
opendj-core/src/main/java/org/forgerock/opendj/ldap/responses/AbstractIntermediateResponse.java
@@ -29,8 +29,6 @@
import org.forgerock.opendj.ldap.ByteString;
import com.forgerock.opendj.util.StaticUtils;
/**
 * An abstract Intermediate response which can be used as the basis for
 * implementing new Intermediate responses.
@@ -77,7 +75,7 @@
        builder.append(getOID() == null ? "" : getOID());
        if (hasValue()) {
            builder.append(", responseValue=");
            StaticUtils.toHexPlusAscii(getValue(), builder, 4);
            builder.append(getValue().toHexPlusAsciiString(4));
        }
        builder.append(", controls=");
        builder.append(getControls());
opendj-core/src/main/java/org/forgerock/opendj/ldap/responses/GenericExtendedResultImpl.java
@@ -30,8 +30,6 @@
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.ResultCode;
import com.forgerock.opendj.util.StaticUtils;
/**
 * Generic extended result implementation.
 */
@@ -93,7 +91,7 @@
        builder.append(getOID() == null ? "" : getOID());
        if (hasValue()) {
            builder.append(", responseValue=");
            StaticUtils.toHexPlusAscii(getValue(), builder, 4);
            builder.append(getValue().toHexPlusAsciiString(4));
        }
        builder.append(", controls=");
        builder.append(getControls());
opendj-core/src/main/java/org/forgerock/opendj/ldap/responses/GenericIntermediateResponseImpl.java
@@ -29,8 +29,6 @@
import org.forgerock.opendj.ldap.ByteString;
import com.forgerock.opendj.util.StaticUtils;
/**
 * Generic intermediate response implementation.
 */
@@ -85,7 +83,7 @@
        builder.append(getOID() == null ? "" : getOID());
        if (hasValue()) {
            builder.append(", requestValue=");
            StaticUtils.toHexPlusAscii(getValue(), builder, 4);
            builder.append(getValue().toHexPlusAsciiString(4));
        }
        builder.append(", controls=");
        builder.append(getControls());