Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Can someone Explain this code to me?

  Asked By: Emily    Date: Jul 31    Category: Java    Views: 832
  

This is some code from the javaalmanac.com to make a java console
window.

It redirects System.err and System.out to the Scrollable text area.

I want to add functionality to redirect System.in there also.

I understand the PipedInputStreams and PipedOutputStreams and how
they created the text area and the Scrollable text area.

The ReaderThread part I understand that they read each byte of data
into an array and display it as a string from System.out and
System.in.
Ignore the code referring to System.in it is mine and does not work.
Thanks in advance for your help and time.

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;


public class ConsoleJ extends JFrame {
PipedInputStream piOut;
PipedInputStream piErr;
// InputStream piIn;
PipedOutputStream poOut;
PipedOutputStream poErr;
// InputStreamReader poIn;



JTextArea textArea = new JTextArea();

public ConsoleJ() throws IOException {
// Set up System.out
piOut = new PipedInputStream();
poOut = new PipedOutputStream(piOut);
System.setOut(new PrintStream(poOut, true));

// Set up System.err
piErr = new PipedInputStream();
poErr = new PipedOutputStream(piErr);
System.setErr(new PrintStream(poErr, true));

// Set up System.in
piIn = new InputStream();
poIn = new InputStreamReader(piIn);
System.setIn(piIn);

// Add a scrolling text area
textArea.setEditable(false);
textArea.setRows(20);
textArea.setColumns(50);
getContentPane().add(new JScrollPane(textArea),
BorderLayout.CENTER);
pack();
setVisible(true);

// Create reader threads
new ReaderThread(piOut).start();
new ReaderThread(piErr).start();
// new ReaderThread(piIn).start();
} //end console

class ReaderThread extends Thread {
PipedInputStream pi;
InputStreamReader ir;

ReaderThread(InputStreamReader ir) {
this.ir = ir;

}

ReaderThread(PipedInputStream pi) {
this.pi = pi;

}

public void run() {

// final BufferedReader stdin = new BufferedReader();
final byte[] buf = new byte[1024];
try {
while (true) {
// final String input = stdin.readLine();
final int len = pi.read(buf);
if (len == -1) {
break;
} //end if
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textArea.append(new String(buf, 0,
len));
//textArea.append(input);
// Make sure the last line is always
visible
textArea.setCaretPosition
(textArea.getDocument().getLength());

// Keep the text area down to a
certain character size
int idealSize = 1000;
int maxExcess = 500;
int excess = textArea.getDocument
().getLength() - idealSize;
if (excess >= maxExcess) {
textArea.replaceRange("", 0,
excess);
}
}
}); //end swing util
}
} catch (IOException e) {
}
}
}


public static void main(String[] args)
{
//The console window is created using
try {
new ConsoleJ();
//System.exit(0);
} catch (IOException e) {}
}

}

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on Can someone Explain this code to me? Or get search suggestion and latest updates.




Tagged: