protected void checkExpand(final int size) {
        final int remain = remaining();
        if (remain >= size) {
            return;
        }
        int length = buffer.length;
        if (length == 0) {
            length = 1;
        }

        // after compute the buffer size, allocate it once for ado.
        final int expandedBufferSize = computeExpandedBufferSize(size, length, remain);
        // allocate buffer
        final byte[] expandedBuffer = new byte[expandedBufferSize];
        System.arraycopy(buffer, 0, expandedBuffer, 0, buffer.length);
        buffer = expandedBuffer;
    }
Buffer that can be expanded automatically
@author emeroad
commons-buffer/src/main/java/com/navercorp/pinpoint/common/buffer/AutomaticBuffer.java