Java Byte Array to String by Charset getContentAsString(byte[] content, Charset charset)

Here you can find the source of getContentAsString(byte[] content, Charset charset)

Description

Converts the byte array into a String based on the specified charset.

License

Apache License

Parameter

Parameter Description
content bytes to convert to a String
charset the character set of the content

Exception

Parameter Description
IllegalArgumentException if charset is null

Return

String containing the converted content

Declaration

public static String getContentAsString(byte[] content, Charset charset) 

Method Source Code

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

import java.nio.charset.Charset;

public class Main {
    /**//w  w  w .j av  a 2 s  .  c  o m
     * Converts the byte array into a String based on the specified charset. The charset cannot be null.
     *
     * @param content bytes to convert to a String
     * @param charset the character set of the content
     * @return String containing the converted content
     * @throws IllegalArgumentException if charset is null
     */
    public static String getContentAsString(byte[] content, Charset charset) {
        if (charset == null) {
            throw new IllegalArgumentException("Charset cannot be null");
        }

        return new String(content, charset);
    }
}

Related

  1. getBytes(String string, Charset charset)
  2. getBytesByCharset(String str, Charset charset)
  3. getBytesUnchecked(String string, String charsetName)
  4. getBytesUnchecked(String string, String charsetName)
  5. getChars(byte[] data, String charset)
  6. getMaxBytes(String string, int maxBytes, Charset charSet)
  7. getNumberOfBytesPerCharacter(final String charsetName)
  8. getPrefixWithMaxBytes(String string, int maxBytes, Charset charSet)
  9. getRowId(final byte[] rowId, final Charset charset, final boolean base64encodeValues)