Here you can find the source of decodeOrDefault(byte[] data, Charset charset, String defaultValue)
static String decodeOrDefault(byte[] data, Charset charset, String defaultValue)
//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 } } } }