From 12da42ad5df736612d6e7cefccab7dad3e5a3c21 Mon Sep 17 00:00:00 2001
From: Nicolas Capponi <nicolas.capponi@forgerock.com>
Date: Thu, 09 Jan 2014 13:34:16 +0000
Subject: [PATCH] Add ByteStringBuilder#clear(int) and  ByteStringBuilder#capacity() methods  to align class with ByteStringBuilder class from server 2.x

---
 opendj-core/src/test/java/org/forgerock/opendj/ldap/ByteStringBuilderTestCase.java |    8 ++++++++
 opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteStringBuilder.java         |   36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 0 deletions(-)

diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteStringBuilder.java b/opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteStringBuilder.java
index 4c9a05f..c99f6a2 100755
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteStringBuilder.java
+++ b/opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteStringBuilder.java
@@ -720,6 +720,16 @@
     }
 
     /**
+     * Returns the current capacity of this byte string builder. The capacity
+     * may increase as more data is appended.
+     *
+     * @return The current capacity of this byte string builder.
+     */
+    public int capacity() {
+        return buffer.length;
+    }
+
+    /**
      * Sets the length of this byte string builder to zero.
      * <p>
      * <b>NOTE:</b> if this method is called, then
@@ -735,6 +745,32 @@
     }
 
     /**
+     * Sets the length of this byte string builder to zero, and resets the
+     * capacity to the specified size.
+     * <p>
+     * <b>NOTE:</b> if this method is called, then
+     * {@code ByteSequenceReader.rewind()} must also be called on any associated
+     * byte sequence readers in order for them to remain valid.
+     *
+     * @param capacity
+     *            The new capacity.
+     * @return This byte string builder.
+     * @throws IllegalArgumentException
+     *             If the {@code capacity} is negative.
+     * @see #asReader()
+     */
+    public ByteStringBuilder clear(int capacity) {
+        if (capacity < 0) {
+            throw new IllegalArgumentException();
+        }
+        if (capacity != buffer.length) {
+            buffer = new byte[capacity];
+        }
+        length = 0;
+        return this;
+    }
+
+    /**
      * {@inheritDoc}
      */
     public int compareTo(final byte[] bytes, final int offset, final int length) {
diff --git a/opendj-core/src/test/java/org/forgerock/opendj/ldap/ByteStringBuilderTestCase.java b/opendj-core/src/test/java/org/forgerock/opendj/ldap/ByteStringBuilderTestCase.java
index bb30eeb..6eb8f03 100644
--- a/opendj-core/src/test/java/org/forgerock/opendj/ldap/ByteStringBuilderTestCase.java
+++ b/opendj-core/src/test/java/org/forgerock/opendj/ldap/ByteStringBuilderTestCase.java
@@ -124,6 +124,14 @@
         bs.byteAt(0);
     }
 
+    @Test(dataProvider = "builderProvider", expectedExceptions = IndexOutOfBoundsException.class)
+    public void testClearWithNewCapacity(ByteStringBuilder bs, byte[] ba) {
+        bs.clear(123);
+        Assert.assertEquals(bs.length(), 0);
+        Assert.assertEquals(bs.capacity(), 123);
+        bs.byteAt(0);
+    }
+
     @Test
     public void testEnsureAdditionalCapacity() {
         final ByteStringBuilder bsb = new ByteStringBuilder(8);

--
Gitblit v1.10.0