From bfdf682424bf2b33530ca240e653865f04b33e8f Mon Sep 17 00:00:00 2001
From: Nicolas Capponi <nicolas.capponi@forgerock.com>
Date: Wed, 08 Jan 2014 11:05:35 +0000
Subject: [PATCH] Add methods in ByteString class to convert to Hex string * Align class with ByteString class from server 2.x
---
opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java | 2
opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteString.java | 99 +++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 100 insertions(+), 1 deletions(-)
diff --git a/opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java b/opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java
index 9e4bb9b..564ba14 100644
--- a/opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java
+++ b/opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java
@@ -2342,7 +2342,7 @@
* space if the provided byte does not have printable ASCII
* representation.
*/
- private static char byteToASCII(final byte b) {
+ public static char byteToASCII(final byte b) {
if (b >= 32 && b <= 126) {
return (char) b;
}
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 2623377..5e8c453 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
@@ -26,6 +26,8 @@
*/
package org.forgerock.opendj.ldap;
+import static com.forgerock.opendj.util.StaticUtils.*;
+
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
@@ -555,6 +557,103 @@
}
/**
+ * Returns a string representation of the contents of this byte sequence
+ * using hexadecimal characters and a space between each byte.
+ *
+ * @return A string representation of the contents of this byte sequence
+ * using hexadecimal characters.
+ */
+ public String 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();
+ }
+
+ /**
+ * Appends a string representation of the data in this byte sequence to the
+ * given buffer 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.
+ */
+ public void toHexPlusAsciiString(StringBuilder builder, int indent) {
+ StringBuilder indentBuf = new StringBuilder(indent);
+ for (int i = 0; i < indent; i++) {
+ indentBuf.append(' ');
+ }
+ int pos = 0;
+ while ((length - pos) >= 16) {
+ StringBuilder asciiBuf = new StringBuilder(17);
+ byte currentByte = buffer[offset + pos];
+ builder.append(indentBuf);
+ builder.append(byteToHex(currentByte));
+ asciiBuf.append(byteToASCII(currentByte));
+ pos++;
+
+ for (int i = 1; i < 16; i++, pos++) {
+ currentByte = buffer[offset + pos];
+ builder.append(' ');
+ builder.append(byteToHex(currentByte));
+ asciiBuf.append(byteToASCII(currentByte));
+
+ if (i == 7) {
+ builder.append(" ");
+ asciiBuf.append(' ');
+ }
+ }
+
+ builder.append(" ");
+ builder.append(asciiBuf);
+ builder.append(EOL);
+ }
+
+ int remaining = (length - pos);
+ if (remaining > 0) {
+ StringBuilder asciiBuf = new StringBuilder(remaining + 1);
+
+ byte currentByte = buffer[offset + pos];
+ builder.append(indentBuf);
+ builder.append(byteToHex(currentByte));
+ asciiBuf.append(byteToASCII(currentByte));
+ pos++;
+
+ for (int i = 1; i < 16; i++, pos++) {
+ builder.append(' ');
+
+ if (i < remaining) {
+ currentByte = buffer[offset + pos];
+ builder.append(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);
+ }
+ }
+
+ /**
* {@inheritDoc}
*/
public byte[] toByteArray() {
--
Gitblit v1.10.0