Java tutorial
/* * Copyright (C) 2008 feilong * * 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. */ package com.sunchenbin.store.feilong.core.io; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * {@link java.io.InputStream} . * * @author feilong * @version 1.0.9 201536 ?10:28:58 * @see ReaderUtil * @see java.io.InputStream * @since 1.0.9 */ public final class InputStreamUtil { /** The Constant LOGGER. */ private static final Logger LOGGER = LoggerFactory.getLogger(InputStreamUtil.class); /** Don't let anyone instantiate this class. */ private InputStreamUtil() { //AssertionError?. ?????. ???. //see Effective Java 2nd throw new AssertionError("No " + getClass().getName() + " instances for you!"); } /** * {@link java.io.InputStream} ?string.<br> * ? {@link Charset#defaultCharset()} * * @param inputStream * the input stream * @return {@link java.io.InputStream} ?string * @see #inputStream2String(InputStream, String) */ public static String inputStream2String(InputStream inputStream) { Charset defaultCharset = Charset.defaultCharset(); String charsetName = defaultCharset.name(); LOGGER.debug("the param defaultCharset:[{}]", charsetName); return inputStream2String(inputStream, charsetName); } /** * {@link java.io.InputStream} ?string.<br> * ?cmd, ??,? <code>charsetName</code>. * * @param inputStream * the input stream * @param charsetName * ?? charset ?? * @return {@link java.io.InputStream} ?string * @see #toBufferedReader(InputStream, String) * @see ReaderUtil#toString(Reader) */ public static String inputStream2String(InputStream inputStream, String charsetName) { BufferedReader bufferedReader = toBufferedReader(inputStream, charsetName); return ReaderUtil.toString(bufferedReader); } /** * {@link java.io.InputStream} ? {@link java.io.BufferedReader} ({@link java.io.BufferedReader} ?). * * @param inputStream * the input stream * @param charsetName * the charset name * @return the buffered reader * @see java.io.BufferedReader * @see java.io.InputStreamReader#InputStreamReader(InputStream, String) * @see org.apache.commons.io.IOUtils#toBufferedReader(Reader) */ public static BufferedReader toBufferedReader(InputStream inputStream, String charsetName) { try { Reader reader = new InputStreamReader(inputStream, charsetName); // ? bufferedReader // read() ?? Reader FileReader InputStreamReader. return org.apache.commons.io.IOUtils.toBufferedReader(reader); } catch (UnsupportedEncodingException e) { throw new UncheckedIOException(e); } } }