Java Convert via ByteBuffer toString(byte[] value, int offset, int length, String encoding)

Here you can find the source of toString(byte[] value, int offset, int length, String encoding)

Description

to String

License

Apache License

Declaration

public static String toString(byte[] value, int offset, int length, String encoding)
            throws UnsupportedEncodingException 

Method Source Code

//package com.java2s;
/*/*w  w w.  j  a v  a 2  s  .co  m*/
 * Copyright 2017 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

import java.nio.charset.Charset;

import java.util.concurrent.ConcurrentHashMap;

public class Main {
    private static final String platformEncoding = System.getProperty("file.encoding");
    private static final ConcurrentHashMap<String, Charset> charsetsByAlias = new ConcurrentHashMap<String, Charset>();

    public static String toString(byte[] value, int offset, int length, String encoding)
            throws UnsupportedEncodingException {
        Charset cs = findCharset(encoding);
        return cs.decode(ByteBuffer.wrap(value, offset, length)).toString();
    }

    public static String toString(byte[] value, String encoding) throws UnsupportedEncodingException {
        return findCharset(encoding).decode(ByteBuffer.wrap(value)).toString();
    }

    public static String toString(byte[] value, int offset, int length) {
        try {
            return findCharset(platformEncoding).decode(ByteBuffer.wrap(value, offset, length)).toString();
        } catch (UnsupportedEncodingException e) {
            // can't happen, emulating new String(byte[])
        }
        return null;
    }

    public static String toString(byte[] value) {
        try {
            return findCharset(platformEncoding).decode(ByteBuffer.wrap(value)).toString();
        } catch (UnsupportedEncodingException e) {
            // can't happen, emulating new String(byte[])
        }
        return null;
    }

    private static Charset findCharset(String alias) throws UnsupportedEncodingException {
        try {
            Charset cs = charsetsByAlias.get(alias);
            if (cs == null) {
                cs = Charset.forName(alias);
                Charset oldCs = charsetsByAlias.putIfAbsent(alias, cs);
                if (oldCs != null) {
                    // if the previous value was recently set by another thread we return it instead of value we found here
                    cs = oldCs;
                }
            }
            return cs;
            // We re-throw these runtimes for compatibility with java.io
        } catch (IllegalArgumentException iae) {
            throw new UnsupportedEncodingException(alias);
        }
    }
}

Related

  1. toShort(final byte byteValue, final ByteOrder byteOrder)
  2. toShortArray(byte[] arr)
  3. toShortArray(final byte[] byteArray)
  4. toSimpleList(List attrValues)
  5. toString(byte[] buf, int arrayOffset, int origLimit, StringBuilder sb)
  6. toString(final String filename)
  7. toString(final Xid xid)
  8. toString(InputStream is)
  9. toString(Object o)