views:

52

answers:

2

I'm trying to save an a string to a file in with a javascript in the Bioclipse workbench, by using

ui.save( "filename", "my string" );

...but get an error that ui.save takes only an inputstream as the second parameter. How can I convert a string to an inputstream in the Bioclipse javascript context?

(Btw, I think Bioclipse uses the Rhino Javascript implementation)

+2  A: 

Here's a page describing java interop using Rhino: http://www.mozilla.org/rhino/ScriptingJava.html

Gabe Moothart
+2  A: 

In this situation we have to fall back to Java.

You are trying to call the method ui.save which according to man ui.save looks like this:

> man ui.save
---------------------------------------------
ui.save(String filePath, InputStream content)
---------------------------------------------
Save the content of the InputStream to the given path.

So this method wants an InputStream. Rhino allows us to instantiate Java objects. This can probably be made much nicer...

var stream = new java.io.ByteArrayInputStream(
                      new java.lang.String("Example String").getBytes("UTF-8") );

And then we call the method with this stream, (and an existing path where to save the file)

ui.save("/test/test.txt", stream);
jonalv