From a8a50d12008ef3b1471e4967125d36888c5cbdee Mon Sep 17 00:00:00 2001
From: pgamba <pgamba@localhost>
Date: Fri, 02 Nov 2007 14:13:02 +0000
Subject: [PATCH] Fix 2321 - Fixes specially  ReplLDIFInputStream and ReplLDIFOutput stream regarding different cases of entry sizes and buffer sizes

---
 opendj-sdk/opends/src/server/org/opends/server/replication/plugin/ReplLDIFInputStream.java |   50 ++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 42 insertions(+), 8 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/replication/plugin/ReplLDIFInputStream.java b/opendj-sdk/opends/src/server/org/opends/server/replication/plugin/ReplLDIFInputStream.java
index d886480..532b815 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/replication/plugin/ReplLDIFInputStream.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/replication/plugin/ReplLDIFInputStream.java
@@ -45,6 +45,10 @@
   // The domain associated to this import.
   ReplicationDomain domain;
 
+  private byte[] bytes;
+
+  private int index;
+
   /**
    * Creates a new ReplLDIFInputStream that will import entries
    * for a synchronzation domain.
@@ -86,20 +90,50 @@
     if (closed)
       return -1;
 
-    byte[] bytes = domain.receiveEntryBytes();
+    int receivedLength;
+    int copiedLength;
 
-    if (bytes==null)
+    if (bytes == null)
     {
-      closed = true;
-      return -1;
+      // First time this method is called or the previous entry was
+      // finished. Read a new entry and return it.
+      bytes = domain.receiveEntryBytes();
+
+      if (bytes==null)
+      {
+        closed = true;
+        return -1;
+      }
+
+      receivedLength = bytes.length;
+      index = 0;
+    }
+    else
+    {
+      // Some data was left from the previous entry, feed the read with this
+      // data.
+      receivedLength = bytes.length - index;
     }
 
-    int l = bytes.length;
-    for (int i =0; i<l; i++)
+    if (receivedLength <= len)
     {
-      b[off+i] = bytes[i];
+      copiedLength = receivedLength;
     }
-    return l;
+    else
+    {
+      copiedLength = len;
+    }
+
+    for (int i =0; i<copiedLength; i++)
+    {
+      b[off+i] = bytes[index+i];
+    }
+    index += copiedLength;
+
+    if (copiedLength <= len)
+      bytes = null;
+
+    return copiedLength;
   }
 
   /**

--
Gitblit v1.10.0