Java Byte Array to String by Charset newStringFromSplit(CharsetDecoder decoder, CharsetDecoder utf8Decoder, String encoding, byte[] fieldBytes, int length)

Here you can find the source of newStringFromSplit(CharsetDecoder decoder, CharsetDecoder utf8Decoder, String encoding, byte[] fieldBytes, int length)

Description

new String From Split

License

Open Source License

Declaration

public static String newStringFromSplit(CharsetDecoder decoder, CharsetDecoder utf8Decoder, String encoding,
            byte[] fieldBytes, int length) 

Method Source Code

//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

import java.nio.ByteBuffer;
import java.nio.CharBuffer;

import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;

public class Main {
    public static String newStringFromSplit(CharsetDecoder decoder, CharsetDecoder utf8Decoder, String encoding,
            byte[] fieldBytes, int length) {
        ByteBuffer fieldBuf = ByteBuffer.wrap(fieldBytes, 0, length);
        CharBuffer fieldCharBuf = CharBuffer.allocate(length);
        utf8Decoder.reset();//w  w w.j  av  a2  s .c om
        CoderResult res = utf8Decoder.decode(fieldBuf, fieldCharBuf, true);
        if (res.isError() && decoder != null) {
            decoder.reset();
            res = decoder.decode(fieldBuf, fieldCharBuf, true);
            if (!res.isError()) {
                decoder.flush(fieldCharBuf);
                return new String(fieldCharBuf.array());
            }
        } else {
            utf8Decoder.flush(fieldCharBuf);
            return new String(fieldCharBuf.array());
        }
        return "";
    }
}

Related

  1. isCharset(byte[] b, String inCharset)
  2. newString(byte[] bytes, Charset charset)
  3. newString(byte[] bytes, String charsetName)
  4. newString(final byte[] bytes, final Charset charset)
  5. newString(final byte[] bytes, final Charset charset)
  6. parseBytes(byte[] encoded, Charset charset)
  7. str(byte[] bytes, String charset)
  8. stringToByteArray(String str, Charset charsetForEncoding, int lenOfByteArray, byte filler)
  9. toBytes(CharSequence str, String charsetName)