From 98e2821b38a775737e42a2479a6bc65107210859 Mon Sep 17 00:00:00 2001 From: Elliot Kroo Date: Thu, 11 Mar 2010 15:21:30 -0800 Subject: reorganizing the first level of folders (trunk/branch folders are not the git way :) --- .../framework-src/modules/process.js | 91 ---------------------- 1 file changed, 91 deletions(-) delete mode 100644 trunk/infrastructure/framework-src/modules/process.js (limited to 'trunk/infrastructure/framework-src/modules/process.js') diff --git a/trunk/infrastructure/framework-src/modules/process.js b/trunk/infrastructure/framework-src/modules/process.js deleted file mode 100644 index 48ab62e..0000000 --- a/trunk/infrastructure/framework-src/modules/process.js +++ /dev/null @@ -1,91 +0,0 @@ -/** - * Simple way to execute external commands through javascript - * - * @example - cmd = exec("cat"); - System.out.println("First: " +cmd.write("this is a loop.").read(Process.READ_AVAILABLE)); // prints "this is a loop." - System.out.println("Second: " +cmd.writeAndClose(" hi there").result()); // prints "this is a loop. hi there" - * - */ - -jimport("java.lang.Runtime"); -jimport("java.io.BufferedInputStream"); -jimport("java.io.BufferedOutputStream"); -jimport("java.lang.System"); - -/* returns a process */ -function exec(process) { - return new Process(process); -}; - -function Process(cmd) { - this.cmd = cmd; - this.proc = Runtime.getRuntime().exec(cmd); - this.resultText = ""; - this.inputStream = new BufferedInputStream(this.proc.getInputStream()); - this.errorStream = new BufferedInputStream(this.proc.getErrorStream()); - this.outputStream = new BufferedOutputStream(this.proc.getOutputStream()); -} - -Process.CHUNK_SIZE = 1024; -Process.READ_ALL = -1; -Process.READ_AVAILABLE = -2; - -Process.prototype.write = function(stdinText) { - this.outputStream.write(new java.lang.String(stdinText).getBytes()); - this.outputStream.flush(); - return this; -}; - -Process.prototype.writeAndClose = function(stdinText) { - this.write(stdinText); - this.outputStream.close(); - return this; -}; - -/* Python file-like behavior: read specified number of bytes, else until EOF*/ -Process.prototype.read = function(nbytesToRead, stream) { - var inputStream = stream || this.inputStream; - var availBytes = inputStream.available(); - if (!availBytes) return null; - - var result = ""; - var nbytes = nbytesToRead || Process.READ_ALL; - var readAll = (nbytes == Process.READ_ALL); - var readAvailable = (nbytes == Process.READ_AVAILABLE); - while (nbytes > 0 || readAll || readAvailable) { - var chunkSize = readAll ? Process.CHUNK_SIZE : - readAvailable ? Process.CHUNK_SIZE : nbytes; - - // allocate a java byte array - var bytes = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, chunkSize); - - var len = inputStream.read(bytes, 0, chunkSize); - - // at end of stream, or when we run out of data, stop reading in chunks. - if (len == -1) break; - if (nbytes > 0) nbytes -= len; - - result += new java.lang.String(bytes); - - if (readAvailable && inputStream.available() == 0) break; - } - - this.resultText += new String(result); - return new String(result); -}; - -Process.prototype.result = function() { - this.outputStream.close(); - this.proc.waitFor(); - this.read(Process.READ_ALL, this.inputStream); - return new String(this.resultText); -}; - -Process.prototype.resultOrError = function() { - this.proc.waitFor(); - this.read(Process.READ_ALL, this.inputStream); - var result = this.resultText; - if(!result || result == "") result = this.read(Process.READ_ALL, this.errorStream); - return result || ""; -}; -- cgit v1.2.3-1-g7c22