All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cloud.agileframework.common.util.stream.StreamUtil Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
package cloud.agileframework.common.util.stream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;

/**
 * @author 佟盟
 * 日期 2020/5/15 15:00
 * 描述 流工具
 * @version 1.0
 * @since 1.0
 */
public class StreamUtil {
    private static final Log log = LogFactory.getLog(StreamUtil.class);

    private StreamUtil() {
    }

    /**
     * 输入流转字符串
     *
     * @param inputStream 输入流
     * @return 字符串
     */
    public static String toString(InputStream inputStream) {
        return toString(inputStream, Charset.defaultCharset());
    }

    /**
     * 输入流转字符串
     *
     * @param inputStream 输入流
     * @return 字符串
     */
    public static String toString(InputStream inputStream, Charset charset) {
        try (ByteArrayOutputStream result = new ByteArrayOutputStream()) {
            toOutputStream(inputStream, result);
            return result.toString(charset.name());
        } catch (IOException e) {
            log.error("InputStream convert to String error", e);
        }
        return null;
    }

    /**
     * 输入流转输出流
     *
     * @param inputStream  输入流
     * @param outputStream 输出流
     */
    public static void toOutputStream(InputStream inputStream, OutputStream outputStream) {
        try {
            final int length = 1024;
            byte[] buffer = new byte[length];
            int r;
            while ((r = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, r);
            }
            outputStream.flush();
        } catch (IOException e) {
            log.error("InputStream convert to OutputStream error", e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy