---
title: "JAVA clone 复制 inputstream "
author: "lvjun106"
date: "2016-05-20T15:47:44Z"
tags: []
categories: ["编程语言"]
slug: "java-clone-inputstream"
draft: false
lastmod: "2016-05-20T15:47:44Z"
id: 2299575
url: "https://lvjun106.iteye.com/blog/2299575"
---

```
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 输入流用过之后，就会关闭。怎样重复使用呢？