From d0d903597fda19eeb6767071894a2ae95c94fe70 Mon Sep 17 00:00:00 2001
From: Nicolas Capponi <nicolas.capponi@forgerock.com>
Date: Tue, 30 Dec 2014 08:44:41 +0000
Subject: [PATCH] Add ByteString.toPercentHexString() method Remove ByteString.toHexString(char) method
---
opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteSequenceReader.java | 19 +++++++++++++++++++
opendj-core/src/main/java/org/forgerock/opendj/ldap/AVA.java | 6 +++---
opendj-core/src/test/java/org/forgerock/opendj/ldap/ByteStringTestCase.java | 8 +++++++-
opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteString.java | 23 +++++++++++++----------
4 files changed, 42 insertions(+), 14 deletions(-)
diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldap/AVA.java b/opendj-core/src/main/java/org/forgerock/opendj/ldap/AVA.java
index 6562263..0be8efa 100644
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldap/AVA.java
+++ b/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;
diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteSequenceReader.java b/opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteSequenceReader.java
index 0433e0c..1add8e2 100755
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteSequenceReader.java
+++ b/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:
diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteString.java b/opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteString.java
index 160b392..508212f 100755
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteString.java
+++ b/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();
diff --git a/opendj-core/src/test/java/org/forgerock/opendj/ldap/ByteStringTestCase.java b/opendj-core/src/test/java/org/forgerock/opendj/ldap/ByteStringTestCase.java
index dedeea1..dc7b599 100644
--- a/opendj-core/src/test/java/org/forgerock/opendj/ldap/ByteStringTestCase.java
+++ b/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
--
Gitblit v1.10.0