Take the 2-minute tour ×

I previously asked this question about using netty to write messages to network efficiently. Now there is even more pressing problem on the reading side of it. Atleast when writing a message one has the control of the byte buffers used as intermediate storage ( and so one can code to reuse the buffers ) , but on the reading side of Netty ( ie an Upstream event ) , the OioWorker and NioWorker classes completely make it impossible to plugin the byte[] used for reading from network's InputStream. For example the run() method of OioWorker is called for reading bytes. This method creates a new byte[] .

How can I tell Netty to stop doing this and maybe reuse some ChannelBuffer ? ( I know there will be threading issues in that - I am prepared to handle those - infact I am running the whole read operation on single thread )

share|improve this question
Could you show some code (or at least some more details) as to how you are reading and configured? For example your pipeline initialization, what allocators you are using, and your user level read callbacks? What version of Netty are you using and have you seen the PooledByteBufAllocator? –  Scott Mitchell Dec 10 at 4:03

1 Answer 1

Using java ThreadLocal<T>.

  private static final ThreadLocal<ChannelBuffer> headBuffer = new ThreadLocal<ChannelBuffer>() {
    protected ChannelBuffer initialValue() {
      return ChannelBuffers.buffer(DEFAULT_HEAD_LENGTH);

share|improve this answer

Your Answer


