Java ByteBuffer to String string(ByteBuffer bytes)

Here you can find the source of string(ByteBuffer bytes)

Description

string

License

Apache License

Declaration

public static String string(ByteBuffer bytes) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 Apigee Corporation/*from w  ww. ja  va 2  s. co m*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

public class Main {
    /**
     * 
     */
    public static final String UTF8_ENCODING = "UTF-8";

    /**
     * @param obj
     * @return
     */
    public static String string(Object obj) {
        if (obj instanceof String) {
            return (String) obj;
        } else if (obj instanceof byte[]) {
            return string((byte[]) obj);
        } else if (obj instanceof ByteBuffer) {
            return string((ByteBuffer) obj);
        } else if (obj != null) {
            return obj.toString();
        }
        return null;
    }

    /**
     * @param bytes
     * @return
     */
    public static String string(byte[] bytes) {
        if (bytes == null) {
            return null;
        }
        return string(bytes, 0, bytes.length, UTF8_ENCODING);
    }

    public static String string(ByteBuffer bytes) {
        if (bytes == null) {
            return null;
        }
        return string(bytes.array(), bytes.arrayOffset() + bytes.position(), bytes.remaining(), UTF8_ENCODING);
    }

    /**
     * @param bytes
     * @param offset
     * @param length
     * @return
     */
    public static String string(byte[] bytes, int offset, int length) {
        return string(bytes, offset, length, UTF8_ENCODING);
    }

    /**
     * @param bytes
     * @param offset
     * @param length
     * @param encoding
     * @return
     */
    public static String string(byte[] bytes, int offset, int length, String encoding) {

        if (length <= 0) {
            return "";
        }

        if (bytes == null) {
            return "";
        }

        try {
            return new String(bytes, offset, length, encoding);
        } catch (UnsupportedEncodingException e) {
            // logger.log(Level.SEVERE, "UnsupportedEncodingException ", e);
            throw new RuntimeException(e);
        }
    }
}

Related

  1. readString(final ByteBuffer buffer, final String encoding)
  2. string(ByteBuffer b, Charset charset)
  3. string(ByteBuffer buffer)
  4. string(ByteBuffer buffer)
  5. string(ByteBuffer buffer, int position, int length, Charset charset)
  6. toString(@Nonnull ByteBuffer buf, int len)
  7. toString(ByteBuffer b, String separator)
  8. toString(ByteBuffer bb)
  9. toString(ByteBuffer buf)