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

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

Description

to String

License

Apache License

Declaration

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

Method Source Code


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

import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

public class Main {

    public static String toString(byte[] bytes, Charset charset) {
        assertNotNull(charset, "charset");
        if (bytes == null) {
            return null;
        }/*from www.  ja va 2  s .c  o  m*/
        return charset.decode(ByteBuffer.wrap(bytes)).toString();
    }

    public static String toString(final Object value) {
        if (value instanceof BigDecimal) {
            return ((BigDecimal) value).toPlainString();
        } else {
            return value.toString();
        }
    }

    private static void assertNotNull(Object argumentThatMustNotBeNull, String argumentName) {
        if (argumentThatMustNotBeNull == null) {
            throw new IllegalArgumentException(argumentName + " must not be null.");
        }
    }
}

Related

  1. toChars(byte[] b, String charset)
  2. toCharSet(byte[] bytes, String fromCharsetName, String toCharsetName)
  3. toDebugString(final byte[] bytes, final int len, final Charset charset)
  4. toEncodedString(byte[] bytes, Charset charset)
  5. toString(byte[] b, String charset)
  6. toString(byte[] bytes, String charsetName)
  7. toString(byte[] data, int from, int length, Charset charset)
  8. toString(final byte[] bytes, final String charsetName)
  9. toUnicode(byte[] isoStr, String charset)