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

OOo-Java: Using XInputStream...
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
r_apodaca
General User
General User


Joined: 15 Sep 2004
Posts: 34

PostPosted: Mon Oct 11, 2004 9:49 pm    Post subject: OOo-Java: Using XInputStream... Reply with quote

I'm trying to figure out how to make XInputStream interface with the Java io API, but I'm not getting very far. Specifically, how can I get an XInputStream to be used by a java.io.InputStream or a java.io.Reader? For example:

Code:

XInputStream xIn = ...; // get the XInputStream from somewhere...
InputStreamUser user = ...; // an object that uses a java.io.InputStream

InputAdapter adapter = new InputAdapter(xIn); // adapter extends java.io.InputStream

user.useInputStream(adapter);


I'd like to do this because I have a large amount of file processing that takes place in an existing library using java.io.Reader. Is this functionality part of OOo? If not, has a third party adapter library already been written?

cheers,
rich
Back to top
View user's profile Send private message Visit poster's website
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Oct 12, 2004 7:01 am    Post subject: Reply with quote

I don't know of one. An adapter class is exactly the right idea. I'm not sure how difficult it would be to just build a crude adapter.

You said you need a java.io.Reader.
Why not use a java.io.InputStreamReader? This is subclassed from java.io.Reader.
Now all you have to do is get a java.io.InputStream to initialize the java.io.InputStreamReader.

Comparing the java.io.InputStream and the XInputStream interface, it seems like a java class could be built that subclasses from java's abstract class java.io.InputStream , and its constructor takes an UNO interface XInputStream . The two don't seem that far apart.

The method [url=http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#available()]available()[/url] would call the method available().

[url=http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#close()]close()[/url] would call closeInput().

[url=http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#mark(int)]mark()[/url] would do nothing.
[url=http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#markSupported()]markSupported()[/url] would return false.

Two of the read methods, [url=http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#read()]read()[/url] and read(byte[]) can both be implemented in terms of the third read(byte[] b, int off, int len) method.

The read(byte[] b, int off, int len) method can call readSomeBytes().

The [url=http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#reset()]reset()[/url] method does nothing, since the stream is not markable.

Finally, the [url=http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#skip(long)]skip()[/url] method can call skipBytes.

Seems simple enough. It should be almost trivial to build an adapter to adapt UNO's XInputStream to a java.io.InputStream subclass. Then you can use this in the constructor call to a java.io.InputStreamReader, which gives you the java.io.Reader that your existing library needs.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
r_apodaca
General User
General User


Joined: 15 Sep 2004
Posts: 34

PostPosted: Tue Oct 12, 2004 7:14 am    Post subject: Reply with quote

Many thanks for the info. I will have a go at writing the OOInputStream adapter. I agree that the implementation would be fairly trivial.

This raises the larger issue of what mechanism is in place for Java developers to be able to directly use their existing code with the OOo API. Some other classes or sets of functionality that come to mind: Exceptions, OutputStream, Writer, Reader, and a variety of event types including those from the mouse and windowing system. Possibly rendering?

I had a look at the demo version of "Java UNO Wrapper" http://www.riess.de/juw/en/index.html and was quite disappointed in both the functionality (which doesn't appear to do what I'm thinking about) and documentation.

If no library of OOo-Java adapters exists, I would be willing to start a project to do this - but I wanted to make certain that the functionality is not available anywhere.

cheers,
rich
Back to top
View user's profile Send private message Visit poster's website
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Oct 12, 2004 7:34 am    Post subject: Reply with quote

Regarding the Java UNO Wrapper....

This seems like a nice idea. However, on the API mailing list I've read about new multiple-inheritance interfaces to be introduced in OOo. This should alleviate much of the queryInterface() business in java.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Wed Oct 13, 2004 9:53 am    Post subject: Reply with quote

DannyB wrote:
Regarding the Java UNO Wrapper....

This seems like a nice idea. However, on the API mailing list I've read about new multiple-inheritance interfaces to be introduced in OOo. This should alleviate much of the queryInterface() business in java.



Here is some interesting reading....

http://udk.openoffice.org/common/man/draft/multinherit.html
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
smash
Newbie
Newbie


Joined: 17 Oct 2004
Posts: 4

PostPosted: Sun Oct 17, 2004 1:33 pm    Post subject: Reply with quote

Hi Everyone,
I saw that a lot of you are interested in wrapper between Java InputStream and XInputStream

Here is the sample code which works perfect for me. Of course, it's just a draft, but should give idea to some of you what to do.
I use this code to load documents from input stream:

Code:

public class OOInputStream extends ByteArrayInputStream implements XInputStream,XSeekable
{
   public OOInputStream(byte[] buf)
   {
      super(buf);
   }
   public void seek(long p1) throws IllegalArgumentException, com.sun.star.io.IOException
   {
      pos=(int)p1;
   }
   public long getPosition() throws com.sun.star.io.IOException
   {
      return pos;
   }
   public long getLength() throws com.sun.star.io.IOException
   {
      return count;
   }
   public int readBytes(byte[][] p1, int p2) throws NotConnectedException, BufferSizeExceededException, com.sun.star.io.IOException
   {
      try
      {
         byte[] b=new byte[p2];
         int res=super.read(b);
         if(res>0)
         {
            if(res<p2)
            {
               byte[] b2=new byte[res];
               System.arraycopy(b,0,b2,0,res);
               b=b2;
            }
         }
         else
         {
            b=new byte[0];
            res=0;
         }
         p1[0]=b;
         return res;
      }
      catch (IOException e) {throw new com.sun.star.io.IOException(e.getMessage(),this);}
   }
   public int readSomeBytes(byte[][] p1, int p2) throws NotConnectedException, BufferSizeExceededException, com.sun.star.io.IOException
   {
      return readBytes(p1,p2);
   }
   public void skipBytes(int p1) throws NotConnectedException, BufferSizeExceededException, com.sun.star.io.IOException
   {
      skip(p1);
   }
   public int available()
   {
      return super.available();
   }
   public void closeInput() throws NotConnectedException, com.sun.star.io.IOException
   {
      try
      {
         close();
      }
      catch (IOException e) {throw new com.sun.star.io.IOException(e.getMessage(),this);}
   }
}
Back to top
View user's profile Send private message
korb
General User
General User


Joined: 01 Apr 2005
Posts: 12

PostPosted: Fri Apr 01, 2005 10:26 am    Post subject: Sending output to a Java OutputStream Reply with quote

Hey, this OOInputStream was exactly what I needed. I also needed to write my document to a Java OutputStream. Although the solution was fairly trivial, here's my code to save the next person the trouble of creating it on their own.

It seems to work OK, but I am seeing one slight issue with it (details follow code).

Code:
public class OOOutputStream extends ByteArrayOutputStream implements XOutputStream {
 
  /** Creates a new instance of OOOutputStream */
  public OOOutputStream() {
    super(32 * 1024);
  }

  public void writeBytes(byte[] values)
    throws NotConnectedException, BufferSizeExceededException, com.sun.star.io.IOException
  {
    try {
      this.write(values);
    } catch ( java.io.IOException ioe ) {
      throw(new com.sun.star.io.IOException(ioe.getMessage()));
    }
  }

  public void closeOutput()
    throws NotConnectedException, BufferSizeExceededException, com.sun.star.io.IOException
  {
    try {
      super.flush();
      super.close();
    } catch ( java.io.IOException ioe ) {
      throw(new com.sun.star.io.IOException(ioe.getMessage()));
    }
  }

  public void flush() {
    try {
      super.flush();
    } catch ( java.io.IOException ignored ) {}
  }
}



OK, now the issue.

I am able to open a document thusly:

Code:
xComponentLoader.loadComponentFromURL("private:stream", "_blank", 0, loadProps);


without any problem, and I specify an InputStream and ReadOnly as my loadProps.

I then am able to write the document out to a stream as text by choosing the appropriate filter after setting my OutputStream, FilterName, and FilterProperties (e.g., FilterName is "Text" for a Writer document, "Text - txt - csv (StarCalc)" for a Calc document).

This all works fine, except when I make one simple change: If I also add the Hidden (true) property to my loadProps, the resultant OutputStream will not contain any data when the InputStream was a Calc document. If the InputStream was a Writer document, the Hidden property makes no difference.

I've double-checked, and my calc document, as loaded, does include the data that I'd expect.

I also had a suspicion that since my spreadsheet was not visible, perhaps the filter didn't know what the current sheet was, so it didn't know what to export. So I went to the trouble of makeing the first sheet in the spreadsheet active, and then writing to my OutputStream, but again, I received no output.

So the bottom line is, I am able to convert the document on my InputStream to text on my OutputStream, but I'd like to remove the overhead of having to draw the UI in order to be able to perform the conversion.

Has anyone else seen this behavior?

Thanks,
Bill[/b]
Back to top
View user's profile Send private message
korb
General User
General User


Joined: 01 Apr 2005
Posts: 12

PostPosted: Fri Apr 01, 2005 10:49 am    Post subject: Hmmm...just sxc spreadsheets! Reply with quote

I was playing around some more, and this is even stranger than I first thought. My OutputStream only yields no output if the Calc spreadsheet I'm converted is an sxc - if it's an Excel xls, it works fine with Hidden=true.

Weird.

Bill
Back to top
View user's profile Send private message
hol.sten
Super User
Super User


Joined: 14 Nov 2004
Posts: 3533
Location: Hamburg, Germany

PostPosted: Fri Apr 01, 2005 11:39 am    Post subject: Re: Sending output to a Java OutputStream Reply with quote

korb wrote:
Although the solution was fairly trivial, here's my code to save the next person the trouble of creating it on their own.

Thanks for the code. I'll try it and see if it suits me too Smile

korb wrote:
So the bottom line is, I am able to convert the document on my InputStream to text on my OutputStream, but I'd like to remove the overhead of having to draw the UI in order to be able to perform the conversion.

Quick "work around idea": Have you tried to start OOo in headless mode? With the parameter "-headless" OOo displays no UI at all (besides the error report, but you can disable him too).

With kind regards
hol.sten
Back to top
View user's profile Send private message
korb
General User
General User


Joined: 01 Apr 2005
Posts: 12

PostPosted: Fri Apr 01, 2005 12:21 pm    Post subject: headless Reply with quote

I did find the "-invisible" flag, which I think is the same thing (I tried both and saw the same results). However, that only makes the initial window invisible - when I open my new document, it does, in fact, render an OOo frame with my spreadsheet displayed.

Thanks for the suggestion, though.

Bill
Back to top
View user's profile Send private message
hol.sten
Super User
Super User


Joined: 14 Nov 2004
Posts: 3533
Location: Hamburg, Germany

PostPosted: Fri Apr 01, 2005 1:41 pm    Post subject: Re: headless Reply with quote

korb wrote:
I did find the "-invisible" flag, which I think is the same thing

Regards this german source (I haven't found an appropriate in english) http://de.openoffice.org/doc/setupguide/1.1/installations_handbuch6.html (Section "Starten von OpenOffice.org mit Parametern"), there is a difference between both parameters:
1) -invisible suppresses splash screen and initial OOo window, but it is possible to open dialogs and windows through the API.
2) -headless suppresses the UI completely.

korb wrote:
(I tried both and saw the same results). However, that only makes the initial window invisible - when I open my new document, it does, in fact, render an OOo frame with my spreadsheet displayed.

I never experienced that! I don't use "-invisible" but I use "-headless" and I never see any OOo windows or dialogs. So I'm really baffled, that you say that it has the same result.

korb wrote:
Thanks for the suggestion

Thanks for your code. It works perfectly for me. I never thought that writing to an OutputStream instead of writing to a file might be so easy. The next step will be to compare the performance between writing to files and writing to OutputStreams.

With kind regards
hol.sten
Back to top
View user's profile Send private message
korb
General User
General User


Joined: 01 Apr 2005
Posts: 12

PostPosted: Fri Apr 01, 2005 3:32 pm    Post subject: Reply with quote

hol.sten,

As my German is not so good (sorry, but my English isn't great, either! Wink ) this is the best documentation I can come up with, by running soffice with the "-help" flag at the Unix (SuSE Linux 9.2 Pro) shell prompt:

    345$ /opt/OpenOffice.org1.1.4/program/soffice -help
    OpenOffice.org 1.1.4 645m52(Build:8824)

    Usage: soffice [options] [documents...]

    Options:

    -minimized keep startup bitmap minimized.
    -invisible no startup screen, no default document and no UI.
    -norestore suppress restart/restore after fatal errors.
    -quickstart starts the quickstart service
    -nologo don't show startup screen.
    -nolockcheck don't check for remote instances using the installation
    -nodefault don't start with an empty document
    -headless like invisible but no userinteraction at all.
    (stuff deleted for brevity)



This would seem to agree with what you're saying (i.e., -headless means no UI at all) but yet, I see one. Perhaps you are running on some other OS or a different OOo version?

In any case, I'm stuck with a UI for the time being, I guess.

Thanks again,
Bill
Back to top
View user's profile Send private message
hol.sten
Super User
Super User


Joined: 14 Nov 2004
Posts: 3533
Location: Hamburg, Germany

PostPosted: Fri Apr 01, 2005 11:50 pm    Post subject: Reply with quote

korb wrote:
    345$ /opt/OpenOffice.org1.1.4/program/soffice -help
    OpenOffice.org 1.1.4 645m52(Build:8824)

    Usage: soffice [options] [documents...]

    Options:

    -invisible no startup screen, no default document and no UI.
    ...
    -headless like invisible but no userinteraction at all.

This would seem to agree with what you're saying (i.e., -headless means no UI at all) but yet, I see one. Perhaps you are running on some other OS or a different OOo version?

Yeah, your right. I'm on different OS: Windows XP, Solaris (with xvfb, so it's really hard to see anything Wink ) and Debian unstable and on different OOo: 1.1.2, 1.1.3 and 1.1.4. Perhaps I haven't got your problem, because I only use writer. And that is also no problem to you...

korb wrote:
In any case, I'm stuck with a UI for the time being, I guess.

Just another guess: I think, you are using a OOo RPM provided with your SuSE. Have you thought about uninstalling that RPM and install a Linux-tar.gz from http://download.openoffice.org/1.1.4/?

With kind regards
hol.sten
Back to top
View user's profile Send private message
korb
General User
General User


Joined: 01 Apr 2005
Posts: 12

PostPosted: Sat Apr 02, 2005 6:13 am    Post subject: OOo installation Reply with quote

Actually, the version included with SuSE 9.2 was 1.1.3 - I downloaded and installed OOo_1.1.4_LinuxIntel_install.tar.gz from the OOo web site.

However, this is just my development platform - my production deployment will be similar to yours (as you mentioned Solaris, I suspect that is your production OS?). I'm using VNC rather than Xvfb, but other than that, it's pretty close.

Even with the output going to the VNC desktop rather than some "visible" X display, the UI code must still be executed even if it doesn't appear for somebody to see. I suspect that there's a performance penalty, however small, as a result.

Bill
Back to top
View user's profile Send private message
hol.sten
Super User
Super User


Joined: 14 Nov 2004
Posts: 3533
Location: Hamburg, Germany

PostPosted: Sat Apr 02, 2005 7:18 am    Post subject: Re: OOo installation Reply with quote

korb wrote:
as you mentioned Solaris, I suspect that is your production OS?

Yeah, your right! And in comparison to Linux and Windows XP it is the most unreliable platform regarding OOo and Java via UNO.

korb wrote:
Even with the output going to the VNC desktop rather than some "visible" X display, the UI code must still be executed even if it doesn't appear for somebody to see. I suspect that there's a performance penalty, however small, as a result.

Yeah, your right again, I guess. But is't much better than doing all the UI drawing.

With kind regards
hol.sten
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
Goto page 1, 2, 3  Next
Page 1 of 3

 
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