From f4b8f759fd6e59b16535187b4772e98f541690cb Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Tue, 03 Nov 2015 16:49:00 +0000
Subject: [PATCH] ByteSequenceReader.java: Added readCompactUnsignedInt() and used it

---
 opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteSequenceReader.java                |   25 ++++++++++++++++++++++++-
 opendj-sdk/opendj-core/src/main/resources/com/forgerock/opendj/ldap/core.properties                   |    2 ++
 opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/ByteStringBuilderTestCase.java         |    2 +-
 opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/TimeBasedMatchingRulesImpl.java |    4 ++--
 4 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteSequenceReader.java b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteSequenceReader.java
index 13507ec..750ffb1 100755
--- a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteSequenceReader.java
+++ b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/ByteSequenceReader.java
@@ -26,6 +26,8 @@
  */
 package org.forgerock.opendj.ldap;
 
+import static com.forgerock.opendj.ldap.CoreMessages.*;
+
 import java.io.IOException;
 import java.io.InputStream;
 
@@ -318,7 +320,7 @@
      *             If there are fewer bytes remaining in this reader than are
      *             required to satisfy the request.
      */
-    public long readCompactUnsigned() {
+    public long readCompactUnsignedLong() {
         try {
             return PackedLong.readCompactUnsignedLong(asInputStream());
         } catch (IOException e) {
@@ -327,6 +329,27 @@
     }
 
     /**
+     * Relative read method for reading a compacted int value.
+     * Compaction allows to reduce number of bytes needed to hold int types
+     * depending on its value (i.e: if value < 128, value will be encoded using one byte only).
+     * Reads the next bytes at this reader's current position, composing them into an int value
+     * according to big-endian byte order, and then increments the position by the size of the
+     * encoded int.
+     *
+     * @return The int value at this reader's current position.
+     * @throws IndexOutOfBoundsException
+     *             If there are fewer bytes remaining in this reader than are
+     *             required to satisfy the request.
+     */
+    public int readCompactUnsignedInt() {
+        long l = readCompactUnsignedLong();
+        if (l > Integer.MAX_VALUE) {
+            throw new IllegalStateException(ERR_INVALID_COMPACTED_UNSIGNED_INT.get(Integer.MAX_VALUE, l).toString());
+        }
+        return (int) l;
+    }
+
+    /**
      * Relative read method for reading an short value. Reads the next 2 bytes at
      * this reader's current position, composing them into an short value
      * according to big-endian byte order, and then increments the position by
diff --git a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/TimeBasedMatchingRulesImpl.java b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/TimeBasedMatchingRulesImpl.java
index 4a80554..c43b786 100644
--- a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/TimeBasedMatchingRulesImpl.java
+++ b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/TimeBasedMatchingRulesImpl.java
@@ -295,7 +295,7 @@
                     int assertHour = reader.readByte();
                     int assertDay = reader.readByte();
                     int assertMonth = reader.readByte();
-                    int assertYear = (int) reader.readCompactUnsigned();
+                    int assertYear = reader.readCompactUnsignedInt();
 
                     List<T> queries = new ArrayList<>();
                     if (assertSecond >= 0) {
@@ -506,7 +506,7 @@
             int assertHour = r.readByte();
             int assertDay = r.readByte();
             int assertMonth = r.readByte();
-            int assertYear = (int) r.readCompactUnsigned();
+            int assertYear = r.readCompactUnsignedInt();
 
             // All the non-zero and non -1 values should match.
             return ConditionResult.valueOf(
diff --git a/opendj-sdk/opendj-core/src/main/resources/com/forgerock/opendj/ldap/core.properties b/opendj-sdk/opendj-core/src/main/resources/com/forgerock/opendj/ldap/core.properties
index dfae9ea..98d3d9c 100755
--- a/opendj-sdk/opendj-core/src/main/resources/com/forgerock/opendj/ldap/core.properties
+++ b/opendj-sdk/opendj-core/src/main/resources/com/forgerock/opendj/ldap/core.properties
@@ -1643,6 +1643,8 @@
  value "%s" could not be parsed as a valid assertion value because the \
  character '%c' is not allowed. The acceptable values are s (second), \
  m (minute), h (hour), D (date), M (month) and Y (year)
+ERR_INVALID_COMPACTED_UNSIGNED_INT="Expected a compacted unsigned int (value less than %d), \
+ but got a compacted unsigned long: %d
 
 # Labels for generated documentation
 DOC_LOCALE_TAG=Code tag: %s
diff --git a/opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/ByteStringBuilderTestCase.java b/opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/ByteStringBuilderTestCase.java
index 0057087..b28bc20 100644
--- a/opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/ByteStringBuilderTestCase.java
+++ b/opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/ByteStringBuilderTestCase.java
@@ -87,7 +87,7 @@
 
     @Test(dataProvider = "unsignedLongValues")
     public void testCanAppendCompactPositiveValue(long value) {
-        assertThat(new ByteStringBuilder().appendCompactUnsigned(value).asReader().readCompactUnsigned())
+        assertThat(new ByteStringBuilder().appendCompactUnsigned(value).asReader().readCompactUnsignedLong())
             .isEqualTo(value);
     }
 

--
Gitblit v1.10.0