Java Byte Array to String by Charset getString(byte[] bytesIn, Charset cs)

Here you can find the source of getString(byte[] bytesIn, Charset cs)

Description

get String

License

Open Source License

Declaration

public static String getString(byte[] bytesIn, Charset cs) 

Method Source Code

//package com.java2s;
/*/*from   w w  w. j  av  a2s. c o  m*/
 * #%L
 * Tesora Inc.
 * Database Virtualization Engine
 * %%
 * Copyright (C) 2011 - 2014 Tesora Inc.
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */

import java.lang.ref.SoftReference;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;
import java.util.Arrays;

public class Main {
    private static ThreadLocal<SoftReference<CharsetDecoder>> decoder = new ThreadLocal<SoftReference<CharsetDecoder>>();

    public static String getString(byte[] bytesIn, Charset cs) {
        return getString(bytesIn, cs, false);
    }

    public static String getString(byte[] bytesIn, Charset cs,
            boolean checkIfValid) {
        if (checkIfValid)
            return getString(bytesIn, cs, CodingErrorAction.REPORT);
        else
            return getString(bytesIn, cs, CodingErrorAction.REPLACE);
    }

    static String getString(byte[] bytesIn, Charset cs,
            CodingErrorAction cea) {
        CharsetDecoder cd = (CharsetDecoder) deref(decoder);
        if ((cd == null) || (!cs.name().equals(cd.charset().name()))) {
            cd = cs.newDecoder();
            set(decoder, cd);
        }

        int en = scale(bytesIn.length, cd.maxCharsPerByte());
        char[] charOut = new char[en];
        if (bytesIn.length == 0)
            return new String(charOut);

        CharBuffer cb = CharBuffer.wrap(charOut);
        CoderResult cr = cd.onMalformedInput(cea)
                .onUnmappableCharacter(cea)
                .decode(ByteBuffer.wrap(bytesIn), cb, true);
        if (cr.isMalformed() || cr.isUnmappable())
            charOut = null;

        return (charOut == null ? null : new String(safeTrim(charOut,
                cb.position())));
    }

    private static <T> T deref(ThreadLocal<SoftReference<T>> tl) {
        SoftReference<T> sr = (SoftReference<T>) tl.get();
        if (sr == null)
            return null;
        return sr.get();
    }

    private static <T> void set(ThreadLocal<SoftReference<T>> tl, T ob) {
        tl.set(new SoftReference<T>(ob));
    }

    private static int scale(int len, float expansionFactor) {
        return (int) (len * (double) expansionFactor);
    }

    private static byte[] safeTrim(byte[] ba, int len) {
        if (ba == null)
            return null;
        if (len == ba.length)
            return ba;
        else
            return Arrays.copyOf(ba, len);
    }

    private static char[] safeTrim(char[] ca, int len) {
        if (ca == null)
            return null;
        if (len == ca.length)
            return ca;
        else
            return Arrays.copyOf(ca, len);
    }
}

Related

  1. getNumberOfBytesPerCharacter(final String charsetName)
  2. getPrefixWithMaxBytes(String string, int maxBytes, Charset charSet)
  3. getRowId(final byte[] rowId, final Charset charset, final boolean base64encodeValues)
  4. getString(@Nonnull byte[] b, @Nonnegative int bufferSize, @Nonnegative int offset, @Nonnull Charset charset)
  5. getString(byte[] bytes, String charset)
  6. identify(byte[] bytes, CharsetDecoder decoder)
  7. inflate(byte[] bytes, Charset encoding)
  8. isCharset(byte[] b, String inCharset)
  9. newString(byte[] bytes, Charset charset)