How to reset Inputstream

Asked

Viewed 221 times

1

I have a Webservice that saves the photo in a folder of the server that was sent by the Android application through the following code:

       ...

        int read = 0;
        byte[] bytes = new byte[1024];

        OutputStream out = new FileOutputStream(new File( caminhodestinofotos + uploadedFileName ));
        while (( read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        out.flush();
        out.close();

        ...

So far so good, but we decided to save this photo in the database.

To that end I thought of transforming the Inputstream that arrives the function of the Webservice in an array of bytes.

I added the following line at the beginning of the function:

byte[] arrbytes =  sun.misc.IOUtils.readFully(uploadedInputStream, -1, true);

It turns out that after running this line the LOOP that reads from the file adds to the byte array does not happen.

The line read = uploadedInputStream.read(bytes)) != -1 returns -1 as if Inputstream is at the end of the file and a sequential read cannot occur again.

How should I proceed? How to reset and place the reading pointer at the beginning of file again, or what would be another way to use that same Inputstream to save to file and save to database?

Grateful.

  • 1

    Hello, in Inputstream there are two methods for this: the mark(int readLimit) and the reset(). The mark does what the name says, marks a point on the file. The reset() resets to exactly this point. Try using a mark(1000) before the line you want to read in loop, and at the end give the reset(). I don’t know exactly if this is what I was looking for, so I didn’t put it in answer.

  • The same error occurs: java.io.Ioexception: mark/reset not supported

  • You can edit your post with the complete code of this method?

  • 1

    The inputStream that arrived at the function did not support dialing and resetting. Copy to another one that supports it : Bufferedinputstream bi = new Bufferedinputstream(uploadedInputStream); and I worked with this one. I marked copied to bytes[] array, reset and saved in table. Offer the answer so that I can accept it.

  • Okay, I’m glad you settled!

1 answer

1


Hello, in the Inputstream there are two methods to this:

mark(int readLimit) - Do what the name says, mark a point on the file.

reset() - Reset exactly to the point marked by the above method.

I suggest trying to use a mark(1000) before the line you want to read in loop, and at the end give reset(), so the reader will go back to where it marked and run read().

  • 1

    Before using these methods it is convenient to use markSupported() to see if it supports, otherwise it is necessary to copy to another one that supports.

  • I didn’t know that. I never had a problem without the markSupported() actually. But it’s always good to know, haha.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.