Java UTF8 Convert To fromUTF8(byte[] bytes)

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

Description

Converts the passed byte array to a string, using UTF-8 encoding, and turning the checked exception (which should never happen) into a runtime exception.

License

Apache License

Declaration

public static String fromUTF8(byte[] bytes) 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.io.UnsupportedEncodingException;

public class Main {
    /**//from   ww w  .  j  a v a  2s.c  om
     *  Converts the passed byte array to a string, using UTF-8 encoding, and
     *  turning the checked exception (which should never happen) into a runtime
     *  exception.
     *  <p>
     *  If passed <code>null</code>, returns an empty string.
     */
    public static String fromUTF8(byte[] bytes) {
        try {
            if (bytes == null)
                return "";
            return new String(bytes, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("UTF-8 not supported", e);
        }
    }
}

Related

  1. fromUTF8(byte[] b)
  2. fromUTF8(byte[] bytes)
  3. fromUTF8(byte[] bytes)
  4. fromUTF8Bytes(byte[] bytes)
  5. fromUTF8Bytes(byte[] bytes)
  6. getBytesUtf8(String str)