Java Code Examples for org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf#readByte()
The following examples show how to use
org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf#readByte() .
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: ReadOnlySlicedBufferTest.java From flink with Apache License 2.0 | 6 votes |
private static void assertReadableBytes(Buffer actualBuffer, int... expectedBytes) { ByteBuffer actualBytesBuffer = actualBuffer.getNioBufferReadable(); int[] actual = new int[actualBytesBuffer.limit()]; for (int i = 0; i < actual.length; ++i) { actual[i] = actualBytesBuffer.get(); } assertArrayEquals(expectedBytes, actual); // verify absolutely positioned read method: ByteBuf buffer = (ByteBuf) actualBuffer; for (int i = 0; i < buffer.readableBytes(); ++i) { actual[i] = buffer.getByte(buffer.readerIndex() + i); } assertArrayEquals(expectedBytes, actual); // verify relatively positioned read method: for (int i = 0; i < buffer.readableBytes(); ++i) { actual[i] = buffer.readByte(); } assertArrayEquals(expectedBytes, actual); }
Example 2
Source File: NettyMessageClientDecoderDelegate.java From flink with Apache License 2.0 | 6 votes |
private void decodeFrameHeader(ByteBuf data) { ByteBuf fullFrameHeaderBuf = ByteBufUtils.accumulate( frameHeaderBuffer, data, FRAME_HEADER_LENGTH, frameHeaderBuffer.readableBytes()); if (fullFrameHeaderBuf != null) { int messageAndFrameLength = fullFrameHeaderBuf.readInt(); checkState(messageAndFrameLength >= 0, "The length field of current message must be non-negative"); int magicNumber = fullFrameHeaderBuf.readInt(); checkState(magicNumber == MAGIC_NUMBER, "Network stream corrupted: received incorrect magic number."); int msgId = fullFrameHeaderBuf.readByte(); if (msgId == NettyMessage.BufferResponse.ID) { currentDecoder = bufferResponseDecoder; } else { currentDecoder = nonBufferResponseDecoder; } currentDecoder.onNewMessageReceived(msgId, messageAndFrameLength - FRAME_HEADER_LENGTH); } }
Example 3
Source File: ReadOnlySlicedBufferTest.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
private static void assertReadableBytes(Buffer actualBuffer, int... expectedBytes) { ByteBuffer actualBytesBuffer = actualBuffer.getNioBufferReadable(); int[] actual = new int[actualBytesBuffer.limit()]; for (int i = 0; i < actual.length; ++i) { actual[i] = actualBytesBuffer.get(); } assertArrayEquals(expectedBytes, actual); // verify absolutely positioned read method: ByteBuf buffer = (ByteBuf) actualBuffer; for (int i = 0; i < buffer.readableBytes(); ++i) { actual[i] = buffer.getByte(buffer.readerIndex() + i); } assertArrayEquals(expectedBytes, actual); // verify relatively positioned read method: for (int i = 0; i < buffer.readableBytes(); ++i) { actual[i] = buffer.readByte(); } assertArrayEquals(expectedBytes, actual); }
Example 4
Source File: ReadOnlySlicedBufferTest.java From flink with Apache License 2.0 | 6 votes |
private static void assertReadableBytes(Buffer actualBuffer, int... expectedBytes) { ByteBuffer actualBytesBuffer = actualBuffer.getNioBufferReadable(); int[] actual = new int[actualBytesBuffer.limit()]; for (int i = 0; i < actual.length; ++i) { actual[i] = actualBytesBuffer.get(); } assertArrayEquals(expectedBytes, actual); // verify absolutely positioned read method: ByteBuf buffer = (ByteBuf) actualBuffer; for (int i = 0; i < buffer.readableBytes(); ++i) { actual[i] = buffer.getByte(buffer.readerIndex() + i); } assertArrayEquals(expectedBytes, actual); // verify relatively positioned read method: for (int i = 0; i < buffer.readableBytes(); ++i) { actual[i] = buffer.readByte(); } assertArrayEquals(expectedBytes, actual); }
Example 5
Source File: AbstractByteBufTest.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
@Test(expected = IndexOutOfBoundsException.class) public void readByteThrowsIndexOutOfBoundsException() { final ByteBuf buffer = newBuffer(8); try { buffer.writeByte(0); assertEquals((byte) 0, buffer.readByte()); buffer.readByte(); } finally { buffer.release(); } }
Example 6
Source File: AbstractByteBufTest.java From flink with Apache License 2.0 | 5 votes |
@Test(expected = IndexOutOfBoundsException.class) public void readByteThrowsIndexOutOfBoundsException() { final ByteBuf buffer = newBuffer(8); try { buffer.writeByte(0); assertEquals((byte) 0, buffer.readByte()); buffer.readByte(); } finally { buffer.release(); } }
Example 7
Source File: NettyMessage.java From flink with Apache License 2.0 | 5 votes |
/** * Parses the message header part and composes a new BufferResponse with an empty data buffer. The * data buffer will be filled in later. * * @param messageHeader the serialized message header. * @param bufferAllocator the allocator for network buffer. * @return a BufferResponse object with the header parsed and the data buffer to fill in later. The * data buffer will be null if the target channel has been released or the buffer size is 0. */ static BufferResponse readFrom(ByteBuf messageHeader, NetworkBufferAllocator bufferAllocator) { InputChannelID receiverId = InputChannelID.fromByteBuf(messageHeader); int sequenceNumber = messageHeader.readInt(); int backlog = messageHeader.readInt(); Buffer.DataType dataType = Buffer.DataType.values()[messageHeader.readByte()]; boolean isCompressed = messageHeader.readBoolean(); int size = messageHeader.readInt(); Buffer dataBuffer = null; if (size != 0) { if (dataType.isBuffer()) { dataBuffer = bufferAllocator.allocatePooledNetworkBuffer(receiverId); } else { dataBuffer = bufferAllocator.allocateUnPooledNetworkBuffer(size, dataType); } } if (dataBuffer != null) { dataBuffer.setCompressed(isCompressed); } return new BufferResponse( dataBuffer, dataType, isCompressed, sequenceNumber, receiverId, backlog, size); }
Example 8
Source File: AbstractByteBufTest.java From flink with Apache License 2.0 | 5 votes |
@Test(expected = IndexOutOfBoundsException.class) public void readByteThrowsIndexOutOfBoundsException() { final ByteBuf buffer = newBuffer(8); try { buffer.writeByte(0); assertEquals((byte) 0, buffer.readByte()); buffer.readByte(); } finally { buffer.release(); } }
Example 9
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()); }
Example 10
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()); }
Example 11
Source File: NettyMessage.java From flink with Apache License 2.0 | 4 votes |
@Override protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { ByteBuf msg = (ByteBuf) super.decode(ctx, in); if (msg == null) { return null; } try { int magicNumber = msg.readInt(); if (magicNumber != MAGIC_NUMBER) { throw new IllegalStateException( "Network stream corrupted: received incorrect magic number."); } byte msgId = msg.readByte(); final NettyMessage decodedMsg; switch (msgId) { case PartitionRequest.ID: decodedMsg = PartitionRequest.readFrom(msg); break; case TaskEventRequest.ID: decodedMsg = TaskEventRequest.readFrom(msg, getClass().getClassLoader()); break; case CancelPartitionRequest.ID: decodedMsg = CancelPartitionRequest.readFrom(msg); break; case CloseRequest.ID: decodedMsg = CloseRequest.readFrom(msg); break; case AddCredit.ID: decodedMsg = AddCredit.readFrom(msg); break; case ResumeConsumption.ID: decodedMsg = ResumeConsumption.readFrom(msg); break; default: throw new ProtocolException( "Received unknown message from producer: " + msg); } return decodedMsg; } finally { // ByteToMessageDecoder cleanup (only the BufferResponse holds on to the decoded // msg but already retain()s the buffer once) msg.release(); } }
Example 12
Source File: NettyMessage.java From flink with Apache License 2.0 | 4 votes |
@Override protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { ByteBuf msg = (ByteBuf) super.decode(ctx, in); if (msg == null) { return null; } try { int magicNumber = msg.readInt(); if (magicNumber != MAGIC_NUMBER) { throw new IllegalStateException( "Network stream corrupted: received incorrect magic number."); } byte msgId = msg.readByte(); final NettyMessage decodedMsg; switch (msgId) { case BufferResponse.ID: decodedMsg = BufferResponse.readFrom(msg); break; case PartitionRequest.ID: decodedMsg = PartitionRequest.readFrom(msg); break; case TaskEventRequest.ID: decodedMsg = TaskEventRequest.readFrom(msg, getClass().getClassLoader()); break; case ErrorResponse.ID: decodedMsg = ErrorResponse.readFrom(msg); break; case CancelPartitionRequest.ID: decodedMsg = CancelPartitionRequest.readFrom(msg); break; case CloseRequest.ID: decodedMsg = CloseRequest.readFrom(msg); break; case AddCredit.ID: decodedMsg = AddCredit.readFrom(msg); break; default: throw new ProtocolException( "Received unknown message from producer: " + msg); } return decodedMsg; } finally { // ByteToMessageDecoder cleanup (only the BufferResponse holds on to the decoded // msg but already retain()s the buffer once) msg.release(); } }
Example 13
Source File: AbstractByteBufTest.java From Flink-CEPplus 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()); }
Example 14
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 15
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()); }
Example 16
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()); }
Example 17
Source File: NettyMessage.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
@Override protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { ByteBuf msg = (ByteBuf) super.decode(ctx, in); if (msg == null) { return null; } try { int magicNumber = msg.readInt(); if (magicNumber != MAGIC_NUMBER) { throw new IllegalStateException( "Network stream corrupted: received incorrect magic number."); } byte msgId = msg.readByte(); final NettyMessage decodedMsg; switch (msgId) { case BufferResponse.ID: decodedMsg = BufferResponse.readFrom(msg); break; case PartitionRequest.ID: decodedMsg = PartitionRequest.readFrom(msg); break; case TaskEventRequest.ID: decodedMsg = TaskEventRequest.readFrom(msg, getClass().getClassLoader()); break; case ErrorResponse.ID: decodedMsg = ErrorResponse.readFrom(msg); break; case CancelPartitionRequest.ID: decodedMsg = CancelPartitionRequest.readFrom(msg); break; case CloseRequest.ID: decodedMsg = CloseRequest.readFrom(msg); break; case AddCredit.ID: decodedMsg = AddCredit.readFrom(msg); break; default: throw new ProtocolException( "Received unknown message from producer: " + msg); } return decodedMsg; } finally { // ByteToMessageDecoder cleanup (only the BufferResponse holds on to the decoded // msg but already retain()s the buffer once) msg.release(); } }