Here you can find the source of utf8ToString(byte[] data)
Parameter | Description |
---|---|
data | an array that contains the UTF-8 sequence that will be decoded. |
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. |
public static String utf8ToString(byte[] data)
//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); } } }