Java ByteBuffer Expand expand(ByteBuffer bb, int minSize, int maxSize)

Here you can find the source of expand(ByteBuffer bb, int minSize, int maxSize)

Description

Ensure that specified buffer has at least enough capacity to accommodate 'minSize' additional bytes, but not more than 'maxSize' additional bytes.

License

Open Source License

Parameter

Parameter Description
bb the ByteBuffer to expand, or null to create a new one
minSize minimum additional capacity for resulting byte buffer
maxSize maximum additional capacity, or -1 for no maximum

Return

the resulting, possible expanded, ByteBuffer

Declaration

public static ByteBuffer expand(ByteBuffer bb, int minSize, int maxSize) 

Method Source Code


//package com.java2s;
/*/*from   ww w .j a  v  a 2s  . c om*/
 * ***** BEGIN LICENSE BLOCK *****
 * Zimbra Collaboration Suite Server
 * Copyright (C) 2007, 2009, 2010, 2011, 2013, 2014, 2016 Synacor, Inc.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software Foundation,
 * version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with this program.
 * If not, see <https://www.gnu.org/licenses/>.
 * ***** END LICENSE BLOCK *****
 */

import java.nio.ByteBuffer;

public class Main {
    public static final int INITIAL_CAPACITY = 32;

    /**
     * Ensure that specified buffer has at least enough capacity to accommodate 'minSize' additional bytes, but not more
     * than 'maxSize' additional bytes. If 'maxSize' is 0, then there is no limit.
     *
     * @param bb the ByteBuffer to expand, or null to create a new one
     * @param minSize minimum additional capacity for resulting byte buffer
     * @param maxSize maximum additional capacity, or -1 for no maximum
     * @return the resulting, possible expanded, ByteBuffer
     */
    public static ByteBuffer expand(ByteBuffer bb, int minSize, int maxSize) {
        if (maxSize != -1 && maxSize < minSize) {
            throw new IllegalArgumentException("maxSize < minSize");
        }
        if (bb == null) {
            int size = Math.max(minSize, INITIAL_CAPACITY);
            if (maxSize != -1 && size > maxSize)
                size = maxSize;
            return ByteBuffer.allocate(size);
        }
        if (bb.remaining() >= minSize)
            return bb;
        int capacity = Math.max((bb.capacity() * 3) / 2 + 1, bb.position() + minSize);
        if (maxSize != -1) {
            capacity = Math.max(capacity, bb.position() + maxSize);
        }
        ByteBuffer tmp = ByteBuffer.allocate(capacity);
        bb.flip();
        return tmp.put(bb);
    }

    public static ByteBuffer expand(ByteBuffer bb, int minSize) {
        return expand(bb, minSize, -1);
    }

    public static ByteBuffer put(ByteBuffer bb, String s) {
        bb = expand(bb, s.length());
        for (int i = 0; i < s.length(); i++) {
            bb.put(i, (byte) s.charAt(i));
        }
        return bb;
    }
}

Related

  1. expandBuffer(ByteBuffer existingBuffer, int additionalRequired)