Java Byte Array to String by Charset decode(CharsetDecoder decoder, byte[] in)

Here you can find the source of decode(CharsetDecoder decoder, byte[] in)

Description

decode

License

Open Source License

Declaration

private static String decode(CharsetDecoder decoder, byte[] in) 

Method Source Code

//package com.java2s;
/*/*www.j  a v  a  2s .  c  om*/
 * ====================================================================
 * Copyright (c) 2004-2010 TMate Software Ltd.  All rights reserved.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at http://svnkit.com/license.html
 * If newer versions of this license are posted there, you may use a
 * newer version instead, at your option.
 * ====================================================================
 */

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

public class Main {
    private static String decode(CharsetDecoder decoder, byte[] in) {
        ByteBuffer inBuf = ByteBuffer.wrap(in);
        CharBuffer outBuf = CharBuffer.allocate(inBuf.capacity() * Math.round(decoder.maxCharsPerByte() + 0.5f));
        decoder.decode(inBuf, outBuf, true);
        decoder.flush(outBuf);
        decoder.reset();
        return outBuf.flip().toString();
    }
}

Related

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