Fixed issue: NETTY-32 (ChannelBuffer.duplicate() sometimes doesn't copy all properties)

This commit is contained in:
Trustin Lee 2008-08-26 11:08:55 +00:00
parent a57e21aee7
commit 3b69dd7ddb
4 changed files with 41 additions and 6 deletions

View File

@ -412,7 +412,9 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
}
public ChannelBuffer duplicate() {
return new CompositeChannelBuffer(this);
ChannelBuffer duplicate = new CompositeChannelBuffer(this);
duplicate.setIndex(readerIndex(), writerIndex());
return duplicate;
}
public ChannelBuffer copy(int index, int length) {

View File

@ -99,7 +99,9 @@ public class SlicedChannelBuffer extends AbstractChannelBuffer implements Wrappe
}
public ChannelBuffer duplicate() {
return new SlicedChannelBuffer(buffer, adjustment, length);
ChannelBuffer duplicate = new SlicedChannelBuffer(buffer, adjustment, length);
duplicate.setIndex(readerIndex(), writerIndex());
return duplicate;
}
public ChannelBuffer copy(int index, int length) {

View File

@ -93,7 +93,9 @@ public class TruncatedChannelBuffer extends AbstractChannelBuffer implements Wra
}
public ChannelBuffer duplicate() {
return new TruncatedChannelBuffer(buffer, length);
ChannelBuffer duplicate = new TruncatedChannelBuffer(buffer, length);
duplicate.setIndex(readerIndex(), writerIndex());
return duplicate;
}
public ChannelBuffer copy(int index, int length) {

View File

@ -1238,22 +1238,51 @@ public abstract class AbstractChannelBufferTest {
final int writerIndex = CAPACITY * 2 / 3;
buffer.setIndex(readerIndex, writerIndex);
// Make sure all properties are cpoied.
// Make sure all properties are copied.
ChannelBuffer copy = buffer.copy();
assertEquals(0, copy.readerIndex());
assertEquals(buffer.readableBytes(), copy.writerIndex());
assertEquals(copy.capacity(), buffer.readableBytes());
assertEquals(buffer.readableBytes(), copy.capacity());
assertSame(buffer.order(), copy.order());
for (int i = 0; i < copy.capacity(); i ++) {
assertEquals(buffer.getByte(i + readerIndex), copy.getByte(i));
}
// Make sure the buffer contents are independent from each other.
// Make sure the buffer content is independent from each other.
buffer.setByte(readerIndex, (byte) (buffer.getByte(readerIndex) + 1));
assertTrue(buffer.getByte(readerIndex) != copy.getByte(0));
copy.setByte(1, (byte) (copy.getByte(1) + 1));
assertTrue(buffer.getByte(readerIndex + 1) != copy.getByte(1));
}
@Test
public void testDuplicate() {
for (int i = 0; i < buffer.capacity(); i ++) {
byte value = (byte) random.nextInt();
buffer.setByte(i, value);
}
final int readerIndex = CAPACITY / 3;
final int writerIndex = CAPACITY * 2 / 3;
buffer.setIndex(readerIndex, writerIndex);
// Make sure all properties are copied.
ChannelBuffer duplicate = buffer.duplicate();
assertEquals(buffer.readerIndex(), duplicate.readerIndex());
assertEquals(buffer.writerIndex(), duplicate.writerIndex());
assertEquals(buffer.capacity(), duplicate.capacity());
assertSame(buffer.order(), duplicate.order());
for (int i = 0; i < duplicate.capacity(); i ++) {
assertEquals(buffer.getByte(i), duplicate.getByte(i));
}
// Make sure the buffer content is shared.
buffer.setByte(readerIndex, (byte) (buffer.getByte(readerIndex) + 1));
assertTrue(buffer.getByte(readerIndex) == duplicate.getByte(readerIndex));
duplicate.setByte(1, (byte) (duplicate.getByte(1) + 1));
assertTrue(buffer.getByte(1) == duplicate.getByte(1));
}
@Test
public void testEquals() {
assertFalse(buffer.equals(null));