Java Byte Array to String by Charset convertToString(byte[] bytes, Charset charset)

Here you can find the source of convertToString(byte[] bytes, Charset charset)

Description

convert To String

License

Apache License

Declaration

public static String convertToString(byte[] bytes, Charset charset) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.nio.charset.*;

public class Main {
    public static String convertToString(byte[] bytes, Charset charset) {
        String ret = new String(bytes, charset);
        if ((bytes[0] == 0xEF - 256) && (bytes[1] == 0xBB - 256)
                && (bytes[2] == 0xBF - 256)) {
            ret = ret.substring(1);/*from   w  ww  .j  a  va 2 s. c  o m*/
        } else if ((bytes[0] == 0xFE - 256) && (bytes[1] == 0xFF - 256)) {
            ret = ret.substring(1);
        } else if ((bytes[0] == 0xFF - 256) && (bytes[1] == 0xFE - 256)) {
            ret = ret.substring(1);
        }
        return ret;
    }
}

Related

  1. charSequence2Bytes(CharSequence sb, Charset charset)
  2. charSetForBOM(byte[] buffer, int offset)
  3. convertCharset(byte[] content, Charset fromCharset, Charset toCharset)
  4. convertToBytes(final String str, final Charset charset)
  5. convertToChars(final byte[] bytes, final Charset charset)
  6. convertToString(final byte[] bytes, final Charset charset)
  7. decode(CharsetDecoder decoder, byte[] in)
  8. decode(final byte[] binaryValue, final Charset charset)
  9. decode(final byte[] binaryValue, final Charset charset)