Java Byte Array to String by Charset decode(final byte[] binaryValue, final Charset charset)

Here you can find the source of decode(final byte[] binaryValue, final Charset charset)

Description

decode

License

Open Source License

Declaration

public static String decode(final byte[] binaryValue, final Charset charset) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  ww w.  ja  v  a2  s .c o m*/
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

public class Main {
    public static String decode(final byte[] binaryValue, final Charset charset) {
        final CharsetDecoder decoder = charset.newDecoder();
        try {
            final ByteBuffer bbuf = ByteBuffer.wrap(binaryValue);
            final CharBuffer cbuf = decoder.decode(bbuf);
            return cbuf.toString();
        } catch (final CharacterCodingException e) {
            return null;
        }
    }
}

Related

  1. convertToBytes(final String str, final Charset charset)
  2. convertToChars(final byte[] bytes, final Charset charset)
  3. convertToString(byte[] bytes, Charset charset)
  4. convertToString(final byte[] bytes, final Charset charset)
  5. decode(CharsetDecoder decoder, byte[] in)
  6. decode(final byte[] binaryValue, final Charset charset)
  7. decode(final byte[] pData, final int pOffset, final int pLength, final String pCharset)
  8. decodeBytes(byte[] data, Charset charset, boolean report)
  9. decodeCHARSET(byte[] bytes, Charset charset)