Java ByteBuffer from getByteBufferUtf8(final String string)

Here you can find the source of getByteBufferUtf8(final String string)

Description

Encodes the given string into a byte buffer using the UTF-8 charset, storing the result into a new byte array.

License

Open Source License

Parameter

Parameter Description
string the String to encode, may be <code>null</code>

Return

encoded bytes, or null if the input string was null

Declaration

public static ByteBuffer getByteBufferUtf8(final String string) throws Exception 

Method Source Code

//package com.java2s;
/*/*  ww  w  .  j a va2s.c  o  m*/
 * Copyright (c) 2015 SK PLANET. ALL Rights Reserved.
 *
 * Syrup Pay Jose Library
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.nio.ByteBuffer;

public class Main {
    /**
     * Encodes the given string into a byte buffer using the UTF-8 charset, storing the result into a new byte
     * array.
     *
     * @param string
     *            the String to encode, may be <code>null</code>
     * @return encoded bytes, or <code>null</code> if the input string was <code>null</code>
     * @see <a href="http://download.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
     * @see #getBytesUnchecked(String, String)
     * @since 1.11
     */
    public static ByteBuffer getByteBufferUtf8(final String string) throws Exception {
        return getByteBuffer(string, "UTF-8");
    }

    /**
     * Calls {@link String#getBytes(Charset)}
     *
     * @param string
     *            The string to encode (if null, return null).
     * @param charset
     *            The {@link Charset} to encode the <code>String</code>
     * @return the encoded bytes
     * @since 1.11
     */
    private static ByteBuffer getByteBuffer(final String string, final String charset) throws Exception {
        if (string == null) {
            return null;
        }
        return ByteBuffer.wrap(string.getBytes(charset));
    }

    /**
     * Calls {@link String#getBytes(Charset)}
     *
     * @param string
     *            The string to encode (if null, return null).
     * @param charset
     *            The {@link Charset} to encode the <code>String</code>
     * @return the encoded bytes
     */
    private static byte[] getBytes(final String string, final String charset) {
        if (string == null) {
            return null;
        }
        return string.getBytes();
    }
}

Related

  1. getByteBufferFromUTF8(String str)
  2. getByteBufferFromUUID(java.util.UUID uuid)
  3. getByteBufferOutputStream(final ByteBuffer buf)
  4. getByteBufferReadMethod(Class clazz)
  5. getByteBuffers(Collection newValue)
  6. toByteBuffer(BigDecimal decimal)