Java Code Examples for org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf#writeBytes()
The following examples show how to use
org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf#writeBytes() .
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 |
@Test public void testForEachByte2() { byte[] expected = {1, 2, 3, 4}; ByteBuf buf = newBuffer(expected.length); try { buf.writeBytes(expected); final byte[] bytes = new byte[expected.length]; int i = buf.forEachByte(new ByteProcessor() { private int index; @Override public boolean process(byte value) throws Exception { bytes[index++] = value; return true; } }); assertEquals(-1, i); assertArrayEquals(expected, bytes); } finally { buf.release(); } }
Example 2
Source File: AbstractByteBufTest.java From flink with Apache License 2.0 | 6 votes |
@Test public void testForEachByte2() { byte[] expected = {1, 2, 3, 4}; ByteBuf buf = newBuffer(expected.length); try { buf.writeBytes(expected); final byte[] bytes = new byte[expected.length]; int i = buf.forEachByte(new ByteProcessor() { private int index; @Override public boolean process(byte value) throws Exception { bytes[index++] = value; return true; } }); assertEquals(-1, i); assertArrayEquals(expected, bytes); } finally { buf.release(); } }
Example 3
Source File: AbstractByteBufTest.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
@Test public void testForEachByteDesc2() { byte[] expected = {1, 2, 3, 4}; ByteBuf buf = newBuffer(expected.length); try { buf.writeBytes(expected); final byte[] bytes = new byte[expected.length]; int i = buf.forEachByteDesc(new ByteProcessor() { private int index = bytes.length - 1; @Override public boolean process(byte value) throws Exception { bytes[index--] = value; return true; } }); assertEquals(-1, i); assertArrayEquals(expected, bytes); } finally { buf.release(); } }
Example 4
Source File: ByteBufUtils.java From flink with Apache License 2.0 | 6 votes |
/** * Accumulates data from <tt>source</tt> to <tt>target</tt>. If no data has been * accumulated yet and <tt>source</tt> has enough data, <tt>source</tt> will be * returned directly. Otherwise, data will be copied into <tt>target</tt>. If the * size of data copied after this operation has reached <tt>targetAccumulationSize</tt>, * <tt>target</tt> will be returned, otherwise <tt>null</tt> will be returned to indicate * more data is required. * * @param target The target buffer. * @param source The source buffer. * @param targetAccumulationSize The target size of data to accumulate. * @param accumulatedSize The size of data accumulated so far. * * @return The ByteBuf containing accumulated data. If not enough data has been accumulated, * <tt>null</tt> will be returned. */ @Nullable public static ByteBuf accumulate(ByteBuf target, ByteBuf source, int targetAccumulationSize, int accumulatedSize) { if (accumulatedSize == 0 && source.readableBytes() >= targetAccumulationSize) { return source; } int copyLength = Math.min(source.readableBytes(), targetAccumulationSize - accumulatedSize); if (copyLength > 0) { target.writeBytes(source, copyLength); } if (accumulatedSize + copyLength == targetAccumulationSize) { return target; } return null; }
Example 5
Source File: NettyMessageClientDecoderDelegateTest.java From flink with Apache License 2.0 | 6 votes |
private List<ByteBuf> partitionBuffer(ByteBuf buffer, int partitionSize) { List<ByteBuf> result = new ArrayList<>(); try { int bufferSize = buffer.readableBytes(); for (int position = 0; position < bufferSize; position += partitionSize) { int endPosition = Math.min(position + partitionSize, bufferSize); ByteBuf partitionedBuffer = ALLOCATOR.buffer(endPosition - position); partitionedBuffer.writeBytes(buffer, position, endPosition - position); result.add(partitionedBuffer); } } catch (Throwable t) { releaseBuffers(result.toArray(new ByteBuf[0])); ExceptionUtils.rethrow(t); } return result; }
Example 6
Source File: AbstractByteBufTest.java From flink with Apache License 2.0 | 6 votes |
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 7
Source File: AbstractByteBufTest.java From flink with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("ForLoopThatDoesntUseLoopVariable") public void testNioBufferExposeOnlyRegion() { final ByteBuf buffer = newBuffer(8); byte[] data = new byte[8]; random.nextBytes(data); buffer.writeBytes(data); ByteBuffer nioBuf = buffer.nioBuffer(1, data.length - 2); assertEquals(0, nioBuf.position()); assertEquals(6, nioBuf.remaining()); for (int i = 1; nioBuf.hasRemaining(); i++) { assertEquals(data[i], nioBuf.get()); } buffer.release(); }
Example 8
Source File: AbstractByteBufTest.java From flink with Apache License 2.0 | 6 votes |
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 9
Source File: MessageSerializer.java From flink with Apache License 2.0 | 6 votes |
/** * Helper for serializing the messages. * * @param alloc The {@link ByteBufAllocator} used to allocate the buffer to serialize the message into. * @param requestId The id of the request to which the message refers to. * @param messageType The {@link MessageType type of the message}. * @param payload The serialized version of the message. * @return A {@link ByteBuf} containing the serialized message. */ private static ByteBuf writePayload( final ByteBufAllocator alloc, final long requestId, final MessageType messageType, final byte[] payload) { final int frameLength = HEADER_LENGTH + REQUEST_ID_SIZE + payload.length; final ByteBuf buf = alloc.ioBuffer(frameLength + Integer.BYTES); buf.writeInt(frameLength); writeHeader(buf, messageType); buf.writeLong(requestId); buf.writeBytes(payload); return buf; }
Example 10
Source File: AbstractByteBufTest.java From Flink-CEPplus 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: NettyMessageClientDecoderDelegateTest.java From flink with Apache License 2.0 | 5 votes |
private ByteBuf mergeBuffers(ByteBuf[] buffers, int start, int end) { ByteBuf mergedBuffer = ALLOCATOR.buffer(); for (int i = start; i < end; ++i) { mergedBuffer.writeBytes(buffers[i]); } return mergedBuffer; }
Example 12
Source File: NettyMessage.java From flink with Apache License 2.0 | 5 votes |
@Override ByteBuf write(ByteBufAllocator allocator) throws IOException { ByteBuf result = null; try { // TODO Directly serialize to Netty's buffer ByteBuffer serializedEvent = EventSerializer.toSerializedEvent(event); result = allocateBuffer(allocator, ID, 4 + serializedEvent.remaining() + 20 + 16 + 16); result.writeInt(serializedEvent.remaining()); result.writeBytes(serializedEvent); partitionId.getPartitionId().writeTo(result); partitionId.getProducerId().writeTo(result); receiverId.writeTo(result); return result; } catch (Throwable t) { if (result != null) { result.release(); } throw new IOException(t); } }
Example 13
Source File: AbstractByteBufTest.java From flink with Apache License 2.0 | 5 votes |
@Test(expected = IndexOutOfBoundsException.class) public void testGetBytesByteBuffer() { byte[] bytes = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}; // Ensure destination buffer is bigger then what is in the ByteBuf. ByteBuffer nioBuffer = ByteBuffer.allocate(bytes.length + 1); ByteBuf buffer = newBuffer(bytes.length); try { buffer.writeBytes(bytes); buffer.getBytes(buffer.readerIndex(), nioBuffer); } finally { buffer.release(); } }
Example 14
Source File: AbstractByteBufTest.java From flink with Apache License 2.0 | 5 votes |
@Test(expected = IndexOutOfBoundsException.class) public void testGetBytesByteBuffer() { byte[] bytes = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}; // Ensure destination buffer is bigger then what is in the ByteBuf. ByteBuffer nioBuffer = ByteBuffer.allocate(bytes.length + 1); ByteBuf buffer = newBuffer(bytes.length); try { buffer.writeBytes(bytes); buffer.getBytes(buffer.readerIndex(), nioBuffer); } finally { buffer.release(); } }
Example 15
Source File: NetworkBuffer.java From flink with Apache License 2.0 | 5 votes |
@Override public ByteBuf readBytes(int length) { // copied from the one in netty 4.0.50 fixing the wrong allocator being used checkReadableBytes(length); if (length == 0) { return Unpooled.EMPTY_BUFFER; } ByteBuf buf = alloc().buffer(length, maxCapacity()); int readerIndex = readerIndex(); buf.writeBytes(this, readerIndex, length); readerIndex(readerIndex + length); return buf; }
Example 16
Source File: AbstractByteBufTest.java From flink with Apache License 2.0 | 5 votes |
@Test public void testHashCode() { ByteBuf elemA = buffer(15); ByteBuf elemB = directBuffer(15); elemA.writeBytes(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 }); elemB.writeBytes(new byte[] { 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9 }); Set<ByteBuf> set = new HashSet<ByteBuf>(); set.add(elemA); set.add(elemB); assertEquals(2, set.size()); ByteBuf elemACopy = elemA.copy(); assertTrue(set.contains(elemACopy)); ByteBuf elemBCopy = elemB.copy(); assertTrue(set.contains(elemBCopy)); buffer.clear(); buffer.writeBytes(elemA.duplicate()); assertTrue(set.remove(buffer)); assertFalse(set.contains(elemA)); assertEquals(1, set.size()); buffer.clear(); buffer.writeBytes(elemB.duplicate()); assertTrue(set.remove(buffer)); assertFalse(set.contains(elemB)); assertEquals(0, set.size()); elemA.release(); elemB.release(); elemACopy.release(); elemBCopy.release(); }
Example 17
Source File: NetworkBuffer.java From flink with Apache License 2.0 | 5 votes |
@Override public ByteBuf readBytes(int length) { // copied from the one in netty 4.0.50 fixing the wrong allocator being used checkReadableBytes(length); if (length == 0) { return Unpooled.EMPTY_BUFFER; } ByteBuf buf = alloc().buffer(length, maxCapacity()); int readerIndex = readerIndex(); buf.writeBytes(this, readerIndex, length); readerIndex(readerIndex + length); return buf; }
Example 18
Source File: AbstractByteBufTest.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
@Test public void testHashCode() { ByteBuf elemA = buffer(15); ByteBuf elemB = directBuffer(15); elemA.writeBytes(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 }); elemB.writeBytes(new byte[] { 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9 }); Set<ByteBuf> set = new HashSet<ByteBuf>(); set.add(elemA); set.add(elemB); assertEquals(2, set.size()); ByteBuf elemACopy = elemA.copy(); assertTrue(set.contains(elemACopy)); ByteBuf elemBCopy = elemB.copy(); assertTrue(set.contains(elemBCopy)); buffer.clear(); buffer.writeBytes(elemA.duplicate()); assertTrue(set.remove(buffer)); assertFalse(set.contains(elemA)); assertEquals(1, set.size()); buffer.clear(); buffer.writeBytes(elemB.duplicate()); assertTrue(set.remove(buffer)); assertFalse(set.contains(elemB)); assertEquals(0, set.size()); elemA.release(); elemB.release(); elemACopy.release(); elemBCopy.release(); }
Example 19
Source File: AbstractByteBufTest.java From flink with Apache License 2.0 | 5 votes |
@Test public void testReadBytes() { ByteBuf buffer = newBuffer(8); byte[] bytes = new byte[8]; buffer.writeBytes(bytes); ByteBuf buffer2 = buffer.readBytes(4); assertSame(buffer.alloc(), buffer2.alloc()); assertEquals(4, buffer.readerIndex()); assertTrue(buffer.release()); assertEquals(0, buffer.refCnt()); assertTrue(buffer2.release()); assertEquals(0, buffer2.refCnt()); }
Example 20
Source File: AbstractByteBufTest.java From flink with Apache License 2.0 | 4 votes |
@Test public void testRetainedDuplicateAndRetainedSliceContentIsExpected() { ByteBuf buf = newBuffer(8).resetWriterIndex(); ByteBuf expected1 = newBuffer(6).resetWriterIndex(); ByteBuf expected2 = newBuffer(5).resetWriterIndex(); ByteBuf expected3 = newBuffer(4).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[] {5, 6, 7}); ByteBuf dup1 = buf.retainedDuplicate(); assertEquals(0, dup1.compareTo(buf)); assertEquals(0, dup1.compareTo(buf.slice())); // Simulate a handler that releases the original buffer, and propagates a slice. buf.release(); // Advance the reader index on the dup. dup1.readByte(); ByteBuf slice1 = dup1.retainedSlice(dup1.readerIndex(), 6); assertEquals(0, slice1.compareTo(expected1)); assertEquals(0, slice1.compareTo(slice1.duplicate())); // Advance the reader index on slice1. slice1.readByte(); ByteBuf dup2 = slice1.duplicate(); assertEquals(0, dup2.compareTo(slice1)); // Advance the reader index on dup2. dup2.readByte(); ByteBuf slice2 = dup2.retainedSlice(dup2.readerIndex() + 1, 3); assertEquals(0, slice2.compareTo(expected3)); assertEquals(0, slice2.compareTo(dup2.slice(dup2.readerIndex() + 1, 3))); // Cleanup the expected buffers used for testing. assertTrue(expected1.release()); assertTrue(expected2.release()); assertTrue(expected3.release()); slice2.release(); slice1.release(); assertEquals(slice2.refCnt(), dup2.refCnt()); assertEquals(dup2.refCnt(), slice1.refCnt()); // The handler is now done with the original slice assertTrue(dup1.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()); }