0

I have a prolog file (Expert System) that I consult from Java using Jpl libraries (org.jpl7.*) and I have an UI where I want to show the output of prolog's queries. This is my Custom Output Stream that should redirect every console content into my interface (jTextAreaOUTPUT is the place where i redirect the content)

public class CustomOutputStream extends OutputStream {
 private JTextArea jTextAreaOUTPUT;

 public CustomOutputStream(JTextArea textArea) {
    jTextAreaOUTPUT = textArea;
 }

 @Override
 public void write(int b) throws IOException {
    // redirects data to the text area
    jTextAreaOUTPUT.append(String.valueOf((char)b));
  // scrolls the text area to the end of data
    jTextAreaOUTPUT.setCaretPosition(jTextAreaOUTPUT.getDocument().getLength());
 }
}

This are some lines I have in my Interface Class: this calls the Custom Output Stream methond:

PrintStream printStream = new PrintStream(new CustomOutputStream(jTextAreaOUTPUT), true, "UTF-8");
 // keeps reference of standard output stream
 PrintStream standardOut = System.out;
 System.setOut(printStream);
 System.setErr(printStream);

For some strange reasons it doesn't work whith this prolog file (I tried with other and It works): UI freezes and content keeps showing in java console (eclipse). The Expert System file works with write instruction in Prolog (e.g. write('Lorem Ipsum') )

  1. Why standardOut in never used ? Is it ok declared this way?
  2. Is there a way to force redirect for all the text that should be written in eclipse console?

I also tried to use " write Stream " method in prolog, but (only for this prolog file, maybe due to recursion) UI freezes even though outpus is written on a txt file.

1 Answers1

0

You might need to override the other write functions in outputstream write(byte[] b), write(byte[] b, int off, int len) if the writer doesn't write one character at a time

To override the other write functions of OutputStream just provide similar code to the single character function you already wrote:

public class CustomOutputStream extends OutputStream {

    private JTextArea jTextAreaOUTPUT;

    public CustomOutputStream(JTextArea textArea) {
        jTextAreaOUTPUT = textArea;
    }

    @Override
    public void write(int b) throws IOException {
        // redirects data to the text area
        jTextAreaOUTPUT.append(String.valueOf((char) b));
        // scrolls the text area to the end of data
        jTextAreaOUTPUT.setCaretPosition(jTextAreaOUTPUT.getDocument().getLength());
    }

    @Override
    public void write(byte[] b, int off, int len) throws IOException {
        // redirects data to the text area
        jTextAreaOUTPUT.append(new String(b, off, len));
        // scrolls the text area to the end of data
        jTextAreaOUTPUT.setCaretPosition(jTextAreaOUTPUT.getDocument().getLength());
    }

    @Override
    public void write(byte[] b) throws IOException {
        write(b,0,b.length);
    }

}
WillShackleford
  • 6,918
  • 2
  • 17
  • 33