carosellini.rJava.JRI.0.9-7.source-code.RConsoleOutputStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of JRI Show documentation
Show all versions of JRI Show documentation
JRI is a Java/R Interface, which allows to run R inside Java applications as a single thread. Basically it loads R dynamic library into Java and provides a Java API to R functionality. It supports both simple calls to R functions and a full running REPL.
The newest version!
// RConsoleOutputStream, part of Java/R Interface
//
// (C)Copyright 2007 Simon Urbanek
//
// For licensing terms see LICENSE in the root if the JRI distribution
package org.rosuda.JRI;
import java.io.OutputStream;
import java.io.IOException;
/** RConsoleOutputStream provides an OutputStream which causes its output to be written to the R console. It is a pseudo-stream as there is no real descriptor connected to the R console and thusly it is legal to have multiple console streams open. The synchonization happens at the RNI level.Note that stdout/stderr are not connected to the R console by default, so one way of using this stream is to re-route Java output to R console:
System.setOut(new PrintStream(new RConsoleOutputStream(engine, 0)));
System.setErr(new PrintStream(new RConsoleOutputStream(engine, 1)));
@since JRI 0.4-0
*/
public class RConsoleOutputStream extends OutputStream {
Rengine eng;
int oType;
boolean isOpen;
/** opens a new output stream to R console
@param eng R engine
@param oType output type (0=regular, 1=error/warning) */
public RConsoleOutputStream(Rengine eng, int oType) {
this.eng = eng;
this.oType = oType;
isOpen = true;
}
public void write(byte[] b, int off, int len) throws IOException {
if (!isOpen) throw new IOException("cannot write to a closed stream");
if (eng == null) throw new IOException("missing R engine");
String s = new String(b, off, len);
eng.rniPrint(s, oType);
}
public void write(byte[] b) throws IOException { write(b, 0, b.length); }
public void write(int b) throws IOException { write(new byte[] { (byte)(b&255) }); }
public void close() throws IOException { isOpen=false; eng=null; }
}