Java Convert via ByteBuffer toAsciiBytes(String s)

Here you can find the source of toAsciiBytes(String s)

Description

to Ascii Bytes

License

Open Source License

Declaration

public static ByteBuffer toAsciiBytes(String s) 

Method Source Code


//package com.java2s;
/*// ww w  . j  a  v  a  2  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 toAsciiBytes(String s) {
        return put(ByteBuffer.allocate(s.length()), s);
    }

    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. toArray(final byte[] byteArray, final int sizeLimit)
  2. toArray(int value)
  3. toArray(IntBuffer src, int[] dst, int offset)
  4. toArray(long length)
  5. toASCII(String str)
  6. toASCIIString(URI u)
  7. toAttributeValue(Object value)
  8. toBigDecimal(byte[] bytes, int scale)
  9. toBigInteger(byte[] bytes)