这是一个正常运行的类。你可以使用以下方法将其实例安装到系统中,然后执行err:
PrintStream con=new PrintStream(new textareaOutputStream(...));System.setOut(con);System.setErr(con);
方法中正确记录和删除了文本区域中的行,
run()以避免在控制台充斥输出时可能发生的追加和删除之间的竞争状况。最终结果对我来说也更干净。
import java.awt.*;import java.io.*;import java.util.*;import java.util.List;import javax.swing.*;public class textareaOutputStreamextends OutputStream{// *************************************************************************************************// INSTANCE MEMBERS// *************************************************************************************************private byte[] oneByte; // array for write(int val);private Appender appender; // most recent actionpublic textareaOutputStream(Jtextarea txtara) { this(txtara,1000); }public textareaOutputStream(Jtextarea txtara, int maxlin) { if(maxlin<1) { throw new IllegalArgumentException("textareaOutputStream maximum lines must be positive (value="+maxlin+")"); } oneByte=new byte[1]; appender=new Appender(txtara,maxlin); }public synchronized void clear() { if(appender!=null) { appender.clear(); } }public synchronized void close() { appender=null; }public synchronized void flush() { }public synchronized void write(int val) { oneByte[0]=(byte)val; write(oneByte,0,1); }public synchronized void write(byte[] ba) { write(ba,0,ba.length); }public synchronized void write(byte[] ba,int str,int len) { if(appender!=null) { appender.append(bytesToString(ba,str,len)); } }@edu.umd.cs.findbugs.annotations.SuppressWarnings("DM_DEFAULT_ENCODING")static private String bytesToString(byte[] ba, int str, int len) { try { return new String(ba,str,len,"UTF-8"); } catch(UnsupportedEncodingException thr) { return new String(ba,str,len); } // all JVMs are required to support UTF-8 }// *************************************************************************************************// STATIC MEMBERS// ************************************************************************************************* static class Appender implements Runnable { private final Jtextarea textarea; private final int maxLines; // maximum lines allowed in text area private final linkedList<Integer> lengths; // length of lines within text area private final List<String> values; // values waiting to be appended private int curLength; // length of current line private boolean clear; private boolean queue; Appender(Jtextarea txtara, int maxlin) { textarea =txtara; maxLines =maxlin; lengths =new linkedList<Integer>(); values =new ArrayList<String>(); curLength=0; clear =false; queue =true; } synchronized void append(String val) { values.add(val); if(queue) { queue=false; EventQueue.invokeLater(this); } } synchronized void clear() { clear=true; curLength=0; lengths.clear(); values.clear(); if(queue) { queue=false; EventQueue.invokeLater(this); } } // MUST BE THE onLY METHOD THAT TOUCHES textarea! public synchronized void run() { if(clear) { textarea.setText(""); } for(String val: values) { curLength+=val.length(); if(val.endsWith(EOL1) || val.endsWith(EOL2)) { if(lengths.size()>=maxLines) { textarea.replaceRange("",0,lengths.removeFirst()); } lengths.addLast(curLength); curLength=0; } textarea.append(val); } values.clear(); clear =false; queue =true; } static private final String EOL1="n"; static private final String EOL2=System.getProperty("line.separator",EOL1); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)