Possible Duplicate:
How to make a deep copy of an InputStream in Java ?
I have an InputStream object and I want to make a copy of it. What is the best way to do this?
The data is not coming from a file but as the payload of a http form being sent from a web page, I am using the Apache Commons FileUpload lib, my code which gives me the InputStream looks like this: ...
InputStream imageStream = null;
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = new ArrayList();
items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) { // this is subject Id
if (item.getFieldName().equals("subId")) {
subId = Integer.parseInt(item.getString());
System.out.println("SubId: " + subId);
}
} else {
imageStream = item.getInputStream();
}
}
What is the best way to get a duplicate/copy of imageStream?
If you want to be able to read the stream again, I think your best option is to wrap the InputStream
in a BufferedInputStream
, and then use the BufferedInputStream
mark()
and reset()
methods. The InputStream
you have will probably not support them directly, since as far as I understood it receives data from the web.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With