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

44
LINES

< > BotCompany Repo | #1001571 // getProgramOutput

JavaX fragment (include)

// This will behave differently on desktop and Android. On the
// desktop, it shows the program's output as it runs. On Android,
// it does not.

static S getProgramOutput(S progID) {
  if (isAndroid()) {
    PrintStream oldOut = System.out;
    PrintStream oldErr = System.err;
    new ByteArrayOutputStream baos;
    PrintStream ps = new PrintStream(baos, true);
    try {
      System.setOut(ps);
      System.setErr(ps);
      callMain(hotwire(progID));
      ps.flush();
      ret fromUtf8(baos.toByteArray()); // XXX - is this right?
    } finally {
      System.setOut(oldOut);
      System.setErr(oldErr);
    }
  } else {
    Class j = getJavaX();
    Appendable oldSystemOut = (Appendable) get(j, "customSystemOut");
    final new StringBuffer buf;
    set(j, "customSystemOut", new Appendable() {
    
      // only using this one
      public Appendable append(CharSequence cs) {
        buf.append(cs.toString());
        return this;
      }
          
      public Appendable append(char c) { return this; }
      public Appendable append(CharSequence s, int start, int end) { return this; }
      
    });
    try {
      callMain(hotwire(progID));
      ret buf.toString();
    } finally {
      set(j, "customSystemOut", oldSystemOut);
    }
  }
}

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: 538 / 754
Referenced in: [show references]