Java ByteBuffer Put put(ByteBuffer bb, String s)

Here you can find the source of put(ByteBuffer bb, String s)

Description

put

License

Open Source License

Declaration

public static ByteBuffer put(ByteBuffer bb, String s) 

Method Source Code


//package com.java2s;
/*/*  w ww  .j  a v  a2 s .c  o  m*/
 * ***** 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;

    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;
    }

    /**
     * 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);
    }
}

Related

  1. inputStreamToByteBuffer(InputStream is)
  2. makeInputStream(final ByteBuffer buffer)
  3. moveBufferToStream(OutputStream out, ByteBuffer in, int length)
  4. outputBuffer(ByteBuffer buffer)
  5. put(ByteBuffer bb, byte[] bytes, int from)
  6. put(ByteBuffer bbuf, byte[] post)
  7. put(ByteBuffer buf, String s, String charsetName)
  8. put(ByteBuffer dst, ByteBuffer src, int transferLimit)
  9. put(ByteBuffer from, ByteBuffer to)