* Merged Frederic's 'aggregate' branch

* Refactored Frederic's test cases
* Code clean up
This commit is contained in:
Trustin Lee 2009-10-13 07:54:18 +00:00
parent 1ed1d63dd6
commit ca4c32df68
5 changed files with 532 additions and 117 deletions

View File

@ -41,9 +41,9 @@ import java.util.List;
*/
public class CompositeChannelBuffer extends AbstractChannelBuffer {
private final ChannelBuffer[] slices;
private final ByteOrder order;
private final int[] indices;
private ChannelBuffer[] slices;
private int[] indices;
private int lastSliceId;
public CompositeChannelBuffer(ChannelBuffer... buffers) {
@ -51,33 +51,124 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
throw new IllegalArgumentException("buffers should not be empty.");
}
// Get the list of the component, while guessing the byte order.
ByteOrder expectedEndianness = null;
for (ChannelBuffer buffer : buffers) {
if (buffer.capacity() != 0) {
final List<ChannelBuffer> bufferList = new ArrayList<ChannelBuffer>(buffers.length);
for (ChannelBuffer buffer: buffers) {
if (buffer.readableBytes() > 0) {
expectedEndianness = buffer.order();
if (buffer instanceof CompositeChannelBuffer) {
// Expand nested composition.
CompositeChannelBuffer child = (CompositeChannelBuffer) buffer;
bufferList.addAll(
child.getBufferList(child.readerIndex(), child.readableBytes()));
} else {
// An ordinary buffer (non-composite)
bufferList.add(buffer.slice());
}
} else if (buffer.capacity() != 0) {
expectedEndianness = buffer.order();
}
}
if (expectedEndianness == null) {
throw new IllegalArgumentException("buffers have only empty buffers.");
throw new IllegalArgumentException(
"buffers have only empty buffers.");
}
order = expectedEndianness;
slices = new ChannelBuffer[buffers.length];
for (int i = 0; i < buffers.length; i ++) {
if (buffers[i].capacity() != 0 && buffers[i].order() != expectedEndianness) {
throw new IllegalArgumentException(
"All buffers must have the same endianness.");
}
slices[i] = buffers[i].slice();
}
indices = new int[buffers.length + 1];
for (int i = 1; i <= buffers.length; i ++) {
indices[i] = indices[i - 1] + slices[i - 1].capacity();
}
writerIndex(capacity());
setFromList(bufferList);
}
/**
* Returns the list of valid buffers from index and length, slicing contents
*/
private List<ChannelBuffer> getBufferList(int index, int length) {
int localReaderIndex = index;
int localWriterIndex = this.writerIndex();
int sliceId = sliceId(localReaderIndex);
// some slices from sliceId must be kept
int maxlength = localWriterIndex - localReaderIndex;
if (maxlength < length) {
// check then if maxlength is compatible with the capacity
maxlength = capacity() - localReaderIndex;
if (maxlength < length) {
// too big
throw new IllegalArgumentException(
"Length is bigger than available.");
}
// use capacity (discardReadBytes method)
}
List<ChannelBuffer> bufferList = new ArrayList<ChannelBuffer>(slices.length);
// first one is not complete
// each slice will be duplicated before assign to the list (maintain original indexes)
ChannelBuffer buf = slices[sliceId].duplicate();
buf.readerIndex(localReaderIndex - indices[sliceId]);
buf.writerIndex(slices[sliceId].writerIndex());
// as writerIndex can be less than capacity, check too for the end
int newlength = length;
while (newlength > 0) {
int leftInBuffer = buf.capacity() - buf.readerIndex();
if (newlength <= leftInBuffer) {
// final buffer
buf.writerIndex(buf.readerIndex() + newlength);
bufferList.add(buf);
newlength = 0;
break;
} else {
// not final buffer
bufferList.add(buf);
newlength -= leftInBuffer;
sliceId ++;
buf = slices[sliceId].duplicate();
buf.readerIndex(0);
buf.writerIndex(slices[sliceId].writerIndex());
// length is > 0
}
}
return bufferList;
}
/**
* Setup this ChannelBuffer from the list and the Endianness
* @param listBuf
* @param expectedEndianness
*/
private void setFromList(List<ChannelBuffer> listBuf) {
// Reset the cached slice position.
lastSliceId = 0;
int number = listBuf.size();
if (number == 0) {
slices = new ChannelBuffer[1];
// to prevent remove too early
slices[0] = ChannelBuffers.EMPTY_BUFFER.slice();
indices = new int[2];
indices[1] = indices[0] + slices[0].capacity();
readerIndex(0);
writerIndex(capacity());
return;
}
slices = new ChannelBuffer[number];
int i = 0;
for (ChannelBuffer buffer: listBuf) {
if (buffer.order() != order()) {
throw new IllegalArgumentException(
"All buffers must have the same endianness.");
}
slices[i] = buffer;
i ++;
}
indices = new int[number + 1];
indices[0] = 0;
for (i = 1; i <= number; i ++) {
indices[i] = indices[i - 1] + slices[i - 1].capacity();
}
setIndex(0, capacity());
}
private CompositeChannelBuffer(CompositeChannelBuffer buffer) {
order = buffer.order;
slices = buffer.slices.clone();
@ -463,16 +554,16 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
if (index == 0) {
if (length == 0) {
return ChannelBuffers.EMPTY_BUFFER;
} else {
return new TruncatedChannelBuffer(this, length);
}
} else if (index < 0 || index > capacity() - length) {
throw new IndexOutOfBoundsException();
} else if (length == 0) {
return ChannelBuffers.EMPTY_BUFFER;
} else {
return new SlicedChannelBuffer(this, index, length);
}
List<ChannelBuffer> listBuffer = getBufferList(index, length);
ChannelBuffer[] buffers = listBuffer.toArray(new ChannelBuffer[listBuffer.size()]);
return new CompositeChannelBuffer(buffers);
}
public ByteBuffer toByteBuffer(int index, int length) {
@ -567,4 +658,62 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
throw new IndexOutOfBoundsException();
}
@Override
public void discardReadBytes() {
// Only the bytes between readerIndex and writerIndex will be kept.
// New readerIndex and writerIndex will become 0 and
// (previous writerIndex - previous readerIndex) respectively.
final int localReaderIndex = this.readerIndex();
if (localReaderIndex == 0) {
return;
}
int localWriterIndex = this.writerIndex();
final int bytesToMove = capacity() - localReaderIndex;
List<ChannelBuffer> list = getBufferList(localReaderIndex, bytesToMove);
// Truncate the discardable bytes of the first buffer.
list.set(0, list.get(0).slice());
// Add a new buffer so that the capacity of this composite buffer does
// not decrease due to the discarded components.
list.add(ChannelBuffers.buffer(order(), localReaderIndex));
// Reset the index markers to get the index marker values.
int localMarkedReaderIndex = localReaderIndex;
try {
resetReaderIndex();
localMarkedReaderIndex = this.readerIndex();
} catch (IndexOutOfBoundsException e) {
// ignore
}
int localMarkedWriterIndex = localWriterIndex;
try {
resetWriterIndex();
localMarkedWriterIndex = this.writerIndex();
} catch (IndexOutOfBoundsException e) {
// ignore
}
setFromList(list);
// reset marked Indexes
localMarkedReaderIndex = Math.max(localMarkedReaderIndex - localReaderIndex, 0);
localMarkedWriterIndex = Math.max(localMarkedWriterIndex - localReaderIndex, 0);
setIndex(localMarkedReaderIndex, localMarkedWriterIndex);
markReaderIndex();
markWriterIndex();
// reset real indexes
localWriterIndex = Math.max(localWriterIndex - localReaderIndex, 0);
setIndex(0, localWriterIndex);
}
@Override
public String toString() {
String result = super.toString();
result = result.substring(0, result.length() - 1);
return result + ", slices=" + slices.length + ")";
}
}

View File

@ -51,6 +51,11 @@ public abstract class AbstractChannelBufferTest {
protected abstract ChannelBuffer newBuffer(int capacity);
protected abstract ChannelBuffer[] components();
protected boolean discardReadBytesDoesNotMoveWritableBytes() {
return true;
}
@Before
public void init() {
buffer = newBuffer(CAPACITY);
@ -1329,8 +1334,13 @@ public abstract class AbstractChannelBufferTest {
assertEquals(0, buffer.readerIndex());
assertEquals(CAPACITY / 2 - 1, buffer.writerIndex());
assertEquals(copy.slice(1, CAPACITY / 2 - 1), buffer.slice(0, CAPACITY / 2 - 1));
// If writable bytes were copied, the test should fail to avoid unnecessary memory bandwidth consumption.
assertFalse(copy.slice(CAPACITY / 2, CAPACITY / 2).equals(buffer.slice(CAPACITY / 2 - 1, CAPACITY / 2)));
if (discardReadBytesDoesNotMoveWritableBytes()) {
// If writable bytes were copied, the test should fail to avoid unnecessary memory bandwidth consumption.
assertFalse(copy.slice(CAPACITY / 2, CAPACITY / 2).equals(buffer.slice(CAPACITY / 2 - 1, CAPACITY / 2)));
} else {
assertEquals(copy.slice(CAPACITY / 2, CAPACITY / 2), buffer.slice(CAPACITY / 2 - 1, CAPACITY / 2));
}
// Marks also should be relocated.
buffer.resetReaderIndex();
@ -1339,6 +1349,28 @@ public abstract class AbstractChannelBufferTest {
assertEquals(CAPACITY / 3 - 1, buffer.writerIndex());
}
/**
* The similar test case with {@link #testDiscardReadBytes()} but this one
* discards a large chunk at once.
*/
@Test
public void testDiscardReadBytes2() {
buffer.writerIndex(0);
for (int i = 0; i < buffer.capacity(); i ++) {
buffer.writeByte((byte) i);
}
ChannelBuffer copy = copiedBuffer(buffer);
// Discard the first (CAPACITY / 2 - 1) bytes.
buffer.setIndex(CAPACITY / 2 - 1, CAPACITY - 1);
buffer.discardReadBytes();
assertEquals(0, buffer.readerIndex());
assertEquals(CAPACITY / 2, buffer.writerIndex());
for (int i = 0; i < CAPACITY / 2; i ++) {
assertEquals(copy.slice(CAPACITY / 2 - 1 + i, CAPACITY / 2 - i), buffer.slice(i, CAPACITY / 2 - i));
}
}
@Test
public void testStreamTransfer1() throws Exception {
byte[] expected = new byte[buffer.capacity()];

View File

@ -0,0 +1,321 @@
package org.jboss.netty.buffer;
import static org.jboss.netty.buffer.ChannelBuffers.*;
import static org.junit.Assert.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public abstract class AbstractCompositeChannelBufferTest extends
AbstractChannelBufferTest {
private final ByteOrder order;
protected AbstractCompositeChannelBufferTest(ByteOrder order) {
if (order == null) {
throw new NullPointerException("order");
}
this.order = order;
}
private List<ChannelBuffer> buffers;
private ChannelBuffer buffer;
@Override
protected ChannelBuffer newBuffer(int length) {
buffers = new ArrayList<ChannelBuffer>();
for (int i = 0; i < length; i += 10) {
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(order, new byte[1]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(order, new byte[2]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(order, new byte[3]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(order, new byte[4]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(order, new byte[5]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(order, new byte[6]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(order, new byte[7]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(order, new byte[8]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(order, new byte[9]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
}
buffer = ChannelBuffers.wrappedBuffer(buffers.toArray(new ChannelBuffer[buffers.size()]));
buffer.writerIndex(length);
buffer = ChannelBuffers.wrappedBuffer(buffer);
assertEquals(length, buffer.capacity());
assertEquals(length, buffer.readableBytes());
assertFalse(buffer.writable());
buffer.writerIndex(0);
return buffer;
}
@Override
protected ChannelBuffer[] components() {
return buffers.toArray(new ChannelBuffer[buffers.size()]);
}
// Composite buffer does not waste bandwidth on discardReadBytes, but
// the test will fail in strict mode.
@Override
protected boolean discardReadBytesDoesNotMoveWritableBytes() {
return false;
}
@Test
public void testDiscardReadBytes3() {
ChannelBuffer a, b;
a = wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
b = wrappedBuffer(
wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 5),
wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 5, 5));
a.skipBytes(6);
a.markReaderIndex();
b.skipBytes(6);
b.markReaderIndex();
assertEquals(a.readerIndex(), b.readerIndex());
a.readerIndex(a.readerIndex()-1);
b.readerIndex(b.readerIndex()-1);
assertEquals(a.readerIndex(), b.readerIndex());
a.writerIndex(a.writerIndex()-1);
a.markWriterIndex();
b.writerIndex(b.writerIndex()-1);
b.markWriterIndex();
assertEquals(a.writerIndex(), b.writerIndex());
a.writerIndex(a.writerIndex()+1);
b.writerIndex(b.writerIndex()+1);
assertEquals(a.writerIndex(), b.writerIndex());
assertTrue(ChannelBuffers.equals(a, b));
// now discard
a.discardReadBytes();
b.discardReadBytes();
assertEquals(a.readerIndex(), b.readerIndex());
assertEquals(a.writerIndex(), b.writerIndex());
assertTrue(ChannelBuffers.equals(a, b));
a.resetReaderIndex();
b.resetReaderIndex();
assertEquals(a.readerIndex(), b.readerIndex());
a.resetWriterIndex();
b.resetWriterIndex();
assertEquals(a.writerIndex(), b.writerIndex());
assertTrue(ChannelBuffers.equals(a, b));
}
@Test
public void testCompositeWrappedBuffer() {
ChannelBuffer header = dynamicBuffer(order, 12);
ChannelBuffer payload = dynamicBuffer(order, 512);
header.writeBytes(new byte[12]);
payload.writeBytes(new byte[512]);
ChannelBuffer buffer = wrappedBuffer(header, payload);
assertTrue(header.readableBytes() == 12);
assertTrue(payload.readableBytes() == 512);
assertEquals(12 + 512, buffer.readableBytes());
assertEquals(12 + 512, buffer.toByteBuffer(0, 12 + 512).remaining());
}
@Test
public void testSeveralBuffersEquals() {
ChannelBuffer a, b;
//XXX Same tests with several buffers in wrappedCheckedBuffer
// Different length.
a = wrappedBuffer(order, new byte[] { 1 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 1 }),
wrappedBuffer(order, new byte[] { 2 }));
assertFalse(ChannelBuffers.equals(a, b));
// Same content, same firstIndex, short length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 1 }),
wrappedBuffer(order, new byte[] { 2 }),
wrappedBuffer(order, new byte[] { 3 }));
assertTrue(ChannelBuffers.equals(a, b));
// Same content, different firstIndex, short length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4 }, 1, 2),
wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4 }, 3, 1));
assertTrue(ChannelBuffers.equals(a, b));
// Different content, same firstIndex, short length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2 }),
wrappedBuffer(order, new byte[] { 4 }));
assertFalse(ChannelBuffers.equals(a, b));
// Different content, different firstIndex, short length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 0, 1, 2, 4, 5 }, 1, 2),
wrappedBuffer(order, new byte[] { 0, 1, 2, 4, 5 }, 3, 1));
assertFalse(ChannelBuffers.equals(a, b));
// Same content, same firstIndex, long length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2, 3 }),
wrappedBuffer(order, new byte[] { 4, 5, 6 }),
wrappedBuffer(order, new byte[] { 7, 8, 9, 10 }));
assertTrue(ChannelBuffers.equals(a, b));
// Same content, different firstIndex, long length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 1, 5),
wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 6, 5));
assertTrue(ChannelBuffers.equals(a, b));
// Different content, same firstIndex, long length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 6 }),
wrappedBuffer(order, new byte[] { 7, 8, 5, 9, 10 }));
assertFalse(ChannelBuffers.equals(a, b));
// Different content, different firstIndex, long length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 1, 5),
wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 6, 5));
assertFalse(ChannelBuffers.equals(a, b));
}
@Test
public void testWrappedBuffer() {
assertEquals(16, wrappedBuffer(wrappedBuffer(ByteBuffer.allocateDirect(16))).capacity());
assertEquals(
wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2, 3 })),
wrappedBuffer(wrappedBuffer(order, new byte[][] { new byte[] { 1, 2, 3 } })));
assertEquals(
wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2, 3 })),
wrappedBuffer(wrappedBuffer(order,
new byte[] { 1 },
new byte[] { 2 },
new byte[] { 3 })));
assertEquals(
wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2, 3 })),
wrappedBuffer(new ChannelBuffer[] {
wrappedBuffer(order, new byte[] { 1, 2, 3 })
}));
assertEquals(
wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2, 3 })),
wrappedBuffer(
wrappedBuffer(order, new byte[] { 1 }),
wrappedBuffer(order, new byte[] { 2 }),
wrappedBuffer(order, new byte[] { 3 })));
assertEquals(
wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2, 3 })),
wrappedBuffer(wrappedBuffer(new ByteBuffer[] {
ByteBuffer.wrap(new byte[] { 1, 2, 3 })
})));
assertEquals(
wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2, 3 })),
wrappedBuffer(wrappedBuffer(
ByteBuffer.wrap(new byte[] { 1 }),
ByteBuffer.wrap(new byte[] { 2 }),
ByteBuffer.wrap(new byte[] { 3 }))));
}
@Test
public void testWrittenBuffersEquals() {
//XXX Same tests than testEquals with written AggregateChannelBuffers
ChannelBuffer a, b;
// Different length.
a = wrappedBuffer(order, new byte[] { 1 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 1 }, new byte[1]));
// to enable writeBytes
b.writerIndex(b.writerIndex()-1);
b.writeBytes(
wrappedBuffer(order, new byte[] { 2 }));
assertFalse(ChannelBuffers.equals(a, b));
// Same content, same firstIndex, short length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 1 }, new byte[2]));
// to enable writeBytes
b.writerIndex(b.writerIndex()-2);
b.writeBytes(
wrappedBuffer(order, new byte[] { 2 }));
b.writeBytes(wrappedBuffer(order, new byte[] { 3 }));
assertTrue(ChannelBuffers.equals(a, b));
// Same content, different firstIndex, short length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4 }, 1, 3));
// to enable writeBytes
b.writerIndex(b.writerIndex()-1);
b.writeBytes(
wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4 }, 3, 1));
assertTrue(ChannelBuffers.equals(a, b));
// Different content, same firstIndex, short length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2 }, new byte[1]));
// to enable writeBytes
b.writerIndex(b.writerIndex()-1);
b.writeBytes(
wrappedBuffer(order, new byte[] { 4 }));
assertFalse(ChannelBuffers.equals(a, b));
// Different content, different firstIndex, short length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 0, 1, 2, 4, 5 }, 1, 3));
// to enable writeBytes
b.writerIndex(b.writerIndex()-1);
b.writeBytes(
wrappedBuffer(order, new byte[] { 0, 1, 2, 4, 5 }, 3, 1));
assertFalse(ChannelBuffers.equals(a, b));
// Same content, same firstIndex, long length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2, 3 }, new byte[7]));
// to enable writeBytes
b.writerIndex(b.writerIndex()-7);
b.writeBytes(
wrappedBuffer(order, new byte[] { 4, 5, 6 }));
b.writeBytes(
wrappedBuffer(order, new byte[] { 7, 8, 9, 10 }));
assertTrue(ChannelBuffers.equals(a, b));
// Same content, different firstIndex, long length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 1, 10));
// to enable writeBytes
b.writerIndex(b.writerIndex()-5);
b.writeBytes(
wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 6, 5));
assertTrue(ChannelBuffers.equals(a, b));
// Different content, same firstIndex, long length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 6 }, new byte[5]));
// to enable writeBytes
b.writerIndex(b.writerIndex()-5);
b.writeBytes(
wrappedBuffer(order, new byte[] { 7, 8, 5, 9, 10 }));
assertFalse(ChannelBuffers.equals(a, b));
// Different content, different firstIndex, long length.
a = wrappedBuffer(order, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
b = wrappedBuffer(wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 1, 10));
// to enable writeBytes
b.writerIndex(b.writerIndex()-5);
b.writeBytes(
wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 6, 5));
assertFalse(ChannelBuffers.equals(a, b));
}
}

View File

@ -15,10 +15,6 @@
*/
package org.jboss.netty.buffer;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
/**
*
@ -27,47 +23,8 @@ import java.util.List;
*
* @version $Rev$, $Date$
*/
public class BigEndianCompositeChannelBufferTest extends AbstractChannelBufferTest {
private List<ChannelBuffer> buffers;
private ChannelBuffer buffer;
@Override
protected ChannelBuffer newBuffer(int length) {
buffers = new ArrayList<ChannelBuffer>();
for (int i = 0; i < length; i += 10) {
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(new byte[1]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(new byte[2]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(new byte[3]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(new byte[4]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(new byte[5]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(new byte[6]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(new byte[7]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(new byte[8]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(new byte[9]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
}
buffer = ChannelBuffers.wrappedBuffer(buffers.toArray(new ChannelBuffer[buffers.size()]));
buffer.writerIndex(length);
buffer = ChannelBuffers.wrappedBuffer(buffer);
assertEquals(length, buffer.capacity());
assertEquals(length, buffer.readableBytes());
assertFalse(buffer.writable());
buffer.writerIndex(0);
return buffer;
}
@Override
protected ChannelBuffer[] components() {
return buffers.toArray(new ChannelBuffer[buffers.size()]);
public class BigEndianCompositeChannelBufferTest extends AbstractCompositeChannelBufferTest {
public BigEndianCompositeChannelBufferTest() {
super(ChannelBuffers.BIG_ENDIAN);
}
}

View File

@ -15,10 +15,6 @@
*/
package org.jboss.netty.buffer;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
/**
*
@ -27,48 +23,8 @@ import java.util.List;
*
* @version $Rev$, $Date$
*/
public class LittleEndianCompositeChannelBufferTest extends AbstractChannelBufferTest {
private List<ChannelBuffer> buffers;
private ChannelBuffer buffer;
@Override
protected ChannelBuffer newBuffer(int length) {
buffers = new ArrayList<ChannelBuffer>();
for (int i = 0; i < length; i += 10) {
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(ChannelBuffers.LITTLE_ENDIAN, new byte[1]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(ChannelBuffers.LITTLE_ENDIAN, new byte[2]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(ChannelBuffers.LITTLE_ENDIAN, new byte[3]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(ChannelBuffers.LITTLE_ENDIAN, new byte[4]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(ChannelBuffers.LITTLE_ENDIAN, new byte[5]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(ChannelBuffers.LITTLE_ENDIAN, new byte[6]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(ChannelBuffers.LITTLE_ENDIAN, new byte[7]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(ChannelBuffers.LITTLE_ENDIAN, new byte[8]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
buffers.add(ChannelBuffers.wrappedBuffer(ChannelBuffers.LITTLE_ENDIAN, new byte[9]));
buffers.add(ChannelBuffers.EMPTY_BUFFER);
}
buffer = ChannelBuffers.wrappedBuffer(buffers.toArray(new ChannelBuffer[buffers.size()]));
buffer.writerIndex(length);
buffer = ChannelBuffers.wrappedBuffer(buffer);
assertEquals(length, buffer.capacity());
assertEquals(length, buffer.readableBytes());
assertFalse(buffer.writable());
buffer.writerIndex(0);
return buffer;
}
@Override
protected ChannelBuffer[] components() {
return buffers.toArray(new ChannelBuffer[buffers.size()]);
public class LittleEndianCompositeChannelBufferTest extends AbstractCompositeChannelBufferTest {
public LittleEndianCompositeChannelBufferTest() {
super(ChannelBuffers.LITTLE_ENDIAN);
}
}