输入流转输出流

输入流转输出流,第1张

输入流转输出
public class IOUtil {

    
    public static ByteArrayOutputStream parse(InputStream inputStream) throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        int ch;
        while ((ch = inputStream.read()) != -1) {
            swapStream.write(ch);
        }
        return swapStream;
    }
    
    
    public static ByteArrayInputStream parse(OutputStream out) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos = (ByteArrayOutputStream) out;
        ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
        return swapStream;
    }
    
    
    public static void close(InputStream in) {
        if (null != in) {
            try {
                in.close();
            } catch (IOException e) {
                log.error("close inputStream error", e);
            }
        }
    }

    
    public static void close(Reader reader) {
        if (null != reader) {
            try {
                reader.close();
            } catch (IOException e) {
                log.error("close reader error", e);
            }
        }
    }

    
    public static void close(OutputStream out) {
        if (null != out) {
            try {
                out.close();
            } catch (IOException e) {
                log.error("close OutputStream error", e);
            }
        }
    }

    
    public static void close(Writer writer) {
        if (null != writer) {
            try {
                writer.close();
            } catch (IOException e) {
                log.error("close writer error", e);
            }
        }
    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5687652.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存