BrpcHttpObjectDecoder extends HttpObjectDecoder { public Object decode(ChannelHandlerContext ctx, ByteBuf in) { BrpcHttpObjectAggregator httpObjectAggregator = new BrpcHttpObjectAggregator(10 * 1024 * 1024); this.reset(); List httpParts = new LinkedList(); List httpResult = new LinkedList(); try { while (in.isReadable()) { int outSize = httpParts.size(); if (outSize > 0) { for (int i = 0; i < outSize; i++) { httpObjectAggregator.aggregate(ctx, httpParts.get(i), httpResult); if (httpResult.size() > 0) { return httpResult.get(0); } } httpParts.clear(); if (ctx.isRemoved()) { break; } outSize = 0; } int oldInputLength = in.readableBytes(); decode(ctx, in, httpParts); if (ctx.isRemoved()) { break; } if (outSize == httpParts.size()) { if (oldInputLength == in.readableBytes()) { break; } else { continue; } } if (oldInputLength == in.readableBytes()) { throw new DecoderException( StringUtil.simpleClassName(getClass()) + ".decode() did not read anything but decoded a message."); } if (isSingleDecode()) { break; } } int outSize = httpParts.size(); if (outSize > 0) { for (int i = 0; i < outSize; i++) { httpObjectAggregator.aggregate(ctx, httpParts.get(i), httpResult); if (httpResult.size() > 0) { return httpResult.get(0); } } } httpObjectAggregator.abort(); return null; } catch (DecoderException e) { throw e; } catch (Exception cause) { throw new DecoderException(cause); } } private  BrpcHttpObjectDecoder(boolean isDecodingRequest); static BrpcHttpObjectDecoder getDecoder(boolean isDecodingRequest); Object decode(ChannelHandlerContext ctx, ByteBuf in);  }
@Test public void testDecode() throws Exception { BrpcHttpObjectDecoder decoder = BrpcHttpObjectDecoder.getDecoder(true); ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class); when(ctx.alloc()).thenReturn(alloc); ByteBuf buf = alloc.buffer(1024); String[] testRequest = new String[]{ "GET / HTTP/1.1", "Host: localhost", "Content-Length: 10", "", "1234567890" }; buf.writeBytes(StringUtils.join(testRequest, "\n\r").getBytes(Charset.forName("UTF-8"))); Object message = decoder.decode(ctx, buf); assertThat(message).isNotNull(); ReferenceCountUtil.release(buf); ReferenceCountUtil.release(message); }