public void accumulate()

in src/main/java/com/twitter/whiskey/net/BodyFutureImpl.java [33:55]


    public void accumulate(ByteBuffer element) {

        if (!element.hasRemaining()) return;
        if (body == null) {
            body = ByteBuffer.allocate(Math.max(expectedLength, element.remaining()));
            Platform.LOGGER.debug("allocated " + body.capacity());
        }

        if (body.remaining() < element.remaining()) {
            int required = body.position() + element.remaining();
            // Allocate nearest power of 2 higher than the total required space
            assert(required < Integer.MAX_VALUE >> 1);
            ByteBuffer expanded = ByteBuffer.allocate(Integer.highestOneBit(required) << 1);
            body.flip();
            expanded.put(body);
            expanded.put(element);
            body = expanded;
            Platform.LOGGER.debug("grew buffer to " + body.capacity());
        } else {
            body.put(element);
        }
        boundaries.add(body.position());
    }