ByteArrayOutputStream 与InputStream 互相转换

InputStream 转为 ByteArrayOutputStream

public Reader(InputStream input) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        try {
            while ((len = input.read(buffer)) > -1) {
                baos.write(buffer, 0, len);
            }
            baos.flush();
        } catch (IOException e) {
            throw new Exception("Illegal flow.");
        } finally {
            try {
                input.close();
            } catch (IOException e) {
                logger.error("file stream shutdown failed.");
            }
        }
        this.baos = baos;
}

ByteArrayOutputStream 转为 InputStream

 private InputStream streamTran(ByteArrayOutputStream in) {
        return new ByteArrayInputStream(in.toByteArray());
    }

你可能感兴趣的:(工具类)