Java Byte Array to String by Charset decode(final byte[] pData, final int pOffset, final int pLength, final String pCharset)

Here you can find the source of decode(final byte[] pData, final int pOffset, final int pLength, final String pCharset)

Description

Constructs a new String by decoding the specified sub array of bytes using the specified charset.

License

Open Source License

Parameter

Parameter Description
pData the bytes to be decoded to characters
pOffset the index of the first byte to decode
pLength the number of bytes to decode
pCharset the name of a supported character set

Exception

Parameter Description
UnsupportedCharsetException an exception

Return

a newly created string.

Declaration

public static String decode(final byte[] pData, final int pOffset, final int pLength, final String pCharset) 

Method Source Code

//package com.java2s;

import java.io.UnsupportedEncodingException;
import java.nio.charset.UnsupportedCharsetException;

public class Main {
    /**//from w  w  w  .  j  a v  a 2 s  .  c  om
     * Constructs a new {@link String} by decoding the specified sub array of bytes using the specified charset.
     * Replacement for {@link String#String(byte[], int, int, String) new String(byte[], int, int, String)}, that does
     * not throw the checked {@link UnsupportedEncodingException},
     * but instead the unchecked {@link UnsupportedCharsetException} if the character set is not supported.
     *
     * @param pData the bytes to be decoded to characters
     * @param pOffset the index of the first byte to decode
     * @param pLength the number of bytes to decode
     * @param pCharset the name of a supported character set
     * @return a newly created string.
     * @throws UnsupportedCharsetException
     *
     * @see String#String(byte[], int, int, String)
     */
    public static String decode(final byte[] pData, final int pOffset, final int pLength, final String pCharset) {
        try {
            return new String(pData, pOffset, pLength, pCharset);
        } catch (UnsupportedEncodingException e) {
            throw new UnsupportedCharsetException(pCharset);
        }
    }
}

Related

  1. convertToString(byte[] bytes, Charset charset)
  2. convertToString(final byte[] bytes, final Charset charset)
  3. decode(CharsetDecoder decoder, byte[] in)
  4. decode(final byte[] binaryValue, final Charset charset)
  5. decode(final byte[] binaryValue, final Charset charset)
  6. decodeBytes(byte[] data, Charset charset, boolean report)
  7. decodeCHARSET(byte[] bytes, Charset charset)
  8. decodeHelper(byte[] byteArray, int numberOfBytes, java.nio.charset.Charset charset)
  9. decodeOrDefault(byte[] data, Charset charset, String defaultValue)