From 3b9f29645c2aa3171e5a4bd821a5254e83a5d3a7 Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Fri, 23 May 2014 15:17:15 +0000
Subject: [PATCH] (CR-3599) Convert all protocols message to use ByteArrayBuilder + ByteArrayScanner

---
 opendj-sdk/opends/src/server/org/opends/server/replication/protocol/WindowMsg.java |   69 +++++++---------------------------
 1 files changed, 14 insertions(+), 55 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/replication/protocol/WindowMsg.java b/opendj-sdk/opends/src/server/org/opends/server/replication/protocol/WindowMsg.java
index e638d45..1bcff24 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/replication/protocol/WindowMsg.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/replication/protocol/WindowMsg.java
@@ -22,14 +22,12 @@
  *
  *
  *      Copyright 2006-2008 Sun Microsystems, Inc.
- *      Portions copyright 2013 ForgeRock AS.
+ *      Portions copyright 2013-2014 ForgeRock AS.
  */
 package org.opends.server.replication.protocol;
 
-import java.io.UnsupportedEncodingException;
 import java.util.zip.DataFormatException;
 
-
 /**
  * This message is used by LDAP server or by Replication Servers to
  * update the send window of the remote entities.
@@ -43,7 +41,6 @@
 {
   private final int numAck;
 
-
   /**
    * Create a new WindowMsg.
    *
@@ -63,64 +60,28 @@
    * @throws DataFormatException If the byte array does not contain a valid
    *                             encoded form of the WindowMsg.
    */
-  public WindowMsg(byte[] in) throws DataFormatException
+  WindowMsg(byte[] in) throws DataFormatException
   {
-    /* The WindowMsg is encoded in the form :
-     * <numAck>
-     */
-    try
+    final ByteArrayScanner scanner = new ByteArrayScanner(in);
+    final byte msgType = scanner.nextByte();
+    if (msgType != MSG_TYPE_WINDOW)
     {
-      /* first byte is the type */
-      if (in[0] != MSG_TYPE_WINDOW)
-        throw new DataFormatException("input is not a valid Window Message");
-      int pos = 1;
-
-      /*
-       * read the number of acks contained in this message.
-       * first calculate the length then construct the string
-       */
-      int length = getNextLength(in, pos);
-      String numAckStr = new String(in, pos, length, "UTF-8");
-      pos += length +1;
-      numAck = Integer.parseInt(numAckStr);
-    } catch (UnsupportedEncodingException e)
-    {
-      throw new DataFormatException("UTF-8 is not supported by this jvm.");
+      throw new DataFormatException("input is not a valid Window Message");
     }
+
+    numAck = scanner.nextIntUTF8();
   }
 
-  /**
-   * {@inheritDoc}
-   */
+  /** {@inheritDoc} */
   @Override
   public byte[] getBytes(short protocolVersion)
   {
-    /*
-     * WindowMsg contains.
-     * <numAck>
-     */
-    try {
-      byte[] byteNumAck = String.valueOf(numAck).getBytes("UTF-8");
-
-      int length = 1 + byteNumAck.length + 1;
-
-      byte[] resultByteArray = new byte[length];
-
-      /* put the type of the operation */
-      resultByteArray[0] = MSG_TYPE_WINDOW;
-      int pos = 1;
-
-      pos = addByteArray(byteNumAck, resultByteArray, pos);
-
-      return resultByteArray;
-    }
-    catch (UnsupportedEncodingException e)
-    {
-      return null;
-    }
+    final ByteArrayBuilder builder = new ByteArrayBuilder();
+    builder.append(MSG_TYPE_WINDOW);
+    builder.appendUTF8(numAck);
+    return builder.toByteArray();
   }
 
-
   /**
    * Get the number of message acknowledged by the Window Message.
    *
@@ -131,9 +92,7 @@
     return numAck;
   }
 
-  /**
-   * {@inheritDoc}
-   */
+  /** {@inheritDoc} */
   @Override
   public String toString()
   {

--
Gitblit v1.10.0