begin process at 2010 02 10 10:30:39
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

J++

 > CONSOLE STANDARD IMPLEMENTABLE AVEC UN INPUTLOGLISTENER

CONSOLE STANDARD IMPLEMENTABLE AVEC UN INPUTLOGLISTENER


 Information sur la source

Note :
Aucune note
Catégorie :J++ Classé sous :console, system, listener, synchronized, printstream Niveau :Initié Date de création :01/06/2007 Date de mise à jour :28/09/2007 03:35:11 Vu :4 333

Auteur : broumbroum

Ecrire un message privé
Site perso
Commentaire sur cette source (1)
Ajouter un commentaire et/ou une note

 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

Source avec Zip Source avec une capture RENDERING SCENE POUR JEU 2D TOUTE PLATEFORME (JAI/JIIO)
Source avec Zip Source avec une capture AFFICHEUR D'IMAGE ACCÉLÉRÉ JAVA ADVANCED IMAGING 1.1
Source avec Zip Source avec une capture IMAGE MULTI CONVERTER
Source avec Zip Source avec une capture SPRITES CACHE MANAGER POUR GESTION MÉMOIRE DE GRAND NOMBRE D...
Source avec Zip Source avec une capture ANIMATIONS JAVA ADVANCED IMAGING (AVEC TAMPON MÉMOIRE)

 Sources de la même categorie

Source avec Zip Source avec une capture GESTION DES SYSTÈMES DISTRIBUÉS par 2mohamed2
Source avec Zip Source avec une capture EXEMPLE DE GESTION DE FICHE DOCUMENTAIRE SOUS WINDOWS par chtitfred
Source avec Zip Source avec une capture MANIPULATION DE FICHIER XML par hedi_tounsi
Source avec Zip CHAT TCP TRES SIMPLE À COMPRENDRE par ouassit
Source avec Zip GESTION D'UN GARAGE par krokro1

 Sources en rapport avec celle ci

Source avec Zip CRYPTAGE" CHÉ" par hardr12
Source avec Zip Source avec une capture CONVERTIR ENTRE LES BASES 10,2,8 ET 16 par 2mohamed2
REDIRECTION DES FLUX SYSTEM.OUT ET SYSTEM.ERR DANS UNE JTEXT... par twinser
Source avec Zip Source avec une capture JEU DEATH NOTE - UNE ADAPTATION EN JAVA DU JEU CARNAGE PRESE... par mad_charif
Source avec Zip Source avec une capture ECRIRE EN COULEURS SUR LA CONSOLE(JNI) par Twinuts

Commentaires et avis

Commentaire de broumbroum le 15/07/2007 02:11:01

cette classe est finalement très pratique meme s'il est vrai qu'une console Java est deja disponible avec JConsole du JRE 5.0.
Il est interessant de constater que sans inclure de tampon memoire la sortie standard System.out est rapidement saturée et bloque le programme. A présent, les BufferedInput/OutputStreams permettent l'exécution correcte de la console et de la récupérer facilement sur un JTextArea comme je le fais.
:)

 Ajouter un commentaire


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 --&gt; 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


Nos sponsors


Sondage...

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 1,404 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales