AbstractChannelBuffer implements ChannelBuffer { public ChannelBuffer copy() { return copy(readerIndex, readableBytes()); }  int readerIndex(); void readerIndex(int readerIndex); int writerIndex(); void writerIndex(int writerIndex); void setIndex(int readerIndex, int writerIndex); void clear(); boolean readable(); boolean writable(); int readableBytes(); int writableBytes(); void markReaderIndex(); void resetReaderIndex(); void markWriterIndex(); void resetWriterIndex(); void discardReadBytes(); void ensureWritableBytes(int writableBytes); void getBytes(int index, byte[] dst); void getBytes(int index, ChannelBuffer dst); void getBytes(int index, ChannelBuffer dst, int length); void setBytes(int index, byte[] src); void setBytes(int index, ChannelBuffer src); void setBytes(int index, ChannelBuffer src, int length); byte readByte(); ChannelBuffer readBytes(int length); void readBytes(byte[] dst, int dstIndex, int length); void readBytes(byte[] dst); void readBytes(ChannelBuffer dst); void readBytes(ChannelBuffer dst, int length); void readBytes(ChannelBuffer dst, int dstIndex, int length); void readBytes(ByteBuffer dst); void readBytes(OutputStream out, int length); void skipBytes(int length); void writeByte(int value); void writeBytes(byte[] src, int srcIndex, int length); void writeBytes(byte[] src); void writeBytes(ChannelBuffer src); void writeBytes(ChannelBuffer src, int length); void writeBytes(ChannelBuffer src, int srcIndex, int length); void writeBytes(ByteBuffer src); int writeBytes(InputStream in, int length); ChannelBuffer copy(); ByteBuffer toByteBuffer(); @Override boolean equals(Object o); int compareTo(ChannelBuffer that); @Override String toString();  }
@Test(expected=IndexOutOfBoundsException.class) public void copyBoundaryCheck1() { buffer.copy(-1, 0); }
@Test(expected=IndexOutOfBoundsException.class) public void copyBoundaryCheck2() { buffer.copy(0, buffer.capacity() + 1); }
@Test(expected=IndexOutOfBoundsException.class) public void copyBoundaryCheck3() { buffer.copy(buffer.capacity() + 1, 0); }
@Test(expected=IndexOutOfBoundsException.class) public void copyBoundaryCheck4() { buffer.copy(buffer.capacity(), 1); }
@Test public void testCopy() { 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); ChannelBuffer copy = buffer.copy(); assertEquals(0, copy.readerIndex()); assertEquals(buffer.readableBytes(), copy.writerIndex()); assertEquals(buffer.readableBytes(), copy.capacity()); for (int i = 0; i < copy.capacity(); i ++) { assertEquals(buffer.getByte(i + readerIndex), copy.getByte(i)); } 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(expected = IndexOutOfBoundsException.class) public void copyBoundaryCheck1() { buffer.copy(-1, 0); }
@Test(expected = IndexOutOfBoundsException.class) public void copyBoundaryCheck2() { buffer.copy(0, buffer.capacity() + 1); }
@Test(expected = IndexOutOfBoundsException.class) public void copyBoundaryCheck3() { buffer.copy(buffer.capacity() + 1, 0); }
@Test(expected = IndexOutOfBoundsException.class) public void copyBoundaryCheck4() { buffer.copy(buffer.capacity(), 1); }
@Test public void testCopy() { 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); ChannelBuffer copy = buffer.copy(); assertEquals(0, copy.readerIndex()); assertEquals(buffer.readableBytes(), copy.writerIndex()); assertEquals(buffer.readableBytes(), copy.capacity()); for (int i = 0; i < copy.capacity(); i++) { assertEquals(buffer.getByte(i + readerIndex), copy.getByte(i)); } 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)); }