流的工具类的使用

流的工具类的使用

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.io.*;

/**
 * @date 2021-08-13 11:16
 */
@Slf4j
public class StreamFileUtil {

    private StreamFileUtil (){

    }
    /**
    * 把bytes数组写到outputStream
    * @date 2021-08-13 11:20
    * @param bytes 字节数组
    * @param outputStream 输出流不能为null
    */
    public static void writeToOutputStream(byte[] bytes, OutputStream outputStream) throws IOException {
        if(outputStream==null){
            throw new HonlitechRTException("outputStream 输出流不能为空");
        }
        try (InputStream in = new ByteArrayInputStream(bytes)) {
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len = in.read(buf)) != -1) {
                outputStream.write(buf, 0, len);
            }
            outputStream.flush();
        } finally {
            outputStream.close();
        }
    }
    /**
    * 输入流转成byte
    * @date 2021-08-13 16:36
    * @param input 输入流
    * @return
    */
    public static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        }
        return output.toByteArray();
    }
    /**
    * 输入流转成文件
    * @date 2021-08-13 16:37
    * @param fileName 文件名
    */
    public static File inputStreamToFile(InputStream ins, String fileName) {
        File file=new File(fileName);
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            throw new HonlitechRTException(ExceptionUtils.getMessage(e));
        }
        return file;
    }

    /**
     * 输入流转写到输出流
     * @date 2021-08-13 16:37
     */
    public static void inputStreamWriteOutputStream(InputStream ins, OutputStream output) {

        try {
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                output.write(buffer, 0, bytesRead);
            }
        } catch (Exception e) {
            throw new HonlitechRTException(ExceptionUtils.getMessage(e));
        }finally {

            try {
                if(output!=null){
                    output.close();
                }
                if(ins!=null){
                    ins.close();
                }
            } catch (IOException e) {
                log.error(ExceptionUtils.getMessage(e));
            }

        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值