users@jax-rs-spec.java.net

[jax-rs-spec users] [jsr339-experts] Response.readEntity questions

From: Sergey Beryozkin <sberyozkin_at_talend.com>
Date: Wed, 24 Jul 2013 14:30:30 +0100

Hi

Would like to clarify, what happens when we do

Response.readEntity(InputStream.class);

The docs say:

"A message instance returned from this method will be cached for
subsequent retrievals via getEntity(). Unless the supplied entity type
is an input stream, this method automatically closes the an unconsumed
original response entity data stream if open".

Basically, I think what this reads is that if we do

Response.readEntity(InputStream.class);

then the original stream is not cached (because we'd need to close the
original stream), not closed and returned as is.
Is it the right interpretation ?

Now, this may not be related, not sure, but here is another question.
The docs continue with:

"In case the entity data has been buffered, the buffer will be reset
prior consuming the buffered data to enable subsequent invocations of
readEntity(...) methods on this response.".

That confuses me a bit. The earlier text says that the entity will be
cached (aka, buffered). Now, the user calls readEntity() again. Why do
we need to reset a buffered entity if we can simply return the buffered
entity ? The only issue I see with the returning readEntity call is that
of we do readEntity(Bar.class) then the cached entity is actually now
Bar instance so when we do next time readEntity(Input.class) - we simply
get IllegalStateException. So why to reset a buffer ?

Finally:

IllegalStateException is returned if "... or if the entity input stream
has been fully consumed already and has not been buffered prior
consuming.".

I wonder how can the implementation determine that the stream has been
consumed (presumably after the code got the original stream via
getEntity or readEntity(InputStream.class) and consumed it somehow) ? If
that happens and the user then does readEntity(Bar.class) then MBR will
throw an exception but we don't know if it was because the stream has
already been consumed or for some other reasons.

Marek, can you clarify please ? Sorry if I'm missing something obvious

Thanks, Sergey