Java Code Examples for org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf#setIndex()

The following examples show how to use org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf#setIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractByteBufTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testRandomHeapBufferTransfer1() {
    byte[] valueContent = new byte[BLOCK_SIZE];
    ByteBuf value = wrappedBuffer(valueContent);
    for (int i = 0; i < buffer.capacity() - BLOCK_SIZE + 1; i += BLOCK_SIZE) {
        random.nextBytes(valueContent);
        value.setIndex(0, BLOCK_SIZE);
        buffer.setBytes(i, value);
        assertEquals(BLOCK_SIZE, value.readerIndex());
        assertEquals(BLOCK_SIZE, value.writerIndex());
    }

    random.setSeed(seed);
    byte[] expectedValueContent = new byte[BLOCK_SIZE];
    ByteBuf expectedValue = wrappedBuffer(expectedValueContent);
    for (int i = 0; i < buffer.capacity() - BLOCK_SIZE + 1; i += BLOCK_SIZE) {
        random.nextBytes(expectedValueContent);
        value.clear();
        buffer.getBytes(i, value);
        assertEquals(0, value.readerIndex());
        assertEquals(BLOCK_SIZE, value.writerIndex());
        for (int j = 0; j < BLOCK_SIZE; j ++) {
            assertEquals(expectedValue.getByte(j), value.getByte(j));
        }
    }
}
 
Example 2
Source File: AbstractByteBufTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test(expected = IndexOutOfBoundsException.class)
public void testReaderIndexLargerThanWriterIndex() {
    String content1 = "hello";
    String content2 = "world";
    int length = content1.length() + content2.length();
    ByteBuf buffer = newBuffer(length);
    buffer.setIndex(0, 0);
    buffer.writeCharSequence(content1, CharsetUtil.US_ASCII);
    buffer.markWriterIndex();
    buffer.skipBytes(content1.length());
    buffer.writeCharSequence(content2, CharsetUtil.US_ASCII);
    buffer.skipBytes(content2.length());
    assertTrue(buffer.readerIndex() <= buffer.writerIndex());

    try {
        buffer.resetWriterIndex();
    } finally {
        buffer.release();
    }
}
 
Example 3
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testRandomHeapBufferTransfer1() {
    byte[] valueContent = new byte[BLOCK_SIZE];
    ByteBuf value = wrappedBuffer(valueContent);
    for (int i = 0; i < buffer.capacity() - BLOCK_SIZE + 1; i += BLOCK_SIZE) {
        random.nextBytes(valueContent);
        value.setIndex(0, BLOCK_SIZE);
        buffer.setBytes(i, value);
        assertEquals(BLOCK_SIZE, value.readerIndex());
        assertEquals(BLOCK_SIZE, value.writerIndex());
    }

    random.setSeed(seed);
    byte[] expectedValueContent = new byte[BLOCK_SIZE];
    ByteBuf expectedValue = wrappedBuffer(expectedValueContent);
    for (int i = 0; i < buffer.capacity() - BLOCK_SIZE + 1; i += BLOCK_SIZE) {
        random.nextBytes(expectedValueContent);
        value.clear();
        buffer.getBytes(i, value);
        assertEquals(0, value.readerIndex());
        assertEquals(BLOCK_SIZE, value.writerIndex());
        for (int j = 0; j < BLOCK_SIZE; j ++) {
            assertEquals(expectedValue.getByte(j), value.getByte(j));
        }
    }
}
 
Example 4
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test(expected = IndexOutOfBoundsException.class)
public void testReaderIndexLargerThanWriterIndex() {
    String content1 = "hello";
    String content2 = "world";
    int length = content1.length() + content2.length();
    ByteBuf buffer = newBuffer(length);
    buffer.setIndex(0, 0);
    buffer.writeCharSequence(content1, CharsetUtil.US_ASCII);
    buffer.markWriterIndex();
    buffer.skipBytes(content1.length());
    buffer.writeCharSequence(content2, CharsetUtil.US_ASCII);
    buffer.skipBytes(content2.length());
    assertTrue(buffer.readerIndex() <= buffer.writerIndex());

    try {
        buffer.resetWriterIndex();
    } finally {
        buffer.release();
    }
}
 
Example 5
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testRandomHeapBufferTransfer1() {
    byte[] valueContent = new byte[BLOCK_SIZE];
    ByteBuf value = wrappedBuffer(valueContent);
    for (int i = 0; i < buffer.capacity() - BLOCK_SIZE + 1; i += BLOCK_SIZE) {
        random.nextBytes(valueContent);
        value.setIndex(0, BLOCK_SIZE);
        buffer.setBytes(i, value);
        assertEquals(BLOCK_SIZE, value.readerIndex());
        assertEquals(BLOCK_SIZE, value.writerIndex());
    }

    random.setSeed(seed);
    byte[] expectedValueContent = new byte[BLOCK_SIZE];
    ByteBuf expectedValue = wrappedBuffer(expectedValueContent);
    for (int i = 0; i < buffer.capacity() - BLOCK_SIZE + 1; i += BLOCK_SIZE) {
        random.nextBytes(expectedValueContent);
        value.clear();
        buffer.getBytes(i, value);
        assertEquals(0, value.readerIndex());
        assertEquals(BLOCK_SIZE, value.writerIndex());
        for (int j = 0; j < BLOCK_SIZE; j ++) {
            assertEquals(expectedValue.getByte(j), value.getByte(j));
        }
    }
}
 
Example 6
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test(expected = IndexOutOfBoundsException.class)
public void testReaderIndexLargerThanWriterIndex() {
    String content1 = "hello";
    String content2 = "world";
    int length = content1.length() + content2.length();
    ByteBuf buffer = newBuffer(length);
    buffer.setIndex(0, 0);
    buffer.writeCharSequence(content1, CharsetUtil.US_ASCII);
    buffer.markWriterIndex();
    buffer.skipBytes(content1.length());
    buffer.writeCharSequence(content2, CharsetUtil.US_ASCII);
    buffer.skipBytes(content2.length());
    assertTrue(buffer.readerIndex() <= buffer.writerIndex());

    try {
        buffer.resetWriterIndex();
    } finally {
        buffer.release();
    }
}