Resetting InputStream

1

I have a WebService that saves the photo to a server folder 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 this end, I thought about turning the InputStream that gets the WebService function into a byte array.

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

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

It turns out that after executing this line the LOOP that reads from the file added to the bytes array does not happen.

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

How should I proceed? Resetting and placing the read pointer at the beginning of the file again, or what would be another way to use this same InputStream to save to file and save to the database?

Thank you.

    
asked by anonymous 14.07.2016 / 17:09

1 answer

1

Hello, in the InputStream there are two methods for this: / p>

mark(int readLimit) - Does what the name says, marks a period in the file.

reset() - Resets 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 of the reset (), the reader will return to where you checked and execute read ().

    
14.07.2016 / 19:14