catching console output into an array
in
Share your Work
•
5 months ago
as the result of another thread, i came up with with a class that once intanciated will capture a configurable amount of lines of console (as in
prinln()) output to an
ArrayList. i figured this might be useful to someone.
- import java.io.ByteArrayOutputStream;
- import java.io.PrintStream;
- public class Console{
- private PApplet parent;
- private ByteArrayOutputStream baos = new ByteArrayOutputStream();
- private PrintStream printStream = new PrintStream(this.baos);
- private PrintStream original= System.out;
- private int streamSizeDummy= 0;
- private int maxLines= 0;
- public ArrayList<String> buffer;
- Console(PApplet parent, int lines){
- this.parent= parent;
- this.parent.registerMethod("pre", this);
- this.buffer= new ArrayList();
- this.maxLines= lines;
- System.setOut(this.printStream);
- }
- public void pre(){
- if(this.baos.size() > this.streamSizeDummy){
- String[] dummy= splitTokens(baos.toString());
- for(int i=0; i<dummy.length; i++){
- this.buffer.add(dummy[i]);
- if(this.buffer.size() > this.maxLines){
- this.buffer.remove(0);
- }
- }
- System.setOut(this.original);
- println(join(dummy, "\n"));
- System.setOut(printStream);
- this.baos.reset();
- }
- }
- }