users@jersey.java.net

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

From: Moises Lejter <moilejter_at_gmail.com>
Date: Mon, 28 Dec 2009 01:14:54 -0500

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
>
>