Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

44
LINES

< > BotCompany Repo | #1001571 // getProgramOutput

JavaX fragment (include)

1  
// This will behave differently on desktop and Android. On the
2  
// desktop, it shows the program's output as it runs. On Android,
3  
// it does not.
4  
5  
static S getProgramOutput(S progID) {
6  
  if (isAndroid()) {
7  
    PrintStream oldOut = System.out;
8  
    PrintStream oldErr = System.err;
9  
    new ByteArrayOutputStream baos;
10  
    PrintStream ps = new PrintStream(baos, true);
11  
    try {
12  
      System.setOut(ps);
13  
      System.setErr(ps);
14  
      callMain(hotwire(progID));
15  
      ps.flush();
16  
      ret fromUtf8(baos.toByteArray()); // XXX - is this right?
17  
    } finally {
18  
      System.setOut(oldOut);
19  
      System.setErr(oldErr);
20  
    }
21  
  } else {
22  
    Class j = getJavaX();
23  
    Appendable oldSystemOut = (Appendable) get(j, "customSystemOut");
24  
    final new StringBuffer buf;
25  
    set(j, "customSystemOut", new Appendable() {
26  
    
27  
      // only using this one
28  
      public Appendable append(CharSequence cs) {
29  
        buf.append(cs.toString());
30  
        return this;
31  
      }
32  
          
33  
      public Appendable append(char c) { return this; }
34  
      public Appendable append(CharSequence s, int start, int end) { return this; }
35  
      
36  
    });
37  
    try {
38  
      callMain(hotwire(progID));
39  
      ret buf.toString();
40  
    } finally {
41  
      set(j, "customSystemOut", oldSystemOut);
42  
    }
43  
  }
44  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1001571
Snippet name: getProgramOutput
Eternal ID of this version: #1001571/1
Text MD5: 10e2d0f2c60ae986af242f5ad1918b1f
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-25 02:59:13
Source code size: 1308 bytes / 44 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 542 / 758
Referenced in: [show references]