I realize you are just integrating another library, but a general comment on this method is that if the base implementation actually worked directly on top of a ByteBuffer instead of requiring being copied into a byte[], built, written to a byte[] and then out to the ByteBuffer, it'll reduce overhead and make things generally faster.