ITeye博客

JAVA clone 复制 inputstream

lvjun106 · 2016-05-20 15:47 · JAVA · 编程语言 · Markdown

ByteArrayOutputStream baos = new ByteArrayOutputStream();

// Fake code simulating the copy
// You can generally do better with nio if you need...
// And please, unlike me, do something about the Exceptions :D
byte[] buffer = new byte[1024];
int len;
while ((len = input.read(buffer)) > -1 ) {
    baos.write(buffer, 0, len);
}
baos.flush();

// Open new InputStreams using the recorded bytes
// Can be repeated as many times as you wish
InputStream is1 = new ByteArrayInputStream(baos.toByteArray()); 
InputStream is2 = new ByteArrayInputStream(baos.toByteArray()); 

 

对于大多数 InputStream 输入流用过之后,就会关闭。怎样重复使用呢?

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。