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

The following examples show how to use org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf#slice() . 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 with Apache License 2.0 6 votes vote down vote up
private void testSliceContents(boolean retainedSlice) {
    ByteBuf buf = newBuffer(8).resetWriterIndex();
    ByteBuf expected = newBuffer(3).resetWriterIndex();
    buf.writeBytes(new byte[] {1, 2, 3, 4, 5, 6, 7, 8});
    expected.writeBytes(new byte[] {4, 5, 6});
    ByteBuf slice = retainedSlice ? buf.retainedSlice(buf.readerIndex() + 3, 3)
                                  : buf.slice(buf.readerIndex() + 3, 3);
    try {
        assertEquals(0, slice.compareTo(expected));
        assertEquals(0, slice.compareTo(slice.duplicate()));
        ByteBuf b = slice.retainedDuplicate();
        assertEquals(0, slice.compareTo(b));
        b.release();
        assertEquals(0, slice.compareTo(slice.slice(0, slice.capacity())));
    } finally {
        if (retainedSlice) {
            slice.release();
        }
        buf.release();
        expected.release();
    }
}
 
Example 2
Source File: AbstractByteBufTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private void testSliceOutOfBounds(boolean initRetainedSlice, boolean finalRetainedSlice, boolean indexOutOfBounds) {
    ByteBuf buf = newBuffer(8);
    ByteBuf slice = initRetainedSlice ? buf.retainedSlice(buf.readerIndex() + 1, 2)
                                      : buf.slice(buf.readerIndex() + 1, 2);
    try {
        assertEquals(2, slice.capacity());
        assertEquals(2, slice.maxCapacity());
        final int index = indexOutOfBounds ? 3 : 0;
        final int length = indexOutOfBounds ? 0 : 3;
        if (finalRetainedSlice) {
            // This is expected to fail ... so no need to release.
            slice.retainedSlice(index, length);
        } else {
            slice.slice(index, length);
        }
    } finally {
        if (initRetainedSlice) {
            slice.release();
        }
        buf.release();
    }
}
 
Example 3
Source File: AbstractByteBufTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private void testSliceContents(boolean retainedSlice) {
    ByteBuf buf = newBuffer(8).resetWriterIndex();
    ByteBuf expected = newBuffer(3).resetWriterIndex();
    buf.writeBytes(new byte[] {1, 2, 3, 4, 5, 6, 7, 8});
    expected.writeBytes(new byte[] {4, 5, 6});
    ByteBuf slice = retainedSlice ? buf.retainedSlice(buf.readerIndex() + 3, 3)
                                  : buf.slice(buf.readerIndex() + 3, 3);
    try {
        assertEquals(0, slice.compareTo(expected));
        assertEquals(0, slice.compareTo(slice.duplicate()));
        ByteBuf b = slice.retainedDuplicate();
        assertEquals(0, slice.compareTo(b));
        b.release();
        assertEquals(0, slice.compareTo(slice.slice(0, slice.capacity())));
    } finally {
        if (retainedSlice) {
            slice.release();
        }
        buf.release();
        expected.release();
    }
}
 
Example 4
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 6 votes vote down vote up
private void testSliceContents(boolean retainedSlice) {
    ByteBuf buf = newBuffer(8).resetWriterIndex();
    ByteBuf expected = newBuffer(3).resetWriterIndex();
    buf.writeBytes(new byte[] {1, 2, 3, 4, 5, 6, 7, 8});
    expected.writeBytes(new byte[] {4, 5, 6});
    ByteBuf slice = retainedSlice ? buf.retainedSlice(buf.readerIndex() + 3, 3)
                                  : buf.slice(buf.readerIndex() + 3, 3);
    try {
        assertEquals(0, slice.compareTo(expected));
        assertEquals(0, slice.compareTo(slice.duplicate()));
        ByteBuf b = slice.retainedDuplicate();
        assertEquals(0, slice.compareTo(b));
        b.release();
        assertEquals(0, slice.compareTo(slice.slice(0, slice.capacity())));
    } finally {
        if (retainedSlice) {
            slice.release();
        }
        buf.release();
        expected.release();
    }
}
 
Example 5
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 6 votes vote down vote up
private void testSliceOutOfBounds(boolean initRetainedSlice, boolean finalRetainedSlice, boolean indexOutOfBounds) {
    ByteBuf buf = newBuffer(8);
    ByteBuf slice = initRetainedSlice ? buf.retainedSlice(buf.readerIndex() + 1, 2)
                                      : buf.slice(buf.readerIndex() + 1, 2);
    try {
        assertEquals(2, slice.capacity());
        assertEquals(2, slice.maxCapacity());
        final int index = indexOutOfBounds ? 3 : 0;
        final int length = indexOutOfBounds ? 0 : 3;
        if (finalRetainedSlice) {
            // This is expected to fail ... so no need to release.
            slice.retainedSlice(index, length);
        } else {
            slice.slice(index, length);
        }
    } finally {
        if (initRetainedSlice) {
            slice.release();
        }
        buf.release();
    }
}
 
Example 6
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void testSliceCapacityChange(boolean retainedSlice) {
    ByteBuf buf = newBuffer(8);
    ByteBuf slice = retainedSlice ? buf.retainedSlice(buf.readerIndex() + 1, 3)
                                  : buf.slice(buf.readerIndex() + 1, 3);
    try {
        slice.capacity(10);
    } finally {
        if (retainedSlice) {
            slice.release();
        }
        buf.release();
    }
}
 
Example 7
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void testSliceCapacityChange(boolean retainedSlice) {
    ByteBuf buf = newBuffer(8);
    ByteBuf slice = retainedSlice ? buf.retainedSlice(buf.readerIndex() + 1, 3)
                                  : buf.slice(buf.readerIndex() + 1, 3);
    try {
        slice.capacity(10);
    } finally {
        if (retainedSlice) {
            slice.release();
        }
        buf.release();
    }
}
 
Example 8
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testDuplicateAfterReleaseRetainedDuplicateSlice() {
    ByteBuf buf = newBuffer(1);
    ByteBuf buf2 = buf.retainedDuplicate();
    ByteBuf buf3 = buf2.slice(0, 1);
    assertDuplicateFailAfterRelease(buf, buf2, buf3);
}
 
Example 9
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testRetainedSliceAfterReleaseRetainedDuplicateSlice() {
    ByteBuf buf = newBuffer(1);
    ByteBuf buf2 = buf.retainedDuplicate();
    ByteBuf buf3 = buf2.slice(0, 1);
    assertRetainedSliceFailAfterRelease(buf, buf2, buf3);
}
 
Example 10
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSliceAfterReleaseRetainedDuplicateSlice() {
    ByteBuf buf = newBuffer(1);
    ByteBuf buf2 = buf.retainedDuplicate();
    ByteBuf buf3 = buf2.slice(0, 1);
    assertSliceFailAfterRelease(buf, buf2, buf3);
}
 
Example 11
Source File: AbstractByteBufTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testSliceAfterReleaseRetainedDuplicateSlice() {
    ByteBuf buf = newBuffer(1);
    ByteBuf buf2 = buf.retainedDuplicate();
    ByteBuf buf3 = buf2.slice(0, 1);
    assertSliceFailAfterRelease(buf, buf2, buf3);
}
 
Example 12
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testDuplicateAfterReleaseRetainedDuplicateSlice() {
    ByteBuf buf = newBuffer(1);
    ByteBuf buf2 = buf.retainedDuplicate();
    ByteBuf buf3 = buf2.slice(0, 1);
    assertDuplicateFailAfterRelease(buf, buf2, buf3);
}
 
Example 13
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testRetainedSliceAfterReleaseRetainedDuplicateSlice() {
    ByteBuf buf = newBuffer(1);
    ByteBuf buf2 = buf.retainedDuplicate();
    ByteBuf buf3 = buf2.slice(0, 1);
    assertRetainedSliceFailAfterRelease(buf, buf2, buf3);
}
 
Example 14
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSliceAfterReleaseRetainedDuplicateSlice() {
    ByteBuf buf = newBuffer(1);
    ByteBuf buf2 = buf.retainedDuplicate();
    ByteBuf buf3 = buf2.slice(0, 1);
    assertSliceFailAfterRelease(buf, buf2, buf3);
}
 
Example 15
Source File: AbstractByteBufTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void testSliceCapacityChange(boolean retainedSlice) {
    ByteBuf buf = newBuffer(8);
    ByteBuf slice = retainedSlice ? buf.retainedSlice(buf.readerIndex() + 1, 3)
                                  : buf.slice(buf.readerIndex() + 1, 3);
    try {
        slice.capacity(10);
    } finally {
        if (retainedSlice) {
            slice.release();
        }
        buf.release();
    }
}
 
Example 16
Source File: AbstractByteBufTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testDuplicateAfterReleaseRetainedDuplicateSlice() {
    ByteBuf buf = newBuffer(1);
    ByteBuf buf2 = buf.retainedDuplicate();
    ByteBuf buf3 = buf2.slice(0, 1);
    assertDuplicateFailAfterRelease(buf, buf2, buf3);
}
 
Example 17
Source File: AbstractByteBufTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testRetainedSliceAfterReleaseRetainedDuplicateSlice() {
    ByteBuf buf = newBuffer(1);
    ByteBuf buf2 = buf.retainedDuplicate();
    ByteBuf buf3 = buf2.slice(0, 1);
    assertRetainedSliceFailAfterRelease(buf, buf2, buf3);
}
 
Example 18
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 4 votes vote down vote up
private void testMultipleLevelRetainedSliceWithNonRetained(boolean doSlice1, boolean doSlice2) {
    ByteBuf buf = newBuffer(8).resetWriterIndex();
    ByteBuf expected1 = newBuffer(6).resetWriterIndex();
    ByteBuf expected2 = newBuffer(4).resetWriterIndex();
    ByteBuf expected3 = newBuffer(2).resetWriterIndex();
    ByteBuf expected4SliceSlice = newBuffer(1).resetWriterIndex();
    ByteBuf expected4DupSlice = newBuffer(1).resetWriterIndex();
    buf.writeBytes(new byte[] {1, 2, 3, 4, 5, 6, 7, 8});
    expected1.writeBytes(new byte[] {2, 3, 4, 5, 6, 7});
    expected2.writeBytes(new byte[] {3, 4, 5, 6});
    expected3.writeBytes(new byte[] {4, 5});
    expected4SliceSlice.writeBytes(new byte[] {5});
    expected4DupSlice.writeBytes(new byte[] {4});

    ByteBuf slice1 = buf.retainedSlice(buf.readerIndex() + 1, 6);
    assertEquals(0, slice1.compareTo(expected1));
    // Simulate a handler that releases the original buffer, and propagates a slice.
    buf.release();

    ByteBuf slice2 = slice1.retainedSlice(slice1.readerIndex() + 1, 4);
    assertEquals(0, slice2.compareTo(expected2));
    assertEquals(0, slice2.compareTo(slice2.duplicate()));
    assertEquals(0, slice2.compareTo(slice2.slice()));

    ByteBuf tmpBuf = slice2.retainedDuplicate();
    assertEquals(0, slice2.compareTo(tmpBuf));
    tmpBuf.release();
    tmpBuf = slice2.retainedSlice();
    assertEquals(0, slice2.compareTo(tmpBuf));
    tmpBuf.release();

    ByteBuf slice3 = doSlice1 ? slice2.slice(slice2.readerIndex() + 1, 2) : slice2.duplicate();
    if (doSlice1) {
        assertEquals(0, slice3.compareTo(expected3));
    } else {
        assertEquals(0, slice3.compareTo(expected2));
    }

    ByteBuf slice4 = doSlice2 ? slice3.slice(slice3.readerIndex() + 1, 1) : slice3.duplicate();
    if (doSlice1 && doSlice2) {
        assertEquals(0, slice4.compareTo(expected4SliceSlice));
    } else if (doSlice2) {
        assertEquals(0, slice4.compareTo(expected4DupSlice));
    } else {
        assertEquals(0, slice3.compareTo(slice4));
    }

    // Cleanup the expected buffers used for testing.
    assertTrue(expected1.release());
    assertTrue(expected2.release());
    assertTrue(expected3.release());
    assertTrue(expected4SliceSlice.release());
    assertTrue(expected4DupSlice.release());

    // Slice 4, 3, and 2 should effectively "share" a reference count.
    slice4.release();
    assertEquals(slice3.refCnt(), slice2.refCnt());
    assertEquals(slice3.refCnt(), slice4.refCnt());

    // Slice 1 should also release the original underlying buffer without throwing exceptions
    assertTrue(slice1.release());

    // Reference counting may be shared, or may be independently tracked, but at this point all buffers should
    // be deallocated and have a reference count of 0.
    assertEquals(0, buf.refCnt());
    assertEquals(0, slice1.refCnt());
    assertEquals(0, slice2.refCnt());
    assertEquals(0, slice3.refCnt());
}
 
Example 19
Source File: AbstractByteBufTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private void testMultipleLevelRetainedSliceWithNonRetained(boolean doSlice1, boolean doSlice2) {
    ByteBuf buf = newBuffer(8).resetWriterIndex();
    ByteBuf expected1 = newBuffer(6).resetWriterIndex();
    ByteBuf expected2 = newBuffer(4).resetWriterIndex();
    ByteBuf expected3 = newBuffer(2).resetWriterIndex();
    ByteBuf expected4SliceSlice = newBuffer(1).resetWriterIndex();
    ByteBuf expected4DupSlice = newBuffer(1).resetWriterIndex();
    buf.writeBytes(new byte[] {1, 2, 3, 4, 5, 6, 7, 8});
    expected1.writeBytes(new byte[] {2, 3, 4, 5, 6, 7});
    expected2.writeBytes(new byte[] {3, 4, 5, 6});
    expected3.writeBytes(new byte[] {4, 5});
    expected4SliceSlice.writeBytes(new byte[] {5});
    expected4DupSlice.writeBytes(new byte[] {4});

    ByteBuf slice1 = buf.retainedSlice(buf.readerIndex() + 1, 6);
    assertEquals(0, slice1.compareTo(expected1));
    // Simulate a handler that releases the original buffer, and propagates a slice.
    buf.release();

    ByteBuf slice2 = slice1.retainedSlice(slice1.readerIndex() + 1, 4);
    assertEquals(0, slice2.compareTo(expected2));
    assertEquals(0, slice2.compareTo(slice2.duplicate()));
    assertEquals(0, slice2.compareTo(slice2.slice()));

    ByteBuf tmpBuf = slice2.retainedDuplicate();
    assertEquals(0, slice2.compareTo(tmpBuf));
    tmpBuf.release();
    tmpBuf = slice2.retainedSlice();
    assertEquals(0, slice2.compareTo(tmpBuf));
    tmpBuf.release();

    ByteBuf slice3 = doSlice1 ? slice2.slice(slice2.readerIndex() + 1, 2) : slice2.duplicate();
    if (doSlice1) {
        assertEquals(0, slice3.compareTo(expected3));
    } else {
        assertEquals(0, slice3.compareTo(expected2));
    }

    ByteBuf slice4 = doSlice2 ? slice3.slice(slice3.readerIndex() + 1, 1) : slice3.duplicate();
    if (doSlice1 && doSlice2) {
        assertEquals(0, slice4.compareTo(expected4SliceSlice));
    } else if (doSlice2) {
        assertEquals(0, slice4.compareTo(expected4DupSlice));
    } else {
        assertEquals(0, slice3.compareTo(slice4));
    }

    // Cleanup the expected buffers used for testing.
    assertTrue(expected1.release());
    assertTrue(expected2.release());
    assertTrue(expected3.release());
    assertTrue(expected4SliceSlice.release());
    assertTrue(expected4DupSlice.release());

    // Slice 4, 3, and 2 should effectively "share" a reference count.
    slice4.release();
    assertEquals(slice3.refCnt(), slice2.refCnt());
    assertEquals(slice3.refCnt(), slice4.refCnt());

    // Slice 1 should also release the original underlying buffer without throwing exceptions
    assertTrue(slice1.release());

    // Reference counting may be shared, or may be independently tracked, but at this point all buffers should
    // be deallocated and have a reference count of 0.
    assertEquals(0, buf.refCnt());
    assertEquals(0, slice1.refCnt());
    assertEquals(0, slice2.refCnt());
    assertEquals(0, slice3.refCnt());
}
 
Example 20
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 4 votes vote down vote up
private void testMultipleLevelRetainedSliceWithNonRetained(boolean doSlice1, boolean doSlice2) {
    ByteBuf buf = newBuffer(8).resetWriterIndex();
    ByteBuf expected1 = newBuffer(6).resetWriterIndex();
    ByteBuf expected2 = newBuffer(4).resetWriterIndex();
    ByteBuf expected3 = newBuffer(2).resetWriterIndex();
    ByteBuf expected4SliceSlice = newBuffer(1).resetWriterIndex();
    ByteBuf expected4DupSlice = newBuffer(1).resetWriterIndex();
    buf.writeBytes(new byte[] {1, 2, 3, 4, 5, 6, 7, 8});
    expected1.writeBytes(new byte[] {2, 3, 4, 5, 6, 7});
    expected2.writeBytes(new byte[] {3, 4, 5, 6});
    expected3.writeBytes(new byte[] {4, 5});
    expected4SliceSlice.writeBytes(new byte[] {5});
    expected4DupSlice.writeBytes(new byte[] {4});

    ByteBuf slice1 = buf.retainedSlice(buf.readerIndex() + 1, 6);
    assertEquals(0, slice1.compareTo(expected1));
    // Simulate a handler that releases the original buffer, and propagates a slice.
    buf.release();

    ByteBuf slice2 = slice1.retainedSlice(slice1.readerIndex() + 1, 4);
    assertEquals(0, slice2.compareTo(expected2));
    assertEquals(0, slice2.compareTo(slice2.duplicate()));
    assertEquals(0, slice2.compareTo(slice2.slice()));

    ByteBuf tmpBuf = slice2.retainedDuplicate();
    assertEquals(0, slice2.compareTo(tmpBuf));
    tmpBuf.release();
    tmpBuf = slice2.retainedSlice();
    assertEquals(0, slice2.compareTo(tmpBuf));
    tmpBuf.release();

    ByteBuf slice3 = doSlice1 ? slice2.slice(slice2.readerIndex() + 1, 2) : slice2.duplicate();
    if (doSlice1) {
        assertEquals(0, slice3.compareTo(expected3));
    } else {
        assertEquals(0, slice3.compareTo(expected2));
    }

    ByteBuf slice4 = doSlice2 ? slice3.slice(slice3.readerIndex() + 1, 1) : slice3.duplicate();
    if (doSlice1 && doSlice2) {
        assertEquals(0, slice4.compareTo(expected4SliceSlice));
    } else if (doSlice2) {
        assertEquals(0, slice4.compareTo(expected4DupSlice));
    } else {
        assertEquals(0, slice3.compareTo(slice4));
    }

    // Cleanup the expected buffers used for testing.
    assertTrue(expected1.release());
    assertTrue(expected2.release());
    assertTrue(expected3.release());
    assertTrue(expected4SliceSlice.release());
    assertTrue(expected4DupSlice.release());

    // Slice 4, 3, and 2 should effectively "share" a reference count.
    slice4.release();
    assertEquals(slice3.refCnt(), slice2.refCnt());
    assertEquals(slice3.refCnt(), slice4.refCnt());

    // Slice 1 should also release the original underlying buffer without throwing exceptions
    assertTrue(slice1.release());

    // Reference counting may be shared, or may be independently tracked, but at this point all buffers should
    // be deallocated and have a reference count of 0.
    assertEquals(0, buf.refCnt());
    assertEquals(0, slice1.refCnt());
    assertEquals(0, slice2.refCnt());
    assertEquals(0, slice3.refCnt());
}