Tuesday, February 22, 2011

java intercept the standard output (1)

 Intercepted in the Java console output
program content:
one, Java pipeline flow
1.1 a
1.2 Note Note Note II
1.3 to solve the problem three
1.4
Second, Java console output
captured three other procedures to capture reference
console output

Yuliang Song About the Author (javaman@163.net)
software engineer, independent consultant and Freelance writer
2001 年 10 month in Java development, console output is still an important tool, but the default console output with various constraints. article describes how to intercept the console with the Java pipeline flow output, the application of pipeline flow problems should be noted, provides the interception of non-Java programs Java program and an instance of the console output.
graphical user interface, even in today's dominant console in the Java program output is still occupied important position. console is not only a Java program the default stack trace and error messages output window, but also a useful debugging tool (especially accustomed to using the println () of the people). However, there are many console window limitations. For example, in Windows 9x platforms, DOS can only accommodate 50 lines of console output. If the Java program one-time large amounts of content to the console output, to view the content very difficult.
javaw for the use of the development of this launcher are concerned, the console window particularly valuable. because javaw start with java program, would not have a console window appears. If the program has encountered a problem and throw an exception and can not view the Java runtime environment is written to System.out or System.err call stack trace information. In order to capture the stack, some people take the use of try / catch () block package main () the way, but this approach does not always work, some of the Java run-time time, some descriptive error message will be written before the exception is thrown System.out and System.err; unless the two consoles to monitor the flow of information will not be able to see otherwise.
Therefore, some time check Java Runtime Environment (or third-party program) to write data to the console stream and take the appropriate action is essential. one of the themes discussed in this paper is to create such an input stream from this input stream can be read into the past Write Java Console flow (or any other program's output stream) of data. We can imagine the data is written to the output stream to enter in the form of immediate A Swing-based console output text window. In the meantime, we will discuss some of the Flow and Java (PipedInputStream and PipedOutputStream) of important considerations. Figure I shows the text used to intercept and display the output of the Java console program, the user interface is the core of a JTextArea. Finally, we would create a program to capture and display of other (non-Java programs can be) a simple process console output.
Figure I: Multi-threaded console output intercept A program
, Java pipeline flow
to display console output in a text box, we must use some method of System.out and System.err into all content. If you are familiar with Java, pipe flow PipedInputStream and PipedOutputStream, will believe that we already have the most effective tools.
PipedOutputStream output stream to write to the data available from the corresponding The PipedInputStream input stream to read. Java pipeline flow greatly facilitated our intercept console output. Listing 1 shows a very simple console output intercept program.
【Listing 1: Console output with the pipeline flow interception 】
PipedInputStream pipedIS = new PipedInputStream ();
PipedOutputStream pipedOS = new PipedOutputStream ();
try {
pipedOS.connect (pipedIS);
}
catch (IOException e) {
System.err.println ((ps);
System.setErr (ps);
can see the code here is extremely simple. We just built a PipedInputStream, set it to all of the data stream written to console the final destination . All the data is written to the console stream have been transferred PipedOutputStream, so read from the corresponding PipedInputStream can quickly capture all of the data stream written to the console. Next thing seems to only in the Swing JTextArea Display data read from the pipedIS stream, get a console that displays output in a text box program. Unfortunately, when using the Java pipe flow there are some important considerations. Only serious about all of these precautions to ensure Listing a stable operation code. Here we look at the first note.
1.1 Note the use of a
PipedInputStream is a 1024-byte fixed-size circular buffer. write the data is actually saved to PipedOutputStream PipedInputStream corresponding internal buffer. PipedInputStream perform read from when the data is actually read from the internal buffer. PipedInputStream if the corresponding input buffer is full, any attempt to write PipedOutputStream thread will be blocked. and The write thread will block until the read operation PipedInputStream delete data from the buffer.
This means writing data to the PipedOutputStream thread should not be responsible for reading data from the corresponding PipedInputStream the only thread. from Figure II can clearly see the problem here: Suppose thread t is responsible only to read data from PipedInputStream thread; In addition, assuming that once PipedOutputStream t attempt the write () method call to write to the corresponding PipedOutputStream 2000 bytes of data. in t thread blocked before it can write up to 1024 bytes of data (PipedInputStream internal buffer size). However, once t is blocked, read PipedInputStream operation it will never appear Because t is the only thread to read PipedInputStream. This, t thread has been completely blocked, while all other attempts to write data to the PipedOutputStream thread will encounter the same situation.
Figure II: The pipeline flow of work processes
This does not mean that in a write () call can not write more than 1024 bytes of data. but it should be guaranteed, in writing data at the same time, there is another thread to read data from PipedInputStream.
Listing 2 demonstrates this problem. The program reads alternately with a thread and write PipedInputStream PipedOutputStream. Each call to write () buffer to PipedInputStream write 20 bytes, each call to read () read from the buffer only access and remove the 10 bytes. internal buffer will eventually be filled, resulting in a write block. As we read the same thread, a write operation, once the write operation is blocked, you can not read data from the PipedInputStream. < br> 【Listing 2: A thread with the same read / write operation causes the thread to block】
import java.io. *;

No comments:

Post a Comment