Java UTF8 asStringUTF8(byte[] bytes)

Here you can find the source of asStringUTF8(byte[] bytes)

Description

Return the String representation of the byte array.

License

Apache License

Parameter

Parameter Description
bytes the byte array

Return

its string representation

Declaration

public static String asStringUTF8(byte[] bytes) 

Method Source Code

//package com.java2s;
/**//from  www .j  av  a2s. co  m
 *
 * jerry - Common Java Functionality
 * Copyright (c) 2012-2015, Sandeep Gupta
 * 
 * http://sangupta.com/projects/jerry
 * 
 * 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.nio.charset.Charset;

public class Main {
    /**
     * UTF-8 charset object
     */
    public static final Charset CHARSET_UTF8 = Charset.forName("UTF8");
    /**
     * An empty string object containing nothing.
     */
    public static final String EMPTY_STRING = "";

    /**
     * Return the {@link String} representation of the byte array. A
     * <code>null</code> byte array is converted back to <code>null</code>, an
     * empty array to an empty string or the bytes to the actual string in UTF-8
     * encoding.
     * 
     * @param bytes
     *            the byte array
     * 
     * @return its string representation
     */
    public static String asStringUTF8(byte[] bytes) {
        if (bytes == null) {
            return null;
        }

        if (bytes.length == 0) {
            return EMPTY_STRING;
        }

        return new String(bytes, CHARSET_UTF8);
    }
}

Related

  1. addLineIds(String inFile, String outFile)
  2. appendToFile(String outputFile, String contents)
  3. asUTF16BEEncoded(String basicString)
  4. asUtf8(byte[] bytes)
  5. asUTF8(InputStream in)
  6. asUTF8bytes(String s)