title: Accumulating Netty direct IO buffer to CompositeByteBuf
body: <p>We are migrating from Servlet API 2.5 (thread per request) in our application to Netty. One of cases is using blocking-style <em>ini</em> file parser which we had for many years. Current approach is accumulating incoming <code>ByteBuf</code> into <code>CompositeByteBuf</code> and feeding it to a parser after wrapping with <code>ByteBufInputStream</code>.</p>
  
  <p>In real application we are using HTTP and <em>ini</em> is sent to a server as HTTP request body. But in snippet below it is assumed that all inbound content is transferred file.</p>
  
  <pre><code>class AccumulatingChannelHandler extends ChannelInboundHandlerAdapter {
    final BlockingIniFileParser parser = new BlockingIniFileParser();
    CompositeByteBuf accumulator;
  
    @Override
    public void channelActive(ChannelHandlerContext ctx) {
      accumulator = ctx.alloc().compositeBuffer(Integer.MAX_VALUE);
    }
  
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
      ByteBuf ioBuffer = (ByteBuf) msg;
      accumulator.addComponent(true, ioBuffer);
    }
  
    @Override
    public void channelInactive(ChannelHandlerContext ctx) {
      IniFile iniFile = parser.parse(new ByteBufInputStream(accumulator));
      accumulator.release();
      ByteBuf result = process(iniFile);
      ctx.writeAndFlush(result);
      ctx.close();
    }
  
    private ByteBuf process(IniFile iniFile) {...}
  }
  
  class BlockingIniFileParser {
    IniFile parse(InputStream in) {...}
  }
  
  interface IniFile {
    String getSetting(String section, String entry);
  }
  </code></pre>
  
  <p>By default pooled direct buffers are coming to <code>channelRead</code> method. And with such strategy we risk to get uncontrollable consumption of direct memory. So, I would like to understand:</p>
  
  <ol>
  <li>Is it rational to accumulate IO buffers in such fashion? </li>
  <li>Is there any best practice for integrating Netty IO with blocking parsers?</li>
  <li>What is the best practice for parsing input (in some structured format) with Netty?</li>
  </ol>