Java UTF8 From getUTF8String(byte[] bytes)

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

Description

Converts string into an UTF8 String and handles the unlikely case where UTF-8 is not supported.

License

LGPL

Parameter

Parameter Description
bytes the <tt>byte</tt> array that we'd like to convert into a <tt>String</tt>.

Return

the UTF-8 String.

Declaration

public static String getUTF8String(byte[] bytes) 

Method Source Code


//package com.java2s;
/*//  w  w  w .  j a v  a2  s .  co m
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

import java.io.*;

public class Main {
    /**
     * Converts <tt>string</tt> into an UTF8 <tt>String</tt> and handles the
     * unlikely case where UTF-8 is not supported.
     *
     * @param bytes the <tt>byte</tt> array that we'd like to convert into a
     * <tt>String</tt>.
     *
     * @return the UTF-8 <tt>String</tt>.
     */
    public static String getUTF8String(byte[] bytes) {
        try {
            return new String(bytes, "UTF-8");
        } catch (UnsupportedEncodingException exc) {
            // shouldn't happen. UTF-8 is always supported, anyways ... if
            //this happens, we'll cheat
            return new String(bytes);
        }
    }
}

Related

  1. getUTF8Bytes(String str)
  2. getUtf8Bytes(String str)
  3. getUtf8Bytes(String str)
  4. getUTF8Stream(String s)
  5. getUTF8String(byte[] bytes)
  6. getUTF8String(String input)