PROTON-1889: ensure array indexing is updated if needed when appending new content to the composite
(cherry picked from commit c5e2f7990ca82cdf682c8ee3e27ad611447e542e) Project: http://git-wip-us.apache.org/repos/asf/qpid-proton-j/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton-j/commit/e740e8a1 Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton-j/tree/e740e8a1 Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton-j/diff/e740e8a1 Branch: refs/heads/0.27.x Commit: e740e8a1ac1587aa319835a7e82c801fa399c009 Parents: 0fe5dab Author: Robbie Gemmell <[email protected]> Authored: Mon Jul 9 16:57:42 2018 +0100 Committer: Robbie Gemmell <[email protected]> Committed: Fri Jul 13 19:18:25 2018 +0100 ---------------------------------------------------------------------- .../proton/codec/CompositeReadableBuffer.java | 4 ++ .../codec/CompositeReadableBufferTest.java | 72 ++++++++++++++++++++ 2 files changed, 76 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/e740e8a1/proton-j/src/main/java/org/apache/qpid/proton/codec/CompositeReadableBuffer.java ---------------------------------------------------------------------- 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 @@ public class CompositeReadableBuffer implements ReadableBuffer { 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; http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/e740e8a1/proton-j/src/test/java/org/apache/qpid/proton/codec/CompositeReadableBufferTest.java ---------------------------------------------------------------------- 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 @@ public class CompositeReadableBufferTest { //----- 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(); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
