Java Byte Array to String by Charset decodeOrDefault(byte[] data, Charset charset, String defaultValue)

Here you can find the source of decodeOrDefault(byte[] data, Charset charset, String defaultValue)

Description

decode Or Default

License

Apache License

Declaration

static String decodeOrDefault(byte[] data, Charset charset, String defaultValue) 

Method Source Code

//package com.java2s;
/*//ww w.  j  av  a  2  s.  co m
 * Copyright 2013 Netflix, 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.Closeable;
import java.io.IOException;

import java.io.Reader;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;

import static java.lang.String.format;

public class Main {
    private static final int BUF_SIZE = 0x800;

    static String decodeOrDefault(byte[] data, Charset charset, String defaultValue) {
        if (data == null) {
            return defaultValue;
        }
        checkNotNull(charset, "charset");
        try {
            return charset.newDecoder().decode(ByteBuffer.wrap(data)).toString();
        } catch (CharacterCodingException ex) {
            return defaultValue;
        }
    }

    /**
     * Copy of {@code com.google.common.base.Preconditions#checkNotNull}.
     */
    public static <T> T checkNotNull(T reference, String errorMessageTemplate, Object... errorMessageArgs) {
        if (reference == null) {
            // If either of these parameters is null, the right thing happens anyway
            throw new NullPointerException(format(errorMessageTemplate, errorMessageArgs));
        }
        return reference;
    }

    /**
     * Adapted from {@code com.google.common.io.CharStreams.toString()}.
     */
    public static String toString(Reader reader) throws IOException {
        if (reader == null) {
            return null;
        }
        try {
            StringBuilder to = new StringBuilder();
            CharBuffer buf = CharBuffer.allocate(BUF_SIZE);
            while (reader.read(buf) != -1) {
                buf.flip();
                to.append(buf);
                buf.clear();
            }
            return to.toString();
        } finally {
            ensureClosed(reader);
        }
    }

    public static void ensureClosed(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException ignored) { // NOPMD
            }
        }
    }
}

Related

  1. decode(final byte[] binaryValue, final Charset charset)
  2. decode(final byte[] pData, final int pOffset, final int pLength, final String pCharset)
  3. decodeBytes(byte[] data, Charset charset, boolean report)
  4. decodeCHARSET(byte[] bytes, Charset charset)
  5. decodeHelper(byte[] byteArray, int numberOfBytes, java.nio.charset.Charset charset)
  6. decodeWithReplacement(byte[] bytes, Charset cs)
  7. encode(byte[] arr, Charset srcCharset, Charset dstCharset)
  8. encodeB(String prefix, String text, int usedCharacters, Charset charset, byte[] bytes)
  9. encodingIsCorrect(byte[] bytes, String charset)