diff options
author | Egil Moeller <egil.moller@freecode.no> | 2010-03-22 00:16:53 +0100 |
---|---|---|
committer | Egil Moeller <egil.moller@freecode.no> | 2010-03-22 00:16:53 +0100 |
commit | fc00cfedd6c25d1634a1e0d28a7b7bc5f42e1cd7 (patch) | |
tree | 928419d6bfe794865e4b8f0634bd792f795c4b8a /trunk/infrastructure/framework-src/modules/process.js | |
parent | 0fdab3db005c4fa31fb8306b573c0b35073d3e56 (diff) | |
parent | d56b9b3b82cdebcaeb00eec0fcb4326ad21adaa8 (diff) | |
download | etherpad-fc00cfedd6c25d1634a1e0d28a7b7bc5f42e1cd7.tar.gz etherpad-fc00cfedd6c25d1634a1e0d28a7b7bc5f42e1cd7.tar.xz etherpad-fc00cfedd6c25d1634a1e0d28a7b7bc5f42e1cd7.zip |
Merge branch 'master' into plugin-framework
Diffstat (limited to 'trunk/infrastructure/framework-src/modules/process.js')
-rw-r--r-- | trunk/infrastructure/framework-src/modules/process.js | 91 |
1 files changed, 0 insertions, 91 deletions
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 || ""; -}; |