Accueil > > > CONSOLE STANDARD IMPLEMENTABLE AVEC UN INPUTLOGLISTENER
CONSOLE STANDARD IMPLEMENTABLE AVEC UN INPUTLOGLISTENER
Information sur la source
Description
Cette Console Java permet de recuperer le flux System.out afin de l'afficher avec un autre composant de votre choix. Il suffit d'implementer une interface InputLogListener avec une seule methode de transmission des messages et le tour est joué. Cela s'avère très pratique pour garder un log complet des messages print de votre code. (à noter que la sortie d'erreur system.err n'est pas incluse, à vous de l'ajouter dans le code si vous le désirez) b23:production 2007 GNU/GPL
Source
- /*
- * Console.java
- *
- * Created on 14 mai 2007, 00:56
- *
- * To change this template, choose Tools | Template Manager
- * and open the template in the editor.
- */
-
- package sf3.system;
-
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.PipedInputStream;
- import java.io.PipedOutputStream;
- import java.io.PrintStream;
- import java.util.Collections;
- import java.util.Formatter;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
-
- /**
- * This class implements a console to get log data from System.out and can be used in any log component implementing the InputLogListener.
- *@see InputLogListener
- * @author www.b23prodtm.com
- */
- public class Console implements Appendable {
- /** the System.out default PrintStream */
- public final static PrintStream _stdout = System.out;
- /***/
- public final static PrintStream _stderr = System.err;
- /** the System default line separator String (e.g. \n)*/
- public final static String newLine = System.getProperty("line.separator");
- /** print error event */
- protected final static int PRINT_ERROR = 0;
- /** print wait event */
- protected final static int PRINT_WAIT = 1;
- /** print stop event */
- protected final static int PRINT_STOP = 2;
- /** print resume constant */
- protected final static int PRINT_RESUME = 3;
- /***/
- private final static int INIT_BUFFER = 200;
- /***/
- private SpritesCacheManager<Integer, StringBuffer> buffer_spm = new SpritesCacheManager<Integer, StringBuffer>(INIT_BUFFER);
- /** the console buffer (init. to 200 lines)*/
- private Map<Integer,StringBuffer> buffer = Collections.synchronizedMap(buffer_spm);
- /**
- * the switch to _stdout
- *
- * @see #setStdoutEnabled(boolean)
- * @default true
- */
- private boolean toStdout = true;
- /**
- * the switch to _stdout input PrintStream to the console
- *
- * @see #setLogStdoutEnabled(boolean)
- * @default false
- */
- private boolean fromStdout = false;
- /** the console output error listeners */
- private Set<ErrorListener> output = Collections.synchronizedSet(new HashSet<ErrorListener>());
- /** the console input listeners */
- private Set<InputLogListener> input = Collections.synchronizedSet(new HashSet<InputLogListener>());
- /** a standard error listener to setLogStdoutEnabled(true)*/
- private final ErrorListener stdOutCallback = new ErrorListener() {
- public void printStreamError(IOException e) {
- e.printStackTrace();
- try {
- setLogStdoutEnabled(false);
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- };
- /** a standard error listener to setLogStdoutEnabled(true)*/
- private final ErrorListener stdErrCallback = new ErrorListener() {
- public void printStreamError(IOException e) {
- e.printStackTrace();
- try {
- setLogStderrEnabled(false);
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- };
- /** the output if setOutput(PrintStream) is called . default is null.
- * @see #setOutput(PrintStream)*/
- private PrintStream out = null;
- /** the input streams indentified by a "long" ID*/
- private Map<? super Long, ? super PrintStream> in;
- /** the back-cache manager*/
- SpritesCacheManager<Object, Object> spm = new SpritesCacheManager<Object, Object>();
- /***/
- public String _format = "%s";
- /** Creates a new instance of Console. the input is initialized with no printstream */
- public Console() {
- spm.setDebugEnabled(false);
- buffer_spm.setDebugEnabled(false);
- in = Collections.synchronizedMap(spm);
- //spm.setAutoCleanupEnabled(true);
- }
-
- /** returns the buffer
- * @return the current buffer*/
- public StringBuffer getBuffer() {
- StringBuffer strBuf = new StringBuffer();
- for(int i = 0; i < buffer.size(); i++) {
- StringBuffer line = buffer.get(i);
- strBuf.append((line instanceof StringBuffer)?line:null);
- }
- return strBuf;
- }
-
- /** sends a new input to InputLogListeners
- * @see #input
- * @param message the characters data */
- public void notifyInput(String message) {
- synchronized(input) {
- for(Iterator<InputLogListener> i = input.iterator(); i.hasNext(); ) {
- InputLogListener ill = i.next();
- if(ill != null)
- ill.newLogPacket(message);
- }
- }
- }
-
- /** adds a new InputLogListener to input. it will receive data from notifyInput() calls. the methods in the listeners should be synchronized with the listener buffers updates
- * @see #notifyInput(String)
- * @param l the input listener*/
- public void addInputLogListener(InputLogListener l) {
- sendConsoleEvent(PRINT_WAIT);
- synchronized(l) { l.notifyAll(); }
- input.add(l);
- sendConsoleEvent(PRINT_RESUME);
- }
-
- /** removes an input
- * @param l the listening input to remove */
- public void removeInputLogListener(InputLogListener l) {
- sendConsoleEvent(PRINT_WAIT);
- synchronized(l) { l.notifyAll(); }
- input.remove(l);
- sendConsoleEvent(PRINT_RESUME);
- }
-
- /** returns the current line in buffer
- * @return current line characters data */
- public StringBuffer getCurrentLine() {
- StringBuffer line = buffer.get(buffer.size() - 1);
- return (line instanceof StringBuffer)?line:new StringBuffer();
- }
-
- /** dis/enables the System.out trace of the console.
- * @param b true or false */
- public void setStdoutEnabled(boolean b) {
- try {
- setLogStdoutEnabled(false);
- } catch (IOException ex) {
- ex.printStackTrace();
- } finally {
- toStdout = b;
- }
- }
-
- /** checks whether the System.out trace is enabled or not
- * @return true or false
- * @see #setStdoutEnabled(boolean)*/
- public boolean isStdoutEnabled() {
- return toStdout;
- }
-
- /** checks whether the System.out is read to this console or not
- * @return true or false
- * @see #setLogStdoutEnabled(boolean)*/
- public boolean isLogStdoutEnabled() {
- return fromStdout;
- }
-
- /**
- * dis/enables the System.out as an input to this console. an IOException is thrown if the method fails to init.
- * @param b dis/enable
- * @throws java.io.IOException
- */
- public void setLogStdoutEnabled(boolean b) throws IOException {
- int pty = Thread.currentThread().getPriority();
- Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
- fromStdout = b;
- toStdout = false;
- clearContents();
- if(b) {
- Map<String, Object> ps = newPrintStream(stdOutCallback);
- PrintStream printstream = (PrintStream)ps.get("ps");
- if(!printstream.checkError())
- System.setOut(printstream);
- sendConsoleEvent(PRINT_RESUME, (Long)ps.get("id"));
- } else {
- if(!_stdout.checkError())
- System.setOut(_stdout);
- }
- toStdout = !b;
- Thread.currentThread().setPriority(pty);
- }
-
- /**
- * dis/enables the System.out as an input to this console. an IOException is thrown if the method fails to init.
- * @param b dis/enable
- * @throws java.io.IOException
- */
- public void setLogStderrEnabled(boolean b) throws IOException {
- int pty = Thread.currentThread().getPriority();
- Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
- clearContents();
- if(b) {
- Map<String, Object> ps = newPrintStream(stdErrCallback);
- PrintStream printstream = (PrintStream)ps.get("ps");
- if(!printstream.checkError())
- System.setErr(printstream);
- sendConsoleEvent(PRINT_RESUME, (Long)ps.get("id"));
- } else {
- if(!_stderr.checkError())
- System.setErr(_stderr);
- }
- Thread.currentThread().setPriority(pty);
- }
-
- /** sets an different output PrintStream for this console.
- * @param newOut the output
- * @return the previous printstream*/
- public PrintStream setOutput(PrintStream newOut) {
- PrintStream old = out;
- out = newOut;
- return old;
- }
-
- /**
- * @param in an InputStream to add to this console, it should be buffered.*/
- public Thread getNewInput(InputStream in) throws IOException{
- return getNewInput(in, _format);
- }
-
- /**
- * @param in an InputStream to add to this console, it should be buffered.*/
- public Thread getNewInput(InputStream in, String format) throws IOException{
- final String _f = format;
- final BufferedInputStream _in = new BufferedInputStream(in);
- final Map<String, Object> map = newPrintStream(new ErrorListener() {
- public void printStreamError(IOException e) {
- e.printStackTrace();
- }
- });
- Thread t = new Thread(new Runnable() { public void run(){
- try{
- sendConsoleEvent(PRINT_RESUME, (Long)map.get("id"));
- PrintStream ps = (PrintStream)map.get("ps");
- ps.println("New Console input has been created.");
- Formatter f = new Formatter(ps);
- byte[] s = new byte[128];
- while(_in.read(s) != -1)
- f.format(_f, new String(s));
- } catch(IOException e) {
- e.printStackTrace();
- }
- }}, "T-Console inputStream");
- t.setDaemon(true);
- t.setPriority(Thread.MAX_PRIORITY);
- return t;
- }
-
- /** adds an error listener to the console output
- * @param l the error listener*/
- public void addErrorListener(ErrorListener l) {
- output.add(l);
- }
-
- /** removes an error listener from the console output
- * @param l the error listener to remove */
- public void removeErrorListener(ErrorListener l) {
- output.remove(l);
- }
-
- /** sends an error notice to the error listeners
- * @param event the error event
- * @param e the IOException caught
- * @see #PRINT_ERROR */
- private void notifyError(int event, IOException e) {
- synchronized(output) {
- switch(event) {
- case PRINT_ERROR:
- synchronized(output) {
- for(Iterator<ErrorListener> i = output.iterator(); i.hasNext();) {
- ErrorListener l = i.next();
- if(l instanceof ErrorListener)
- l.printStreamError(e);
- }
- }
- break;
- default:
- break;
- }
- }
- }
-
- /***/
- private int tflag = 0;
- /**
- * returns a new PrintStream to print on loggin' stack.
- * it is simply a 32bits packet atream read from the printStream that will be sent to the InputListeners.
- * NOTICE: after this method you MUST send a PRINT_RESUME event to the console otherwise the returned PrintStream will stay inactive.
- *
- * @return new buffered PrintStream instance ready to be used as an input and it associated "long" ID.
- * That is, when you get the map Map<String, Object>:
- * long id = (Long)map.get("id");
- * PrintStream ps = (PrintStream)map.get("ps");
- * // assign the PrintStream ps somewhere
- * // and send a PRINT_RESUME to start writing to the console.
- * console.sendConsoleEvent(Console.PRINT_RESUME);
- * @see #sendConsoleEvent(int)
- * @see #PRINT_RESUME
- * @see ErrorListener
- * @see #input
- * @see InputLogListener#newLogPacket(String)
- * @param l Output listener to the printStream
- * @throws java.io.IOException
- */
- protected synchronized Map<String, Object> newPrintStream(ErrorListener l) throws IOException {
- int pty = Thread.currentThread().getPriority();
- Thread.currentThread().setPriority(Thread.MAX_PRIORITY - 1);
- addErrorListener(l);
- PipedOutputStream pipedOutputStream = new PipedOutputStream();
- final BufferedInputStream pis = new BufferedInputStream(new PipedInputStream(pipedOutputStream));
- final long id = System.currentTimeMillis();
- final PrintStream ps;
- tflag = 0;
- in.put(id, ps = new PrintStream(new BufferedOutputStream(pipedOutputStream)));
- Thread t = new Thread(new Runnable() { public void run() {
- try {
- new Thread(new Runnable() { public void run() {
- synchronized(Console.this) {
- while(tflag == 0) {
- System.out.println("new PrintStream notice");
- Console.this.notify();
- try {
- Console.this.wait(0,50);
- } catch (InterruptedException ex) {
- ex.printStackTrace();
- Thread.currentThread().interrupt();
- }
- }
- }
- }}).start();
- synchronized(ps) {
- ps.wait();
- }
- System.out.println("new PrintStream is startin'...");
- byte[] b = new byte[128];
- while(pis.read(b) != -1) {
- final String s = new String(b);
- switch(readConsoleEvent(id)) {
- case Console.PRINT_WAIT:
- synchronized(ps) {
- ps.wait();
- }
- break;
- default:
- break;
- }
- notifyInput(s);
- try {
- append(s);
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- pis.close();
- } catch (InterruptedException ex) {
- ex.printStackTrace();
- Thread.currentThread().interrupt();
- } catch (IOException ex) {
- notifyError(PRINT_ERROR, ex);
- }
- }}, "T-logPrintStream");
- t.setPriority(Thread.MAX_PRIORITY);
- t.setDaemon(true);
- spm.buffer(t);
- try {
- spm.memorySensitiveCallback("start", t, null, null);
- System.out.println("new PrintStream is waitin'...");
- wait();
- tflag = 1;
- } catch (InterruptedException ex) {
- ex.printStackTrace();
- Thread.currentThread().interrupt();
- } catch(Throwable tr) {
- tr.printStackTrace();
- } finally {
- Map<String, Object> map = Collections.synchronizedMap(new HashMap<String, Object>());
- map.put("id", id);
- map.put("ps", ps);
- Thread.currentThread().setPriority(pty);
- return map;
- }
- }
-
-
- /** removes a identified input
- * @param id the "long" id of the PrintStream input
- * @return the same PrintStream
- * @see #newPrintStream(ErrorListener)*/
- protected PrintStream removePrintStream(long id) {
- return (PrintStream) in.remove(id);
- }
- /** the events map that will be used by sendConsoleEvent and readConsoleEvent
- * @see #sendConsoleEvent(int)
- * @see #readConsoleEvent(long)*/
- Map<Integer, Map<Long, Integer>> events = Collections.synchronizedMap(new HashMap<Integer, Map<Long, Integer>>());
- /** reads for a sent event in the events map for the given "long" id
- * @param id the id to distinguish events
- * @return the event
- * @see #PRINT_WAIT
- *@see #PRINT_RESUME
- *@see #PRINT_STOP
- */
- private int readConsoleEvent(long id) {
- Set<Integer> set = events.keySet();
- synchronized(events) {
- for(Iterator<Integer> i = set.iterator(); i.hasNext();) {
- int key = i.next();
- Map<Long, Integer> event = events.get(key);
- if(event == null)
- return -1;
- else
- return (event.get(id) != null)?events.remove(key).get(id):-1;
- }
- return -1;
- }
- }
-
- /** sends a new event to the console
- * @param event the event
- * @see #PRINT_WAIT
- * @see #PRINT_RESUME
- * @see #PRINT_STOP*/
- public void sendConsoleEvent(int event) {
- Set<? super Long> set = in.keySet();
- synchronized(in){
- for(Iterator<? super Long> i = set.iterator(); i.hasNext();)
- sendConsoleEvent(event, (Long)i.next());
- }
- }
-
- /** sends a new console event to the selected "long" id, usually a PrintStream registered input
- * @see #newPrintStream(ErrorListener)
- * @param event the event
- *@param id the "long" id
- * @return true or false whether the event could be sent to a registered PrintStream
- * @see #sendConsoleEvent(int)*/
- private boolean sendConsoleEvent(int event, long id) {
- if(id != -1) {
- PrintStream ps = (PrintStream)in.get(id);
- if(ps == null)
- return false;
- synchronized(ps) {
- switch(event) {
- case PRINT_RESUME:
- ps.notifyAll();
- return true;
- case PRINT_STOP:
- ps.close();
- return true;
- default:
- ps.notify();
- break;
- }
- }
- }
- Map<Long, Integer> map = new HashMap();
- map.put(id, event);
- events.put(events.size(), map);
- return true;
- }
-
- /**
- * appends new characters data to the console buffer and notifies the output
- *
- * @return the buffer with the new data appended
- * @see #buffer
- * @param charSequence the data to append
- * @throws java.io.IOException
- */
- public Appendable append(CharSequence charSequence) throws IOException {
- charSequence = new Formatter().format(_format, charSequence).toString().subSequence(0, charSequence.length());
- if(toStdout)
- _stdout.append(charSequence);
- if(out instanceof PrintStream)
- out.append(charSequence);
- String[] s = charSequence.toString().split(newLine);
- for(int i = 0; i < s.length; i++){
- String line = s[i];
- if(i < s.length - 1)
- line += newLine;
- if(getCurrentLine().lastIndexOf(newLine) == -1)
- buffer.put(buffer.size() - 1, getCurrentLine().append(line));
- else
- buffer.put(buffer.size(), new StringBuffer(line));
- }
- return this;
- }
-
- /**
- * appends new characters data to the console buffer and notifies the output
- *
- * @return the buffer with the new data appended
- * @see #buffer
- * @param charSequence the data to append
- * @param i the starting index of the characters data to append
- * @param i0 the 1+end index
- * @throws java.io.IOException
- */
- public Appendable append(CharSequence charSequence, int i, int i0) throws IOException {
- charSequence = new Formatter().format(_format, charSequence).toString().subSequence(i, i0);
- if(toStdout)
- _stdout.append(charSequence, i, i0);
- if(out instanceof PrintStream)
- out.append(charSequence, i, i0);
- String[] s = charSequence.toString().split(newLine);
- for(int j = 0; j < s.length; j++){
- String line = s[j];
- if(j < s.length - 1)
- line += newLine;
- if(getCurrentLine().lastIndexOf(newLine) == -1)
- buffer.put(buffer.size() - 1, getCurrentLine().append(line, i, i0));
- else
- buffer.put(buffer.size(), new StringBuffer().append(line, i, i0));
- }
- return this;
- }
-
- /**
- * appends new characters data to the console buffer and notifies the output
- *
- * @return the buffer with the new data appended
- * @see #buffer
- * @param c the data to append
- * @throws java.io.IOException
- */
- public Appendable append(char c) throws IOException {
- c = new Formatter().format(_format, c).toString().charAt(0);
- if(toStdout)
- _stdout.append(c);
- if(out instanceof PrintStream)
- out.append(c);
- if(getCurrentLine().lastIndexOf(newLine) == -1)
- buffer.put(buffer.size() - 1, getCurrentLine().append(c));
- else
- buffer.put(buffer.size(), new StringBuffer(c));
- return this;
- }
-
- /** clears the contents of buffer. all previous from input unread data in the buffer will be lost.
- * @see #buffer*/
- public void clearContents() {
- buffer.clear();
- }
-
- /**
- * overriden to disable the System.out read stream to this console and reset the System.out to _stdout.
- */
- public void finalize() {
- try {
- setLogStdoutEnabled(false);
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
-
- /***/
- public PrintStream getNewPrintStream(ErrorListener l) {
- try {
- Map<String, Object> map = newPrintStream(l);
- PrintStream ps = (PrintStream)map.get("ps");
- sendConsoleEvent(PRINT_RESUME, (Long)map.get("id"));
- return ps;
- } catch (IOException ex) {
- ex.printStackTrace();
- return null;
- }
- }
-
- }
/*
* Console.java
*
* Created on 14 mai 2007, 00:56
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package sf3.system;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.util.Collections;
import java.util.Formatter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* This class implements a console to get log data from System.out and can be used in any log component implementing the InputLogListener.
*@see InputLogListener
* @author www.b23prodtm.com
*/
public class Console implements Appendable {
/** the System.out default PrintStream */
public final static PrintStream _stdout = System.out;
/***/
public final static PrintStream _stderr = System.err;
/** the System default line separator String (e.g. \n)*/
public final static String newLine = System.getProperty("line.separator");
/** print error event */
protected final static int PRINT_ERROR = 0;
/** print wait event */
protected final static int PRINT_WAIT = 1;
/** print stop event */
protected final static int PRINT_STOP = 2;
/** print resume constant */
protected final static int PRINT_RESUME = 3;
/***/
private final static int INIT_BUFFER = 200;
/***/
private SpritesCacheManager<Integer, StringBuffer> buffer_spm = new SpritesCacheManager<Integer, StringBuffer>(INIT_BUFFER);
/** the console buffer (init. to 200 lines)*/
private Map<Integer,StringBuffer> buffer = Collections.synchronizedMap(buffer_spm);
/**
* the switch to _stdout
*
* @see #setStdoutEnabled(boolean)
* @default true
*/
private boolean toStdout = true;
/**
* the switch to _stdout input PrintStream to the console
*
* @see #setLogStdoutEnabled(boolean)
* @default false
*/
private boolean fromStdout = false;
/** the console output error listeners */
private Set<ErrorListener> output = Collections.synchronizedSet(new HashSet<ErrorListener>());
/** the console input listeners */
private Set<InputLogListener> input = Collections.synchronizedSet(new HashSet<InputLogListener>());
/** a standard error listener to setLogStdoutEnabled(true)*/
private final ErrorListener stdOutCallback = new ErrorListener() {
public void printStreamError(IOException e) {
e.printStackTrace();
try {
setLogStdoutEnabled(false);
} catch (IOException ex) {
ex.printStackTrace();
}
}
};
/** a standard error listener to setLogStdoutEnabled(true)*/
private final ErrorListener stdErrCallback = new ErrorListener() {
public void printStreamError(IOException e) {
e.printStackTrace();
try {
setLogStderrEnabled(false);
} catch (IOException ex) {
ex.printStackTrace();
}
}
};
/** the output if setOutput(PrintStream) is called . default is null.
* @see #setOutput(PrintStream)*/
private PrintStream out = null;
/** the input streams indentified by a "long" ID*/
private Map<? super Long, ? super PrintStream> in;
/** the back-cache manager*/
SpritesCacheManager<Object, Object> spm = new SpritesCacheManager<Object, Object>();
/***/
public String _format = "%s";
/** Creates a new instance of Console. the input is initialized with no printstream */
public Console() {
spm.setDebugEnabled(false);
buffer_spm.setDebugEnabled(false);
in = Collections.synchronizedMap(spm);
//spm.setAutoCleanupEnabled(true);
}
/** returns the buffer
* @return the current buffer*/
public StringBuffer getBuffer() {
StringBuffer strBuf = new StringBuffer();
for(int i = 0; i < buffer.size(); i++) {
StringBuffer line = buffer.get(i);
strBuf.append((line instanceof StringBuffer)?line:null);
}
return strBuf;
}
/** sends a new input to InputLogListeners
* @see #input
* @param message the characters data */
public void notifyInput(String message) {
synchronized(input) {
for(Iterator<InputLogListener> i = input.iterator(); i.hasNext(); ) {
InputLogListener ill = i.next();
if(ill != null)
ill.newLogPacket(message);
}
}
}
/** adds a new InputLogListener to input. it will receive data from notifyInput() calls. the methods in the listeners should be synchronized with the listener buffers updates
* @see #notifyInput(String)
* @param l the input listener*/
public void addInputLogListener(InputLogListener l) {
sendConsoleEvent(PRINT_WAIT);
synchronized(l) { l.notifyAll(); }
input.add(l);
sendConsoleEvent(PRINT_RESUME);
}
/** removes an input
* @param l the listening input to remove */
public void removeInputLogListener(InputLogListener l) {
sendConsoleEvent(PRINT_WAIT);
synchronized(l) { l.notifyAll(); }
input.remove(l);
sendConsoleEvent(PRINT_RESUME);
}
/** returns the current line in buffer
* @return current line characters data */
public StringBuffer getCurrentLine() {
StringBuffer line = buffer.get(buffer.size() - 1);
return (line instanceof StringBuffer)?line:new StringBuffer();
}
/** dis/enables the System.out trace of the console.
* @param b true or false */
public void setStdoutEnabled(boolean b) {
try {
setLogStdoutEnabled(false);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
toStdout = b;
}
}
/** checks whether the System.out trace is enabled or not
* @return true or false
* @see #setStdoutEnabled(boolean)*/
public boolean isStdoutEnabled() {
return toStdout;
}
/** checks whether the System.out is read to this console or not
* @return true or false
* @see #setLogStdoutEnabled(boolean)*/
public boolean isLogStdoutEnabled() {
return fromStdout;
}
/**
* dis/enables the System.out as an input to this console. an IOException is thrown if the method fails to init.
* @param b dis/enable
* @throws java.io.IOException
*/
public void setLogStdoutEnabled(boolean b) throws IOException {
int pty = Thread.currentThread().getPriority();
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
fromStdout = b;
toStdout = false;
clearContents();
if(b) {
Map<String, Object> ps = newPrintStream(stdOutCallback);
PrintStream printstream = (PrintStream)ps.get("ps");
if(!printstream.checkError())
System.setOut(printstream);
sendConsoleEvent(PRINT_RESUME, (Long)ps.get("id"));
} else {
if(!_stdout.checkError())
System.setOut(_stdout);
}
toStdout = !b;
Thread.currentThread().setPriority(pty);
}
/**
* dis/enables the System.out as an input to this console. an IOException is thrown if the method fails to init.
* @param b dis/enable
* @throws java.io.IOException
*/
public void setLogStderrEnabled(boolean b) throws IOException {
int pty = Thread.currentThread().getPriority();
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
clearContents();
if(b) {
Map<String, Object> ps = newPrintStream(stdErrCallback);
PrintStream printstream = (PrintStream)ps.get("ps");
if(!printstream.checkError())
System.setErr(printstream);
sendConsoleEvent(PRINT_RESUME, (Long)ps.get("id"));
} else {
if(!_stderr.checkError())
System.setErr(_stderr);
}
Thread.currentThread().setPriority(pty);
}
/** sets an different output PrintStream for this console.
* @param newOut the output
* @return the previous printstream*/
public PrintStream setOutput(PrintStream newOut) {
PrintStream old = out;
out = newOut;
return old;
}
/**
* @param in an InputStream to add to this console, it should be buffered.*/
public Thread getNewInput(InputStream in) throws IOException{
return getNewInput(in, _format);
}
/**
* @param in an InputStream to add to this console, it should be buffered.*/
public Thread getNewInput(InputStream in, String format) throws IOException{
final String _f = format;
final BufferedInputStream _in = new BufferedInputStream(in);
final Map<String, Object> map = newPrintStream(new ErrorListener() {
public void printStreamError(IOException e) {
e.printStackTrace();
}
});
Thread t = new Thread(new Runnable() { public void run(){
try{
sendConsoleEvent(PRINT_RESUME, (Long)map.get("id"));
PrintStream ps = (PrintStream)map.get("ps");
ps.println("New Console input has been created.");
Formatter f = new Formatter(ps);
byte[] s = new byte[128];
while(_in.read(s) != -1)
f.format(_f, new String(s));
} catch(IOException e) {
e.printStackTrace();
}
}}, "T-Console inputStream");
t.setDaemon(true);
t.setPriority(Thread.MAX_PRIORITY);
return t;
}
/** adds an error listener to the console output
* @param l the error listener*/
public void addErrorListener(ErrorListener l) {
output.add(l);
}
/** removes an error listener from the console output
* @param l the error listener to remove */
public void removeErrorListener(ErrorListener l) {
output.remove(l);
}
/** sends an error notice to the error listeners
* @param event the error event
* @param e the IOException caught
* @see #PRINT_ERROR */
private void notifyError(int event, IOException e) {
synchronized(output) {
switch(event) {
case PRINT_ERROR:
synchronized(output) {
for(Iterator<ErrorListener> i = output.iterator(); i.hasNext();) {
ErrorListener l = i.next();
if(l instanceof ErrorListener)
l.printStreamError(e);
}
}
break;
default:
break;
}
}
}
/***/
private int tflag = 0;
/**
* returns a new PrintStream to print on loggin' stack.
* it is simply a 32bits packet atream read from the printStream that will be sent to the InputListeners.
* NOTICE: after this method you MUST send a PRINT_RESUME event to the console otherwise the returned PrintStream will stay inactive.
*
* @return new buffered PrintStream instance ready to be used as an input and it associated "long" ID.
* That is, when you get the map Map<String, Object>:
* long id = (Long)map.get("id");
* PrintStream ps = (PrintStream)map.get("ps");
* // assign the PrintStream ps somewhere
* // and send a PRINT_RESUME to start writing to the console.
* console.sendConsoleEvent(Console.PRINT_RESUME);
* @see #sendConsoleEvent(int)
* @see #PRINT_RESUME
* @see ErrorListener
* @see #input
* @see InputLogListener#newLogPacket(String)
* @param l Output listener to the printStream
* @throws java.io.IOException
*/
protected synchronized Map<String, Object> newPrintStream(ErrorListener l) throws IOException {
int pty = Thread.currentThread().getPriority();
Thread.currentThread().setPriority(Thread.MAX_PRIORITY - 1);
addErrorListener(l);
PipedOutputStream pipedOutputStream = new PipedOutputStream();
final BufferedInputStream pis = new BufferedInputStream(new PipedInputStream(pipedOutputStream));
final long id = System.currentTimeMillis();
final PrintStream ps;
tflag = 0;
in.put(id, ps = new PrintStream(new BufferedOutputStream(pipedOutputStream)));
Thread t = new Thread(new Runnable() { public void run() {
try {
new Thread(new Runnable() { public void run() {
synchronized(Console.this) {
while(tflag == 0) {
System.out.println("new PrintStream notice");
Console.this.notify();
try {
Console.this.wait(0,50);
} catch (InterruptedException ex) {
ex.printStackTrace();
Thread.currentThread().interrupt();
}
}
}
}}).start();
synchronized(ps) {
ps.wait();
}
System.out.println("new PrintStream is startin'...");
byte[] b = new byte[128];
while(pis.read(b) != -1) {
final String s = new String(b);
switch(readConsoleEvent(id)) {
case Console.PRINT_WAIT:
synchronized(ps) {
ps.wait();
}
break;
default:
break;
}
notifyInput(s);
try {
append(s);
} catch (IOException ex) {
ex.printStackTrace();
}
}
pis.close();
} catch (InterruptedException ex) {
ex.printStackTrace();
Thread.currentThread().interrupt();
} catch (IOException ex) {
notifyError(PRINT_ERROR, ex);
}
}}, "T-logPrintStream");
t.setPriority(Thread.MAX_PRIORITY);
t.setDaemon(true);
spm.buffer(t);
try {
spm.memorySensitiveCallback("start", t, null, null);
System.out.println("new PrintStream is waitin'...");
wait();
tflag = 1;
} catch (InterruptedException ex) {
ex.printStackTrace();
Thread.currentThread().interrupt();
} catch(Throwable tr) {
tr.printStackTrace();
} finally {
Map<String, Object> map = Collections.synchronizedMap(new HashMap<String, Object>());
map.put("id", id);
map.put("ps", ps);
Thread.currentThread().setPriority(pty);
return map;
}
}
/** removes a identified input
* @param id the "long" id of the PrintStream input
* @return the same PrintStream
* @see #newPrintStream(ErrorListener)*/
protected PrintStream removePrintStream(long id) {
return (PrintStream) in.remove(id);
}
/** the events map that will be used by sendConsoleEvent and readConsoleEvent
* @see #sendConsoleEvent(int)
* @see #readConsoleEvent(long)*/
Map<Integer, Map<Long, Integer>> events = Collections.synchronizedMap(new HashMap<Integer, Map<Long, Integer>>());
/** reads for a sent event in the events map for the given "long" id
* @param id the id to distinguish events
* @return the event
* @see #PRINT_WAIT
*@see #PRINT_RESUME
*@see #PRINT_STOP
*/
private int readConsoleEvent(long id) {
Set<Integer> set = events.keySet();
synchronized(events) {
for(Iterator<Integer> i = set.iterator(); i.hasNext();) {
int key = i.next();
Map<Long, Integer> event = events.get(key);
if(event == null)
return -1;
else
return (event.get(id) != null)?events.remove(key).get(id):-1;
}
return -1;
}
}
/** sends a new event to the console
* @param event the event
* @see #PRINT_WAIT
* @see #PRINT_RESUME
* @see #PRINT_STOP*/
public void sendConsoleEvent(int event) {
Set<? super Long> set = in.keySet();
synchronized(in){
for(Iterator<? super Long> i = set.iterator(); i.hasNext();)
sendConsoleEvent(event, (Long)i.next());
}
}
/** sends a new console event to the selected "long" id, usually a PrintStream registered input
* @see #newPrintStream(ErrorListener)
* @param event the event
*@param id the "long" id
* @return true or false whether the event could be sent to a registered PrintStream
* @see #sendConsoleEvent(int)*/
private boolean sendConsoleEvent(int event, long id) {
if(id != -1) {
PrintStream ps = (PrintStream)in.get(id);
if(ps == null)
return false;
synchronized(ps) {
switch(event) {
case PRINT_RESUME:
ps.notifyAll();
return true;
case PRINT_STOP:
ps.close();
return true;
default:
ps.notify();
break;
}
}
}
Map<Long, Integer> map = new HashMap();
map.put(id, event);
events.put(events.size(), map);
return true;
}
/**
* appends new characters data to the console buffer and notifies the output
*
* @return the buffer with the new data appended
* @see #buffer
* @param charSequence the data to append
* @throws java.io.IOException
*/
public Appendable append(CharSequence charSequence) throws IOException {
charSequence = new Formatter().format(_format, charSequence).toString().subSequence(0, charSequence.length());
if(toStdout)
_stdout.append(charSequence);
if(out instanceof PrintStream)
out.append(charSequence);
String[] s = charSequence.toString().split(newLine);
for(int i = 0; i < s.length; i++){
String line = s[i];
if(i < s.length - 1)
line += newLine;
if(getCurrentLine().lastIndexOf(newLine) == -1)
buffer.put(buffer.size() - 1, getCurrentLine().append(line));
else
buffer.put(buffer.size(), new StringBuffer(line));
}
return this;
}
/**
* appends new characters data to the console buffer and notifies the output
*
* @return the buffer with the new data appended
* @see #buffer
* @param charSequence the data to append
* @param i the starting index of the characters data to append
* @param i0 the 1+end index
* @throws java.io.IOException
*/
public Appendable append(CharSequence charSequence, int i, int i0) throws IOException {
charSequence = new Formatter().format(_format, charSequence).toString().subSequence(i, i0);
if(toStdout)
_stdout.append(charSequence, i, i0);
if(out instanceof PrintStream)
out.append(charSequence, i, i0);
String[] s = charSequence.toString().split(newLine);
for(int j = 0; j < s.length; j++){
String line = s[j];
if(j < s.length - 1)
line += newLine;
if(getCurrentLine().lastIndexOf(newLine) == -1)
buffer.put(buffer.size() - 1, getCurrentLine().append(line, i, i0));
else
buffer.put(buffer.size(), new StringBuffer().append(line, i, i0));
}
return this;
}
/**
* appends new characters data to the console buffer and notifies the output
*
* @return the buffer with the new data appended
* @see #buffer
* @param c the data to append
* @throws java.io.IOException
*/
public Appendable append(char c) throws IOException {
c = new Formatter().format(_format, c).toString().charAt(0);
if(toStdout)
_stdout.append(c);
if(out instanceof PrintStream)
out.append(c);
if(getCurrentLine().lastIndexOf(newLine) == -1)
buffer.put(buffer.size() - 1, getCurrentLine().append(c));
else
buffer.put(buffer.size(), new StringBuffer(c));
return this;
}
/** clears the contents of buffer. all previous from input unread data in the buffer will be lost.
* @see #buffer*/
public void clearContents() {
buffer.clear();
}
/**
* overriden to disable the System.out read stream to this console and reset the System.out to _stdout.
*/
public void finalize() {
try {
setLogStdoutEnabled(false);
} catch (IOException ex) {
ex.printStackTrace();
}
}
/***/
public PrintStream getNewPrintStream(ErrorListener l) {
try {
Map<String, Object> map = newPrintStream(l);
PrintStream ps = (PrintStream)map.get("ps");
sendConsoleEvent(PRINT_RESUME, (Long)map.get("id"));
return ps;
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
}
}
Conclusion
src du package installer complet sur sf.net/projects/sf3jswing rubrique CVS en libre consultation
Historique
- 01 juin 2007 23:35:23 :
- PrintStream ErrorListener added
- 11 juillet 2007 21:38:50 :
- bufferedStreams
- 15 juillet 2007 02:12:41 :
- dernier fix pour les tampons mémoire
- 28 septembre 2007 03:35:11 :
- slightly modified due to inherent updates of the other classes in the same packages (SpritesCacheManager)
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Changer la couleur du texte dans une console [ par Mithrandir62 ]
hello,Je sais pas si cela interesse quelqu'un mais j'ai la solution pour ecrire dans une console de de differentes couleurs, cela ne fonctionne que so
Lire un caractère. [ par LordLife ]
Bonjour à tous, Je débute vraiment en Java et je suis sur un programme qui est sensé proposer des multiplications simples à résoudre (1*1 --> 10*
Listener System [ par aurelbobol6 ]
Bonjour Voila mon petit problème: Je sais écouter les événements du clavier, mais uniquement lorsque je suis sur la fenêtre du programme. Mais mon pr
Redirect Plugin System.out to console view [ par devwithjava ]
Bonjour tout le monde,j'ai développez un plugin et dans le code il y a des System.out.println(), le probleme est :lors de l'execution de ce plugin l'a
Impossible de saisir un entier en java , en utilisant la class console [ par lagdu ]
Bonjour à tous.Je me permet de poster sur le forum affin de vous exposer mon problème.Je vais essayer d'etre le plus precis possible sur celui ci mais
Eclipse et Console.ReadLine() [ par Kristof_Koder ]
Bonjour à tous ! Me voilà à faire mes premiers pas en Java et je trébuche au bout de 10 lignes de code !! Voici le code de ma classe principale : //-
JTextArea + flux de telnet [ par ghofrane ]
Bonjour,j'ai le programme suivant : TelnetWrapper telnet = new TelnetWrapper();
Lecture sur Port serie [ par gethy ]
Salut a tous.J'ai fait des recherche sur ce theme et je n'ai pas trouvé mon probleme evoqué quelque part.Alors voila.J'ai un flasheur de code a barre
PB RMI Naming HELP [ par co2_gaz ]
Salutj'ai passé des heures pas de solution.* dans la fonction si dessous de mon client si je supprime soit le Naming.lookup ou le Naming.rebind tout m
E/S console en java.. comliqués ! Pourquoi ? [ par Chatbour ]
Salut à tous et à toutes à ce que je connais, en java il n'existe aucune méthode équivalente à scanf() de C, à cin de C++, read() de Pascal...il faut
|
Derniers Blogs
UNE JOLIE-HORLOGE ET PAS QU'UN PEU !UNE JOLIE-HORLOGE ET PAS QU'UN PEU ! par neodante
Pour les possesseurs d'iPhone, ça y est Bijin Tokei - qui se traduit littéralement en Français par " Jolie Horloge " - est arrivé et GRATUITEMENT s'il vous plaît ! Après la version Tokyo, Hokkaido, night club, racing, Gal, "pour les mademoiselles'", . voi...
Cliquez pour lire la suite de l'article par neodante TECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICESTECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICES par ROMELARD Fabrice
Animé par: Gaetan Bouveret et Julien Chomarat Business Connectivity Services (BCS) est dans SharePoint 2010 la version 2 de Business Data Catalog (BDC dans SharePoint 2007). Il s'agit de la solution permettant de visualiser des données provenan...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE[DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE par orion
Comme de nombreux geek, je suis un grand amateur de série TV et je rate régulièrement des épisodes de mes séries préférés. Une solution s'offre à vous avec ce merveilleux site : Tv Gorge - www.tvgorge.com Moteur de recherche à l'appui, vous pouvez ...
Cliquez pour lire la suite de l'article par orion TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Vincent Bellet et Baptiste Giraudier La BI dans SharePoint 2010, Les nouveaux services d'application dans SP2010 et SQL Server Reporting services 2008 R2. La BI dans SharePoint est généralisée pour tous afin de permettre à tous les coll...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
|