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

Rolf Lear rlear at algorithmics.com
Tue Aug 8 04:26:19 PDT 2006


This is an un-necessarily memory-demanding mechanism for doing it. The
complete document is required to be stored in the bytearray, and will need
garbage collection, etc.
 
A faster, and more memory friendly way to do it would be to create a class
like what's at the end, and use it instead of the ByteArrayOutputStream.
 
Rolf
import java.io.IOException;

import java.io.OutputStream;

public final class CountingDevNullOutputStream extends OutputStream {

private int count = 0;

public void reset() {

count = 0;

}

public int size() {

return count;

}

public void write(int b) throws IOException {

count++;

}

public void close() throws IOException {

}

public void flush() throws IOException {

}

public void write(byte[] b, int off, int len) throws IOException {

count += len;

}

public void write(byte[] b) throws IOException {

count += b.length;

}

}

 

-----Original Message-----
From: jdom-interest-bounces at jdom.org
[mailto:jdom-interest-bounces at jdom.org]On Behalf Of Søren Faltz
Sent: Tuesday, August 08, 2006 6:09 AM
To: jdom-interest at jdom.org
Subject: Re: [jdom-interest] How many bytes is my JDOM Document


I am determing the size this way
 

String s;
    int contentLength=0;

    try {
      s = convertDocumentToString(doc);
      
      ByteArrayOutputStream bout = new ByteArrayOutputStream();
      ObjectOutputStream objOut = new ObjectOutputStream(bout);
      
      objOut.writeObject (s);
      contentLength = bout.size();

      objOut.flush();
      bout.flush(); 



 
2006/8/4, Paul Libbrecht < paul at activemath.org <mailto:paul at activemath.org>
>: 

Print it to a stream that only counts but otherwise discards ? It's easy
to write such a stream... just subclass outputstream.write(byte) and
maybe .write(byte[],start,len) and count...

I don't think there's a way without outputting.

paul

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







This email and any files transmitted with it are confidential and
proprietary to Algorithmics Incorporated and its affiliates
("Algorithmics").  If received in error, use is prohibited.  Please destroy,
and notify sender.  Sender does not waive confidentiality or privilege.
Internet communications cannot be guaranteed to be timely, secure, error or
virus-free.  Algorithmics does not accept liability for any errors or
omissions.  Any commitment intended to bind Algorithmics must be reduced to
writing and signed by an authorized signatory.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.jdom.org/pipermail/jdom-interest/attachments/20060808/6dfe4f89/attachment.htm


More information about the jdom-interest mailing list