InputStream, ByteArrayOutputStream, HashMaps & DefaultFileItemFactory
807601Mar 21 2008 — edited Mar 25 2008I am using DefaultFileItemFactory get a file upload. My project requires that I dont write the file to the hard drive. I must however be able to carry the file to multiple.
I found that item.get() corrupted some files when putting it in the Map so I started using
InputStream stream = item.getInputStream();
And put that in the HashMap. When I want to retrieve the object I use the code below.
//get the file back out of the hashmap
InputStream stream = (InputStream)formDataHM.get(key);
int bufferSize = stream.available();
byte[] buffer = new byte[bufferSize];
ByteArrayOutputStream baos = new ByteArrayOutputStream(bufferSize);
int bytesread = 0;
while(true){
bytesread = stream.read(buffer);
if (bytesread == -1)
break;
baos.write(buffer,0,bytesread);
}
content = baos.toByteArray();
Its seems to work fine with no issues but I am wondering what performance hit I am taking by doing this. I am new to java so I even have questions about setting the bufferSize. I see some people set it for the size of the file while others set it for 1024 or 8 thousand something.
Any input would be appreciated.
Regards,
Earl