1 22 package org.enhydra.multiServer.launch.capture; 23 24 import java.io.PrintStream ; 26 import java.io.OutputStream ; 27 import java.util.Vector ; 28 29 public class Printer extends PrintStream { 31 private static Vector listeners = new Vector (); 32 33 public Printer(int type) { 34 super(new CaptureOutputStream(type)); 35 } 36 37 public static synchronized void addPrintListener(PrintListener l) { 39 listeners.addElement(l); 40 } 41 42 public static synchronized void removePrintListener(PrintListener l) { 43 if (listeners.contains(l)) { 44 listeners.removeElement(l); 45 } 46 } 47 48 protected static synchronized void notifyPrintListeners(Object source, 49 String s, int type) { 50 int count = 0; 51 PrintListener listener = null; 52 PrintEvent event = new PrintEvent(source, type, s); 53 54 count = listeners.size(); 55 for (int i = 0; i < count; i++) { 56 listener = (PrintListener) listeners.elementAt(i); 57 listener.onPrint(event); 58 } 59 } 60 61 } 62 class CaptureOutputStream extends OutputStream { 63 private StringBuffer lineBuffer = new StringBuffer (); 64 private int type = PrintEvent.OUTPUT; 65 66 public CaptureOutputStream(int t) { 67 super(); 68 type = t; 69 } 70 71 public synchronized void write(int i) { 72 byte[] b = new byte[1]; 73 74 b[0] = (byte) i; 75 write(b); 76 } 77 78 86 public synchronized void write(byte[] b) { 87 write(b, 0, b.length); 88 } 89 90 public synchronized void write(byte[] b, int off, int len) { 91 String str = new String (b); 92 String out = str.substring(off, (off + len)); 93 boolean writeLine = false; 94 95 if (true) { 96 if (out.length() > 1) { 97 char end1 = out.charAt(out.length() - 2); 98 char end2 = out.charAt(out.length() - 1); 99 100 if ((int) end1 == 13 && (int) end2 == 10) { 101 if (out.length() < 3) { 102 out = ""; 103 } else { 104 out = out.substring(0, out.length() - 2); 105 } 106 writeLine = true; 107 } 108 if (out.endsWith("\n")) { 109 writeLine = true; 110 } 111 } 112 if (writeLine) { 113 Printer.notifyPrintListeners(this, 114 lineBuffer.toString() + out, 115 type); 116 lineBuffer.setLength(0); 117 } else { 118 lineBuffer.append(out); 119 } 120 } else { 121 Printer.notifyPrintListeners(this, out, type); 122 } 123 } 124 125 } 126 | Popular Tags |