当前位置 : 主页 > 编程语言 > java >

保存输入流

来源:互联网 收集:自由互联 发布时间:2021-06-28
saveStream public class SaveStream { private InputStream input; private byte[] data = new byte[0]; public SaveStream(InputStream input) throws IOException { this.input = input; save(); } private void save() throws IOException { ByteArrayOut
saveStream
public class SaveStream {
    private InputStream input;
    private byte[] data = new byte[0];

    public SaveStream(InputStream input) throws IOException {
        this.input = input;
        save();
    }

    private void save() throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = input.read(buffer)) != -1) {
            output.write(buffer, 0, len);
        }
        data = output.toByteArray();
    }

    public InputStream getInputStream() {
        return new ByteArrayInputStream(data);
    }
}
网友评论