Fixed a bug in the getSlice() method : we were setting the position before the limit() (DIRMINA-981)
Project: http://git-wip-us.apache.org/repos/asf/mina/repo Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/0a30a079 Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/0a30a079 Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/0a30a079 Branch: refs/heads/2.0 Commit: 0a30a079fe2dca721cc6f049d0b4563d98844268 Parents: 3b793f3 Author: Emmanuel Lécharny <[email protected]> Authored: Tue Sep 2 08:54:04 2014 +0200 Committer: Emmanuel Lécharny <[email protected]> Committed: Tue Sep 2 08:54:04 2014 +0200 ---------------------------------------------------------------------- .../mina/core/buffer/AbstractIoBuffer.java | 94 ++++++++++---------- .../apache/mina/core/buffer/IoBufferTest.java | 34 +++++++ 2 files changed, 81 insertions(+), 47 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mina/blob/0a30a079/mina-core/src/main/java/org/apache/mina/core/buffer/AbstractIoBuffer.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/core/buffer/AbstractIoBuffer.java b/mina-core/src/main/java/org/apache/mina/core/buffer/AbstractIoBuffer.java index 5a6395c..45449db 100644 --- a/mina-core/src/main/java/org/apache/mina/core/buffer/AbstractIoBuffer.java +++ b/mina-core/src/main/java/org/apache/mina/core/buffer/AbstractIoBuffer.java @@ -1193,12 +1193,12 @@ public abstract class AbstractIoBuffer extends IoBuffer { } clear(); - position(index); limit(endIndex); + position(index); IoBuffer slice = slice(); - position(pos); limit(limit); + position(pos); return slice; } @@ -2232,8 +2232,8 @@ public abstract class AbstractIoBuffer extends IoBuffer { putInt(newPos - oldPos - 4); position(newPos); return this; - } - + } + /** * {@inheritDoc} */ @@ -2319,40 +2319,40 @@ public abstract class AbstractIoBuffer extends IoBuffer { public IoBuffer fill(byte value, int size) { autoExpand(size); int q = size >>> 3; - int r = size & 7; + int r = size & 7; - if (q > 0) { - int intValue = value | value << 8 | value << 16 | value << 24; - long longValue = intValue; - longValue <<= 32; - longValue |= intValue; + if (q > 0) { + int intValue = value | value << 8 | value << 16 | value << 24; + long longValue = intValue; + longValue <<= 32; + longValue |= intValue; - for (int i = q; i > 0; i--) { - putLong(longValue); + for (int i = q; i > 0; i--) { + putLong(longValue); + } } - } - q = r >>> 2; - r = r & 3; + q = r >>> 2; + r = r & 3; - if (q > 0) { - int intValue = value | value << 8 | value << 16 | value << 24; - putInt(intValue); - } + if (q > 0) { + int intValue = value | value << 8 | value << 16 | value << 24; + putInt(intValue); + } - q = r >> 1; - r = r & 1; + q = r >> 1; + r = r & 1; - if (q > 0) { - short shortValue = (short) (value | value << 8); - putShort(shortValue); - } + if (q > 0) { + short shortValue = (short) (value | value << 8); + putShort(shortValue); + } - if (r > 0) { - put(value); - } + if (r > 0) { + put(value); + } - return this; + return this; } /** @@ -2377,31 +2377,31 @@ public abstract class AbstractIoBuffer extends IoBuffer { public IoBuffer fill(int size) { autoExpand(size); int q = size >>> 3; - int r = size & 7; + int r = size & 7; - for (int i = q; i > 0; i--) { - putLong(0L); - } + for (int i = q; i > 0; i--) { + putLong(0L); + } - q = r >>> 2; - r = r & 3; + q = r >>> 2; + r = r & 3; - if (q > 0) { - putInt(0); - } + if (q > 0) { + putInt(0); + } - q = r >> 1; - r = r & 1; + q = r >> 1; + r = r & 1; - if (q > 0) { - putShort((short) 0); - } + if (q > 0) { + putShort((short) 0); + } - if (r > 0) { - put((byte) 0); - } + if (r > 0) { + put((byte) 0); + } - return this; + return this; } /** http://git-wip-us.apache.org/repos/asf/mina/blob/0a30a079/mina-core/src/test/java/org/apache/mina/core/buffer/IoBufferTest.java ---------------------------------------------------------------------- diff --git a/mina-core/src/test/java/org/apache/mina/core/buffer/IoBufferTest.java b/mina-core/src/test/java/org/apache/mina/core/buffer/IoBufferTest.java index 3a4e477..4d23a7e 100644 --- a/mina-core/src/test/java/org/apache/mina/core/buffer/IoBufferTest.java +++ b/mina-core/src/test/java/org/apache/mina/core/buffer/IoBufferTest.java @@ -1415,4 +1415,38 @@ public class IoBufferTest { assertEquals(0x0000000000008181L, buf.getUnsignedInt()); assertEquals(0x0000000000000080L, buf.getUnsignedInt()); } + + /** + * Test the getSlice method (even if we haven't flipped the buffer + */ + @Test + public void testGetSlice() { + IoBuffer buf = IoBuffer.allocate(36); + + for (byte i = 0; i < 36; i++) { + buf.put(i); + } + + IoBuffer res = buf.getSlice(1, 3); + + // The limit should be 3, the pos should be 0 and the bytes read + // should be 0x01, 0x02 and 0x03 + assertEquals(0, res.position()); + assertEquals(3, res.limit()); + assertEquals(0x01, res.get()); + assertEquals(0x02, res.get()); + assertEquals(0x03, res.get()); + + // Now test after a flip + buf.flip(); + + res = buf.getSlice(1, 3); + // The limit should be 3, the pos should be 0 and the bytes read + // should be 0x01, 0x02 and 0x03 + assertEquals(0, res.position()); + assertEquals(3, res.limit()); + assertEquals(0x01, res.get()); + assertEquals(0x02, res.get()); + assertEquals(0x03, res.get()); + } }
