Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to make a copy of an InputStream in java [duplicate]

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?

like image 443
Ankur Avatar asked Dec 24 '10 11:12

Ankur


1 Answers

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.

like image 152
Flavio Avatar answered Oct 06 '22 07:10

Flavio