aboutsummaryrefslogtreecommitdiffstats
path: root/infrastructure/framework-src/modules/process.js
blob: 48ab62ed3169936f1564983cf444d0443bc62300 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
 * 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 || "";
};