[jdom-interest] How many bytes is my JDOM Document

Jason Winnebeck gillius-ml at gillius.org
Thu Aug 3 06:30:05 PDT 2006


Hmm, if there is not a much better solution, perhaps what you can do is write 
it to a memory buffer (ByteOutputStream I believe it is) and find the size of 
that memory buffer.  That would still require a run through all of the 
serialization code, but then you at least wouldn't be hitting the disk.

If the memory use is a concern you could even write your own implementation of 
OutputStream which does not actually output the bytes anywhere, but instead 
just increments a counter.  Writing such a class would only take a few minutes 
in an IDE that does automatic inheritance like IDEA, or maybe 15 minutes by 
hand.  That class would increase the speed to the maximum as well as not using 
any memory.

I think ultimately any solution will require this, because the number of bytes 
is dependent on the character encoding of the document -- which may not be 
constant in the case of UTF, and also the "pretty" formatter can insert spaces 
and tabs or whatever it wants in places, so you basically would need to go 
through the whole process of serialization.

Jason

Søren Faltz wrote:
> I want to find out how many bytes my document is.
>  
> An easy way would be to stream the document to disk, and then call 
> File.size()
>  
> but is there any way to find out without saving the document to disk first??
>  
> best regards
> søren
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com



More information about the jdom-interest mailing list