Java UTF8 utf8ToString(byte[] data)

Here you can find the source of utf8ToString(byte[] data)

Description

Decodes the specified UTF-8 sequence to a string.

License

Open Source License

Parameter

Parameter Description
data an array that contains the UTF-8 sequence that will be decoded.

Exception

Parameter Description
IllegalArgumentException if<ul><li>the UTF-8 sequence is malformed, or</li><li>the UTF-8 sequence contains bytes that cannot be mapped to a character.</li></ul>
UnsupportedCharsetException if the Java implementation does not support the UTF-8 character encoding, which is requiredof all Java implementations.

Return

the string that results from decoding the input sequence.

Declaration


public static String utf8ToString(byte[] data) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.UnsupportedEncodingException;
import java.nio.charset.UnsupportedCharsetException;

public class Main {
    public static final String ENCODING_NAME_UTF8 = "UTF-8";

    /**/*from  w w  w. ja  v  a 2 s .  c  o  m*/
     * Decodes the specified UTF-8 sequence to a string.  An {@code IllegalArgumentException} is thrown if
     * the UTF-8 sequence is malformed or contains bytes that cannot be mapped to a character.  This method
     * is equivalent to {@code #utf8ToString( data, 0, data.length )}.
     *
     * @param  data  an array that contains the UTF-8 sequence that will be decoded.
     * @return the string that results from decoding the input sequence.
     * @throws IllegalArgumentException
     *           if
     *           <ul>
     *             <li>the UTF-8 sequence is malformed, or</li>
     *             <li>the UTF-8 sequence contains bytes that cannot be mapped to a character.</li>
     *           </ul>
     * @throws UnsupportedCharsetException
     *           if the Java implementation does not support the UTF-8 character encoding, which is required
     *           of all Java implementations.
     */

    public static String utf8ToString(byte[] data) {
        return utf8ToString(data, 0, data.length);
    }

    /**
     * Decodes the specified UTF-8 sequence to a string.  An {@code IllegalArgumentException} is thrown if
     * the UTF-8 sequence is malformed or if it contains bytes that cannot be mapped to a character.
     *
     * @param  data    an array that contains the UTF-8 sequence that will be decoded.
     * @param  offset  the offset to {@code data} at which the input sequence begins.
     * @param  length  the length of the input sequence.
     * @return the string that results from decoding the input sequence.
     * @throws UnsupportedCharsetException
     *           if the Java implementation does not support the UTF-8 character encoding, which is required
     *           of all Java implementations.
     */

    public static String utf8ToString(byte[] data, int offset, int length) {
        try {
            return new String(data, offset, length, ENCODING_NAME_UTF8);
        } catch (UnsupportedEncodingException e) {
            throw new UnsupportedCharsetException(ENCODING_NAME_UTF8);
        }
    }
}

Related

  1. utf8String(String... strs)
  2. utf8StringLength(final CharSequence sequence)
  3. utf8StringSizeInBytes(String s)
  4. utf8StringToByteArray(String dataString)
  5. utf8StringToBytes(String string)
  6. utf8URLDecode(String input)
  7. utf8urldecode(String text)
  8. Utf8URLencode(String text)