OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Get size of document?

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
zaphod
Power User
Power User


Joined: 04 Nov 2004
Posts: 51
Location: Magdeburg, Germany

PostPosted: Sun Dec 04, 2005 11:05 am    Post subject: Get size of document? Reply with quote

Hi folks

How do I get the size of the current OOo document, i.e. the size of the buffer of this document?
I can get the size of the file, where the document is stored to, already:
Code:
XSimpleFileAccess simpleFileAccess = (XSimpleFileAccess)UnoRuntime.queryInterface(
   XSimpleFileAccess.class, sFileAccess);

//return file size in bytes
int nFileSize = simpleFileAccess.getSize(sURL);

But the file of the document isn't always up to date and it's not advisable to store the document always, before asking for the size (have to do that very often). So I need to get the size of the buffer of the current document. I searched the whole day in the API, but I have no clue.
Please help me out! Surprised
Back to top
View user's profile Send private message
probe1
Moderator
Moderator


Joined: 18 Aug 2004
Posts: 2560
Location: Chonburi Thailand Asia

PostPosted: Sun Dec 04, 2005 6:32 pm    Post subject: Reply with quote

The controller has a PageCount property, even document is not saved.
HTH
_________________
Cheers
Winfried
My Macros
DateTime2 extension: insert date, time or timestamp, formatted to your needs
Back to top
View user's profile Send private message Visit poster's website
pitonyak
Administrator
Administrator


Joined: 09 Mar 2004
Posts: 3655
Location: Columbus, Ohio, USA

PostPosted: Mon Dec 05, 2005 11:09 pm    Post subject: Reply with quote

The size of the document as stored on disk is not likely to correlate in any nice way to the actual size of the document in memory. There are many reasons for this. consider some of the following:

1. The document, as saved on disk, is stored in a "zipped" format. In other words, when you look on disk, you see the compressed size.

2. The document in memory may contain data that is linked rather than actually stored in the document. For example, consider a document that contains links to 10 images of 1MB each. The document itself does not contain the images.

These are just two reasons why the size is not likely to be the same. You could, perhaps, try to stream the file to some kind of in memory buffer and check the final size. Not certain how to do this, however.
_________________
--
Andrew Pitonyak
http://www.pitonyak.org/oo.php
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address
zaphod
Power User
Power User


Joined: 04 Nov 2004
Posts: 51
Location: Magdeburg, Germany

PostPosted: Fri Dec 30, 2005 9:37 am    Post subject: Reply with quote

My sollution is the following:
I just store the document in an OutputStream, like shown in the following code. You can get the size of the document by using the size() method of this object. The implementation of XOutputStream is described in the thread "OOo-Java: Using XInputStream...". http://www.oooforum.org/forum/viewtopic.phtml?t=13205
But now I have a huge problem with the user interface:
I want to get the buffer size a lot of times, say every 5 or 10 seconds. Each time this is done, the user interface of OOo in blocked and the status bar shows the progress of saving the document. This is extremely undesirable, because the process should not disturb the user.

How can I suppress the interaction of OOo when saving?

Code:
  public int getDocumentBufferSize(XComponent xComp) {

    OOoOutputStream oStream = new OOoOutputStream();

    com.sun.star.beans.PropertyValue[] lProperties = new com.sun.star.beans.PropertyValue[1];
    lProperties[0] = new com.sun.star.beans.PropertyValue();
    lProperties[0].Name = "OutputStream";
    lProperties[0].Value = oStream;

    com.sun.star.frame.XStorable xStore = (com.sun.star.frame.XStorable) UnoRuntime
        .queryInterface(com.sun.star.frame.XStorable.class, xComp);
    try {
      if (xStore != null) {
        xStore.storeToURL("private:stream", lProperties);
        return oStream.size();
      }
    } catch (IOException e) {
      return 0;
    }
    return 0;
  }
Back to top
View user's profile Send private message
pitonyak
Administrator
Administrator


Joined: 09 Mar 2004
Posts: 3655
Location: Columbus, Ohio, USA

PostPosted: Fri Dec 30, 2005 1:51 pm    Post subject: Reply with quote

Quote:
I want to get the buffer size a lot of times, say every 5 or 10 seconds. Each time this is done, the user interface of OOo in blocked and the status bar shows the progress of saving the document. This is extremely undesirable, because the process should not disturb the user.

Nice solution, but remember that if you want to know the size as saved on disk, then you need to compress the file. Compressing a file takes time.
_________________
--
Andrew Pitonyak
http://www.pitonyak.org/oo.php
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address
zaphod
Power User
Power User


Joined: 04 Nov 2004
Posts: 51
Location: Magdeburg, Germany

PostPosted: Sat Dec 31, 2005 4:51 am    Post subject: Reply with quote

Thanks for your well meant advices. In my use case it isn't mandatory to measure the file size you would get from the document. I need to measure, if document buffer size has changed and what the buffer quantity is in bytes. This size seems no to differ too much from the related file size, here the output of some test cases:
Code:
Document URL: file:///C:\ ... \TestFiles\OOo2.0_Writer.odt
File size in bytes:   12882
Buffer size in bytes: 12910

Document URL: file:///C:\ ...  \TestFiles\OOo2.0_Calc.ods
File size in bytes:   6108
Buffer size in bytes: 6108

Document URL: file:///C:\ ... \TestFiles\OOo1.1.5_Writer.sxw
File size in bytes:   45626
Buffer size in bytes: 44710

But I still have the problem with the deactivated user interface when calling xStore.storeToURL() ! Somehow I need to store the document unseen to a OutputStream (or get the size an other way), so the user wouldn't be disturbed by the deactivated UI by OOo. I also tried a second approach with with the XSeekable.getLength() method, which was intended to be a workaround for this problem:
Code:
   public long getSizeViaXSeekable(XFrame xFrame) throws IllegalArgumentException, IOException{   
   
    PropertyValue[] pVal = xFrame.getController().getModel().getArgs();   
    XStream xStream = null;
   
    for (int x = 0; x < pVal.length; x++) {     
      if (pVal[x].Name.equals("Stream")){
      xStream = (XStream) com.sun.star.uno.AnyConverter.toObject(XStream.class, pVal[x].Value);
      }
    }
    if (xStream != null){
      XInputStream xIn = xStream.getInputStream();
      XSeekable xSeek = (XSeekable) UnoRuntime.queryInterface(XSeekable.class, xIn);
      return xSeek.getLength();
    }   
    return 0;
  }
But there are two problems:
    1.) The getLength() of XSeekable returns the size of the file again, not the size of the modified document in the buffer. So you can edit the document for half an hour an still get the size of the file on disk, until you save the document to disk.

    2.) As implication of 1.) you get zero for an document, which has no URL to a file on disk (e.g. a new created document).
Any Idea is highly appreciated! Rolling Eyes
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group