KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > multiServer > launch > capture > Printer


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  */

22 package org.enhydra.multiServer.launch.capture;
23
24 // Standard imports
25
import java.io.PrintStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 //
30
public class Printer extends PrintStream JavaDoc {
31     private static Vector JavaDoc listeners = new Vector JavaDoc();
32
33     public Printer(int type) {
34         super(new CaptureOutputStream(type));
35     }
36
37     // event methods
38
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 JavaDoc source,
49             String JavaDoc 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 JavaDoc {
63     private StringBuffer JavaDoc lineBuffer = new StringBuffer JavaDoc();
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     /**
79      * Method declaration
80      *
81      *
82      * @param str
83      *
84      * @see
85      */

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 JavaDoc str = new String JavaDoc(b);
92         String JavaDoc 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