use of org.jaggeryjs.hostobjects.stream.StreamHostObject in project jaggery by wso2.
the class WebSocketHostObject method processBinary.
public void processBinary(ByteBuffer byteBuffer) {
if (binaryCallback == null) {
return;
}
Context cx = RhinoEngine.enterContext(this.contextFactory);
RhinoEngine.putContextProperty(EngineConstants.JAGGERY_CONTEXT, this.asyncContext);
ByteArrayInputStream bis = new ByteArrayInputStream(byteBuffer.array());
StreamHostObject sho = (StreamHostObject) cx.newObject(this, "Stream", new Object[] { bis });
binaryCallback.call(cx, this, this, new Object[] { sho });
RhinoEngine.exitContext();
}
use of org.jaggeryjs.hostobjects.stream.StreamHostObject in project jaggery by wso2.
the class FileHostObject method jsFunction_write.
public static // NOPMD
void jsFunction_write(// NOPMD
Context cx, // NOPMD
Scriptable thisObj, // NOPMD
Object[] args, // NOPMD
Function funObj) throws ScriptException {
String functionName = "write";
int argsCount = args.length;
if (argsCount != 1) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
}
Object data = args[0];
FileHostObject fho = (FileHostObject) thisObj;
if (data instanceof InputStream) {
fho.file.write((InputStream) data);
} else if (data instanceof StreamHostObject) {
fho.file.write(((StreamHostObject) data).getStream());
} else {
fho.file.write(HostObjectUtil.serializeObject(args[0]));
}
}
use of org.jaggeryjs.hostobjects.stream.StreamHostObject in project jaggery by wso2.
the class JaggeryParserHostObject method jsFunction_parse.
/**
* Parses a Jaggery string | stream passed into the parse method and returns a parsed stream
*/
public static Scriptable jsFunction_parse(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "parse";
int argsCount = args.length;
if (argsCount != 1) {
HostObjectUtil.invalidNumberOfArgs(HOSTOBJECT_NAME, functionName, argsCount, false);
}
InputStream source = null;
if (args[0] instanceof String) {
source = new ByteArrayInputStream(((String) args[0]).getBytes());
} else if (args[0] instanceof StreamHostObject) {
source = ((StreamHostObject) args[0]).getStream();
} else {
HostObjectUtil.invalidArgsError(HOSTOBJECT_NAME, functionName, "1", "string|stream", args[0], false);
}
return cx.newObject(thisObj, "Stream", new Object[] { JaggeryParser.parse(source) });
}
use of org.jaggeryjs.hostobjects.stream.StreamHostObject in project jaggery by wso2.
the class CommonManager method print.
/**
* JaggeryMethod responsible of writing to the output stream
*/
public static void print(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "print";
JaggeryContext jaggeryContext = getJaggeryContext();
int argsCount = args.length;
if (argsCount != 1) {
HostObjectUtil.invalidNumberOfArgs("RhinoTopLevel", functionName, argsCount, false);
}
OutputStream out = (OutputStream) jaggeryContext.getProperty(CommonManager.JAGGERY_OUTPUT_STREAM);
if (args[0] instanceof StreamHostObject) {
InputStream in = ((StreamHostObject) args[0]).getStream();
if (in instanceof FileInputStream) {
// if form file we will use channel since it's faster
ReadableByteChannel inputChannel = null;
WritableByteChannel outputChannel = null;
FileChannel fc = null;
try {
inputChannel = Channels.newChannel(in);
outputChannel = Channels.newChannel(out);
fc = (FileChannel) inputChannel;
fc.transferTo(0, fc.size(), outputChannel);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
} finally {
clearResources(fc, inputChannel, outputChannel, in, out);
}
} else {
try {
IOUtils.copy(in, out);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
} finally {
clearResources(in, out);
}
}
} else {
try {
out.write(HostObjectUtil.serializeObject(args[0]).getBytes());
} catch (IOException e) {
if (log.isDebugEnabled()) {
log.debug(e.getMessage(), e);
}
throw new ScriptException(e);
}
}
}
use of org.jaggeryjs.hostobjects.stream.StreamHostObject in project jaggery by wso2.
the class WebSocketHostObject method jsFunction_send.
public static void jsFunction_send(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "send";
int argsCount = args.length;
if (argsCount != 1) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
}
if (!(args[0] instanceof String) && !(args[1] instanceof StreamHostObject)) {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "1", "string | Stream", args[0], false);
}
WebSocketHostObject who = (WebSocketHostObject) thisObj;
if (args[0] instanceof String) {
try {
who.inbound.getWsOutbound().writeTextMessage(CharBuffer.wrap((String) args[0]));
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
} else {
StreamHostObject sho = (StreamHostObject) args[0];
InputStream is = sho.getStream();
try {
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) != -1) {
who.inbound.getWsOutbound().writeBinaryMessage(ByteBuffer.wrap(buffer, 0, length));
}
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
}
}
Aggregations