How can I reopen a closed InputStream when I need to use it 2 times?

I would probably drain the input stream into a byte using ByteArrayOutputStream and then create a new ByteArrayInputStream based on the result every time I need to reread the stream Something like this: ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte buf = new byte1024; int n = 0; while ((n = myInputStream. Read(buf)) >= 0) baos. Write(buf, 0, n); byte content = baos.toByteArray(); InputStream is1 = new ByteArrayInputStream(content); ... use is1 ... InputStream is2 = new ByteArrayInputStream(content); ... use is2.

I would probably drain the input stream into a byte using ByteArrayOutputStream and then create a new ByteArrayInputStream based on the result every time I need to reread the stream. Something like this: ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte buf = new byte1024; int n = 0; while ((n = myInputStream. Read(buf)) >= 0) baos.

Write(buf, 0, n); byte content = baos.toByteArray(); InputStream is1 = new ByteArrayInputStream(content); ... use is1 ... InputStream is2 = new ByteArrayInputStream(content); ... use is2 ...

Alternatively, I found this great way to achieve it: import java.io. ByteArrayInputStream; import java.io. ByteArrayOutputStream; import java.io.

IOException; import java.io. InputStream; public class CopyInputStream { private InputStream _is; private ByteArrayOutputStream _copy = new ByteArrayOutputStream(); /** * */ public CopyInputStream(InputStream is) { _is = is; try { copy(); } catch(IOException ex) { // do nothing } } private int copy() throws IOException { int read = 0; int chunk = 0; byte data = new byte256; while(-1! = (chunk = _is.

Read(data))) { read += data. Length; _copy. Write(data, 0, chunk); } return read; } public InputStream getCopy() { return (InputStream)new ByteArrayInputStream(_copy.toByteArray()); } } And I call it with CopyInputStream cis = new CopyInputStream(input); InputStream input1 = cis.getCopy().

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions