private ByteBuffer merge(List<ByteBuffer> pendingBuffers, ByteBuffer buffer) {
        if (!pendingBuffers.isEmpty()) {
            int total = 0;
            for (ByteBuffer pendingBuffer : pendingBuffers) {
                total += pendingBuffer.remaining();
            }
            total += buffer.remaining();

            // Merge elder buffer first.
            int offset = 0;
            byte[] array = new byte[total];
            for (ByteBuffer pendingBuffer : pendingBuffers) {
                System.arraycopy(pendingBuffer.array(), pendingBuffer.position(), array, offset,
                        pendingBuffer.remaining());
                offset += pendingBuffer.remaining();
            }

            // Merge the incoming buffer
            System.arraycopy(buffer.array(), buffer.position(), array, offset, buffer.remaining());

            buffer = ByteBuffer.wrap(array);
            // Clear all data.
            pendingBuffers.clear();
        }
        return buffer;
    }
Merge all the response pending buffers and a given buffer, and output a new buffer which
contains all data. The pending buffers will be clear after the merge action.

@param buffer A fresh packet buffer.
@return A new buffer.
netbare-core/src/main/java/com/github/megatronking/netbare/gateway/PendingIndexedInterceptor.java