users@jersey.java.net

RE: [Jersey] How to find out content-length *before* unmarshalling?

From: Markus Karg <markus.karg_at_gmx.net>
Date: Mon, 28 Dec 2009 12:01:02 +0100

Tatu and Moises,

 

thank you for your ideas. It seems the idea of using a counting "dev/null
Stream" would be the most efficient one, since keeping the once streamed
result for a later request might clutter RAM with possibly never requested
String.

 

Thanks!

Markus

 

From: Moises Lejter [mailto:moilejter_at_gmail.com]
Sent: Montag, 28. Dezember 2009 07:15
To: users_at_jersey.dev.java.net
Subject: Re: [Jersey] How to find out content-length *before* unmarshalling?

 

Or one could try to cache the serialized form used to compute length, and
then reuse it the next time serialization is requested to actually return
the value...
(one would have to worry about the object changing between the two requests
to serialize, I guess...)

Moises

On Mon, Dec 28, 2009 at 1:01 AM, Tatu Saloranta <tsaloranta_at_gmail.com>
wrote:

On Sun, Dec 27, 2009 at 2:22 AM, Markus Karg <markus.karg_at_gmx.net> wrote:
> I need to find out the byte size (content-length) of a JAXB object (i. e.
> which content-length *would* be set *if i would* return it as the result
of
> a JAX-RS method). I know that I could obtain the unmarshaller manually via
> the Providers interface, marshal the object and check the resulting size.
> But I wonder if there is some smarter way to do that?

As far as I know, not in general case. There may be limited number
special cases (for trivial types), but the general problem of
calculating length faster than actually serializing (or equivalent,
without writes) is difficult one. And especially so for xml, what with
namespace binding, character quoting, and even underlying UTF-8
encoding.

So I think simplest way is about the best: reuse a context, create
marshaller with a bogus output stream (one that only calculates number
of bytes that it has been requested to write, but does not store any
of it -- assuming you don't actually need the serialization).

-+ Tatu +-

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
For additional commands, e-mail: users-help_at_jersey.dev.java.net