In Java network inputstream why is it not recommended that this be done :
PdfReader localPdfReader = new PdfReader(item.getinputStream());
Where item is a FileItem uploaded from a client.
Instead this is recommended:
ByteArrayOutputStream output = new ByteArrayOutputStream();
IOUtils.copy(item.getInputStream(), output);
What is the diference between the two?
EDIT
I came to know that it is because inputstream here is a network stream. So once a byte is read you cannot read it anymore. So you have to copy it to memory(IOUtils) before using it. My question is while copying to memory also you have to read the stream byte by byte right? then why doesn’t the inputstream get closed there?
I guess the difference is of Reader and Inputstream. In your example, a PDF doc is binary data which should not be transferred character by character but byte by byte. Check this link in the same forum for more on Reader and InputStream. Even though it mentioned wrapping of Stream by Reader, as mentioned earlier for binary data this should be discouraged.
EDIT: 1
Lets check the way Reader and InputStream’s read method works
Reader.read()returns integer in the range 0 to 65535 (single 16-bit Unicode character)InputStream.read()returns byte (8-bit signed two’s complement integer) of dataNow imagine if you use Reader to read binary data (which is sequence of 8 bit integer), you will end up reading two bytes (8*2) instead of one assuming it to be a character.
I have not seen the code for
PdfReaderso not sure if it usesjava.io.Reader. This explaination is purly forjava.io.Reader/InputStream. I would appreciate if you share some link or post which which says the thePdfReaderif used in a manner you mentioned, is not good.EDIT:2
Remember:
If you use
PdfReader localPdfReader = new PdfReader(item.getinputStream());then PdfReader internally reads the bytes from stream and uses it to validate. It does not store it for any further usage.
If you use
It copies the bytes from network to a byte array which later can be used in
PdfReaderas well as JDBC call to store it in DB.