I can see there are a number of posts regarding reuse InputStream
. I understand InputStream
is a one-time thing and cannot be reused.
However, I have a use case like this:
I have downloaded the file from DropBox
by obtaining the DropBoxInputStream
using the DropBox's Java SDK
. I then need to upload the file to another system by passing the InputStream
. However, as part of the download, I have to provide the MD5
of the file. So I have to read the file from the stream before uploading the file. Because the DropBoxInputStream
I received can only be used once, I have to get another DropBoxInputStream
after I have calculated the MD5
and before uploading the file. The procedure is like:
- Get first DropBoxInputStream
- Read from the DropBoxInputStream and calculate MD5
- Get the second DropBoxInputStream
- Upload file using the MD5 and the second DropBoxInputStream.
I am thinking that, if there are many way for me to "cache" or "backup" the InputStream
before I calculate the MD5
so that I can save step 3 of obtaining the same DropBoxInputStream
again?
Many thanks
EDIT:
Sorry I missed some information.
What I am currently doing is that I use a MD5DigestOutputStream
to calculate MD5
. I stream data across the MD5DigestOutputStream
and save them locally as a temp file. Once the data goes through the MD5DigestOutputStream
, it will calculate the MD5
.
I then call a third party library to upload the file using the calculated md5 and a FileInputStream
which reads from the temp file.
However, this requires huge disk space sometime and I want to remove the needs to use temp file. The library I use only accepts a MD5
and InputStream
. This means I have to calculate the MD5
on my end. My plan is to use my MD5DigestOutputStream
to write data to /dev/null
(not keeping the file) so that I can calculate theMD5
, and get the InputStream
from DropBox again and pass that to the library I use. I assume the library will be able to get the file directly from DropBox
without the need for me to cache the file either in the memory of at the disk. Will it work?