Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a string to an inputstream in Bioclipse javascript editor?

Tags:

javascript

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)

like image 519
Samuel Lampa Avatar asked Sep 14 '10 17:09

Samuel Lampa


2 Answers

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);
like image 192
jonalv Avatar answered Nov 06 '22 05:11

jonalv


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

like image 25
Gabe Moothart Avatar answered Nov 06 '22 05:11

Gabe Moothart