views:

241

answers:

1

In my application I develop web service that get attached file. The file is mapped to DataHandler object via JaxB, and I have access to the file via DataHandler.getInputStream()

My problem is this: When the file attribute exist in the web service request, but no file is attached, I still get the DataHandler object, and its getInputStream().available() = 11 bytes (a header I guess...??).

So I can I know that the inputStream is empty?

Thanks, Alon

+1  A: 

Read it and parse the data as it should be parsed. The answer is in there.

The InputStream#available() certainly does not return the length of the stream or so as you seem to think. In some cases it (by coincidence) may, but you shouldn't rely on that. It just returns the amount of bytes which are available for read without blocking other threads. Just read the stream the usual Java IO way fully until the last bit returned -1 and then intercept on the whole data you received.

BalusC
But if the file is large, I will have to parse the whole file...Do I have other way without parsing the whole file?
Alon
Another problem is I can get many file types (images, videos, mp3, etc...) So I don't want to read each file according to its type in order to see if the InputStream is empty.If the InputStream is empty will I get always 11 bytes in InputStream().available() ?
Alon
No, that fully depends on the other side. If I was you I would study its documentation more and/or contact its support team. This is not normal and you should never ever rely on the `available()` to indicate some kind of length.
BalusC
Is the InputStream's length of the DataHandler is always 11 when no file is attached to it?
Alon