Java Code Examples for org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf#retainedSlice()
The following examples show how to use
org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf#retainedSlice() .
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 |
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 2
Source File: AbstractByteBufTest.java From flink with Apache License 2.0 | 6 votes |
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 | 5 votes |
@Test public void testSliceAfterReleaseRetainedSliceDuplicate() { ByteBuf buf = newBuffer(1); ByteBuf buf2 = buf.retainedSlice(0, 1); ByteBuf buf3 = buf2.duplicate(); assertSliceFailAfterRelease(buf, buf2, buf3); }
Example 4
Source File: AbstractByteBufTest.java From flink with Apache License 2.0 | 5 votes |
private void testReadRetainedSliceUnreleasable(boolean initRetainedSlice, boolean finalRetainedSlice) { ByteBuf buf = newBuffer(8); ByteBuf buf1 = initRetainedSlice ? buf.retainedSlice() : buf.slice().retain(); ByteBuf buf2 = unreleasableBuffer(buf1); ByteBuf buf3 = finalRetainedSlice ? buf2.readRetainedSlice(buf2.readableBytes()) : buf2.readSlice(buf2.readableBytes()).retain(); assertFalse(buf3.release()); assertFalse(buf2.release()); buf1.release(); assertTrue(buf.release()); assertEquals(0, buf1.refCnt()); assertEquals(0, buf.refCnt()); }
Example 5
Source File: AbstractByteBufTest.java From flink with Apache License 2.0 | 5 votes |
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 6
Source File: AbstractByteBufTest.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
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 |
@Test public void testDuplicateAfterReleaseRetainedSliceDuplicate() { ByteBuf buf = newBuffer(1); ByteBuf buf2 = buf.retainedSlice(0, 1); ByteBuf buf3 = buf2.duplicate(); assertDuplicateFailAfterRelease(buf, buf2, buf3); }
Example 8
Source File: AbstractByteBufTest.java From flink with Apache License 2.0 | 5 votes |
@Test public void testRetainedSliceAfterReleaseRetainedSliceRetainedDuplicate() { ByteBuf buf = newBuffer(1); ByteBuf buf2 = buf.retainedSlice(0, 1); ByteBuf buf3 = buf2.retainedDuplicate(); assertRetainedSliceFailAfterRelease(buf, buf2, buf3); }
Example 9
Source File: AbstractByteBufTest.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
@Test public void testDuplicateAfterReleaseRetainedSliceDuplicate() { ByteBuf buf = newBuffer(1); ByteBuf buf2 = buf.retainedSlice(0, 1); ByteBuf buf3 = buf2.duplicate(); assertDuplicateFailAfterRelease(buf, buf2, buf3); }
Example 10
Source File: AbstractByteBufTest.java From flink with Apache License 2.0 | 5 votes |
private void testSliceReleaseOriginal(boolean retainedSlice1, boolean retainedSlice2) { ByteBuf buf = newBuffer(8).resetWriterIndex(); ByteBuf expected1 = newBuffer(3).resetWriterIndex(); ByteBuf expected2 = newBuffer(2).resetWriterIndex(); buf.writeBytes(new byte[] {1, 2, 3, 4, 5, 6, 7, 8}); expected1.writeBytes(new byte[] {6, 7, 8}); expected2.writeBytes(new byte[] {7, 8}); ByteBuf slice1 = retainedSlice1 ? buf.retainedSlice(buf.readerIndex() + 5, 3) : buf.slice(buf.readerIndex() + 5, 3).retain(); assertEquals(0, slice1.compareTo(expected1)); // Simulate a handler that releases the original buffer, and propagates a slice. buf.release(); ByteBuf slice2 = retainedSlice2 ? slice1.retainedSlice(slice1.readerIndex() + 1, 2) : slice1.slice(slice1.readerIndex() + 1, 2).retain(); assertEquals(0, slice2.compareTo(expected2)); // Cleanup the expected buffers used for testing. assertTrue(expected1.release()); assertTrue(expected2.release()); // The handler created a slice of the slice and is now done with it. slice2.release(); // The handler is now done with the original slice 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()); }
Example 11
Source File: AbstractByteBufTest.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
@Test public void testRetainedSliceAfterReleaseRetainedSliceDuplicate() { ByteBuf buf = newBuffer(1); ByteBuf buf2 = buf.retainedSlice(0, 1); ByteBuf buf3 = buf2.duplicate(); assertRetainedSliceFailAfterRelease(buf, buf2, buf3); }
Example 12
Source File: AbstractByteBufTest.java From flink with Apache License 2.0 | 5 votes |
@Test public void testDuplicateAfterReleaseRetainedSliceDuplicate() { ByteBuf buf = newBuffer(1); ByteBuf buf2 = buf.retainedSlice(0, 1); ByteBuf buf3 = buf2.duplicate(); assertDuplicateFailAfterRelease(buf, buf2, buf3); }
Example 13
Source File: AbstractByteBufTest.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
@Test public void testRetainedSliceAndRetainedDuplicateContentIsExpected() { ByteBuf buf = newBuffer(8).resetWriterIndex(); ByteBuf expected1 = newBuffer(6).resetWriterIndex(); ByteBuf expected2 = newBuffer(5).resetWriterIndex(); ByteBuf expected3 = newBuffer(4).resetWriterIndex(); ByteBuf expected4 = newBuffer(3).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, 7}); expected3.writeBytes(new byte[] {4, 5, 6, 7}); expected4.writeBytes(new byte[] {5, 6, 7}); ByteBuf slice1 = buf.retainedSlice(buf.readerIndex() + 1, 6); assertEquals(0, slice1.compareTo(expected1)); assertEquals(0, slice1.compareTo(buf.slice(buf.readerIndex() + 1, 6))); // Simulate a handler that releases the original buffer, and propagates a slice. buf.release(); // Advance the reader index on the slice. slice1.readByte(); ByteBuf dup1 = slice1.retainedDuplicate(); assertEquals(0, dup1.compareTo(expected2)); assertEquals(0, dup1.compareTo(slice1.duplicate())); // Advance the reader index on dup1. dup1.readByte(); ByteBuf dup2 = dup1.duplicate(); assertEquals(0, dup2.compareTo(expected3)); // Advance the reader index on dup2. dup2.readByte(); ByteBuf slice2 = dup2.retainedSlice(dup2.readerIndex(), 3); assertEquals(0, slice2.compareTo(expected4)); assertEquals(0, slice2.compareTo(dup2.slice(dup2.readerIndex(), 3))); // Cleanup the expected buffers used for testing. assertTrue(expected1.release()); assertTrue(expected2.release()); assertTrue(expected3.release()); assertTrue(expected4.release()); slice2.release(); dup2.release(); assertEquals(slice2.refCnt(), dup2.refCnt()); assertEquals(dup2.refCnt(), dup1.refCnt()); // The handler is now done with the original slice 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, dup1.refCnt()); assertEquals(0, dup2.refCnt()); }
Example 14
Source File: AbstractByteBufTest.java From flink with Apache License 2.0 | 4 votes |
private void testMultipleRetainedDuplicateReleaseOriginal(boolean retainedDuplicate1, boolean retainedDuplicate2) { ByteBuf buf = newBuffer(8).resetWriterIndex(); ByteBuf expected = newBuffer(8).resetWriterIndex(); buf.writeBytes(new byte[] {1, 2, 3, 4, 5, 6, 7, 8}); expected.writeBytes(buf, buf.readerIndex(), buf.readableBytes()); ByteBuf dup1 = retainedDuplicate1 ? buf.retainedDuplicate() : buf.duplicate().retain(); assertEquals(0, dup1.compareTo(expected)); // Simulate a handler that releases the original buffer, and propagates a slice. buf.release(); ByteBuf dup2 = retainedDuplicate2 ? dup1.retainedDuplicate() : dup1.duplicate().retain(); assertEquals(0, dup2.compareTo(expected)); assertEquals(0, dup2.compareTo(dup2.duplicate())); assertEquals(0, dup2.compareTo(dup2.slice())); ByteBuf tmpBuf = dup2.retainedDuplicate(); assertEquals(0, dup2.compareTo(tmpBuf)); tmpBuf.release(); tmpBuf = dup2.retainedSlice(); assertEquals(0, dup2.compareTo(tmpBuf)); tmpBuf.release(); // The handler created a slice of the slice and is now done with it. dup2.release(); ByteBuf dup3 = dup1.retainedDuplicate(); assertEquals(0, dup3.compareTo(expected)); // The handler created another slice of the slice and is now done with it. dup3.release(); // The handler is now done with the original slice assertTrue(dup1.release()); // Cleanup the expected buffers used for testing. assertTrue(expected.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, dup1.refCnt()); assertEquals(0, dup2.refCnt()); assertEquals(0, dup3.refCnt()); }
Example 15
Source File: AbstractByteBufTest.java From flink with Apache License 2.0 | 4 votes |
private void testMultipleRetainedSliceReleaseOriginal(boolean retainedSlice1, boolean retainedSlice2) { ByteBuf buf = newBuffer(8).resetWriterIndex(); ByteBuf expected1 = newBuffer(3).resetWriterIndex(); ByteBuf expected2 = newBuffer(2).resetWriterIndex(); ByteBuf expected3 = newBuffer(2).resetWriterIndex(); buf.writeBytes(new byte[] {1, 2, 3, 4, 5, 6, 7, 8}); expected1.writeBytes(new byte[] {6, 7, 8}); expected2.writeBytes(new byte[] {7, 8}); expected3.writeBytes(new byte[] {6, 7}); ByteBuf slice1 = retainedSlice1 ? buf.retainedSlice(buf.readerIndex() + 5, 3) : buf.slice(buf.readerIndex() + 5, 3).retain(); assertEquals(0, slice1.compareTo(expected1)); // Simulate a handler that releases the original buffer, and propagates a slice. buf.release(); ByteBuf slice2 = retainedSlice2 ? slice1.retainedSlice(slice1.readerIndex() + 1, 2) : slice1.slice(slice1.readerIndex() + 1, 2).retain(); assertEquals(0, slice2.compareTo(expected2)); // The handler created a slice of the slice and is now done with it. slice2.release(); ByteBuf slice3 = slice1.retainedSlice(slice1.readerIndex(), 2); assertEquals(0, slice3.compareTo(expected3)); // The handler created another slice of the slice and is now done with it. slice3.release(); // The handler is now done with the original slice assertTrue(slice1.release()); // Cleanup the expected buffers used for testing. assertTrue(expected1.release()); assertTrue(expected2.release()); assertTrue(expected3.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 16
Source File: AbstractByteBufTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testSliceAfterReleaseRetainedSlice() { ByteBuf buf = newBuffer(1); ByteBuf buf2 = buf.retainedSlice(0, 1); assertSliceFailAfterRelease(buf, buf2); }
Example 17
Source File: AbstractByteBufTest.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
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 18
Source File: AbstractByteBufTest.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
@Test public void testSliceAfterReleaseRetainedSlice() { ByteBuf buf = newBuffer(1); ByteBuf buf2 = buf.retainedSlice(0, 1); assertSliceFailAfterRelease(buf, buf2); }
Example 19
Source File: AbstractByteBufTest.java From flink with Apache License 2.0 | 4 votes |
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 |
@Test public void testRetainedSliceAndRetainedDuplicateContentIsExpected() { ByteBuf buf = newBuffer(8).resetWriterIndex(); ByteBuf expected1 = newBuffer(6).resetWriterIndex(); ByteBuf expected2 = newBuffer(5).resetWriterIndex(); ByteBuf expected3 = newBuffer(4).resetWriterIndex(); ByteBuf expected4 = newBuffer(3).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, 7}); expected3.writeBytes(new byte[] {4, 5, 6, 7}); expected4.writeBytes(new byte[] {5, 6, 7}); ByteBuf slice1 = buf.retainedSlice(buf.readerIndex() + 1, 6); assertEquals(0, slice1.compareTo(expected1)); assertEquals(0, slice1.compareTo(buf.slice(buf.readerIndex() + 1, 6))); // Simulate a handler that releases the original buffer, and propagates a slice. buf.release(); // Advance the reader index on the slice. slice1.readByte(); ByteBuf dup1 = slice1.retainedDuplicate(); assertEquals(0, dup1.compareTo(expected2)); assertEquals(0, dup1.compareTo(slice1.duplicate())); // Advance the reader index on dup1. dup1.readByte(); ByteBuf dup2 = dup1.duplicate(); assertEquals(0, dup2.compareTo(expected3)); // Advance the reader index on dup2. dup2.readByte(); ByteBuf slice2 = dup2.retainedSlice(dup2.readerIndex(), 3); assertEquals(0, slice2.compareTo(expected4)); assertEquals(0, slice2.compareTo(dup2.slice(dup2.readerIndex(), 3))); // Cleanup the expected buffers used for testing. assertTrue(expected1.release()); assertTrue(expected2.release()); assertTrue(expected3.release()); assertTrue(expected4.release()); slice2.release(); dup2.release(); assertEquals(slice2.refCnt(), dup2.refCnt()); assertEquals(dup2.refCnt(), dup1.refCnt()); // The handler is now done with the original slice 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, dup1.refCnt()); assertEquals(0, dup2.refCnt()); }