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:/*  ww w  .  j av 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;
        }
    }

    /**
     * Converts a char[] to String.
     */
    public static String toString(final char[] c) {
        return new String(c);
    }

    /**
     * Converts a char[][] to String, where segments are separated by '.'.
     */
    public static String toString(final char[][] c) {
        final StringBuilder sb = new StringBuilder();
        for (int i = 0, max = c.length; i < max; ++i) {
            if (i != 0) {
                sb.append('.');
            }
            sb.append(c[i]);
        }
        return sb.toString();
    }

    /**
     * Converts a char[][] and a char[] to String, where segments are separated by '.'.
     */
    public static String toString(final char[][] c, final char[] d) {
        if (c == null) {
            return new String(d);
        }
        final StringBuilder sb = new StringBuilder();
        for (final char[] element : c) {
            sb.append(element);
            sb.append('.');
        }
        sb.append(d);
        return sb.toString();
    }
}

Related

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