PROTON-1889: ensure array indexing is updated if needed when appending new content to the composite
diff --git a/proton-j/src/main/java/org/apache/qpid/proton/codec/CompositeReadableBuffer.java b/proton-j/src/main/java/org/apache/qpid/proton/codec/CompositeReadableBuffer.java
index 5a030f0..f5f2e2a 100644
--- a/proton-j/src/main/java/org/apache/qpid/proton/codec/CompositeReadableBuffer.java
+++ b/proton-j/src/main/java/org/apache/qpid/proton/codec/CompositeReadableBuffer.java
@@ -666,8 +666,12 @@
             contents.add(currentArray);
             contents.add(array);
             currentArrayIndex = 0;
+            // If we exhausted the array previously then it should move to the new one now.
+            maybeMoveToNextArray();
         } else {
             contents.add(array);
+            // If we exhausted the list previously then it didn't move onward at the time, so it should now.
+            maybeMoveToNextArray();
         }
 
         capacity += array.length;
diff --git a/proton-j/src/test/java/org/apache/qpid/proton/codec/CompositeReadableBufferTest.java b/proton-j/src/test/java/org/apache/qpid/proton/codec/CompositeReadableBufferTest.java
index 09dc427..136f655 100644
--- a/proton-j/src/test/java/org/apache/qpid/proton/codec/CompositeReadableBufferTest.java
+++ b/proton-j/src/test/java/org/apache/qpid/proton/codec/CompositeReadableBufferTest.java
@@ -1108,6 +1108,78 @@
     //----- Test appending data to the buffer --------------------------------//
 
     @Test
+    public void testAppendToBufferAtEndOfContentArray() {
+        CompositeReadableBuffer buffer = new CompositeReadableBuffer();
+
+        byte[] source1 = new byte[] { 0, 1, 2, 3 };
+
+        buffer.append(source1);
+
+        assertTrue(buffer.hasArray());
+        assertEquals(0, buffer.getArrays().size());
+        assertEquals(-1, buffer.getCurrentIndex());
+
+        buffer.position(source1.length);
+
+        assertFalse(buffer.hasRemaining());
+        assertEquals(0, buffer.remaining());
+        assertEquals(-1, buffer.getCurrentIndex());
+
+        byte[] source2 = new byte[] { 4, 5, 6, 7 };
+        buffer.append(source2);
+
+        assertTrue(buffer.hasRemaining());
+        assertEquals(source2.length, buffer.remaining());
+        assertFalse(buffer.hasArray());
+        assertEquals(2, buffer.getArrays().size());
+        assertEquals(1, buffer.getCurrentIndex());
+        assertEquals(source1.length, buffer.position());
+
+        // Check each position in the array is read
+        for(int i = 0; i < source2.length; i++) {
+            assertEquals(1, buffer.getCurrentIndex());
+            assertEquals(source1.length + i, buffer.get());
+        }
+    }
+
+    @Test
+    public void testAppendToBufferAtEndOfContentList() {
+        CompositeReadableBuffer buffer = new CompositeReadableBuffer();
+
+        byte[] source1 = new byte[] { 0, 1, 2, 3 };
+        byte[] source2 = new byte[] { 4, 5, 6, 7 };
+
+        buffer.append(source1);
+        buffer.append(source2);
+
+        assertFalse(buffer.hasArray());
+        assertEquals(2, buffer.getArrays().size());
+        assertEquals(0, buffer.getCurrentIndex());
+
+        buffer.position(source1.length + source2.length);
+
+        assertFalse(buffer.hasRemaining());
+        assertEquals(0, buffer.remaining());
+        assertEquals(1, buffer.getCurrentIndex());
+
+        byte[] source3 = new byte[] { 8, 9, 10, 11 };
+        buffer.append(source3);
+
+        assertTrue(buffer.hasRemaining());
+        assertEquals(source3.length, buffer.remaining());
+        assertFalse(buffer.hasArray());
+        assertEquals(3, buffer.getArrays().size());
+        assertEquals(2, buffer.getCurrentIndex());
+        assertEquals(source1.length + source2.length, buffer.position());
+
+        // Check each position in the array is read
+        for(int i = 0; i < source3.length; i++) {
+            assertEquals(2, buffer.getCurrentIndex());
+            assertEquals(source1.length + source2.length + i, buffer.get());
+        }
+    }
+
+    @Test
     public void testAppendOne() {
         CompositeReadableBuffer buffer = new CompositeReadableBuffer();