Rather than do this, you should assemble the bytes mathematically.  ByteBuffers are expensive.