View Javadoc

1   /**
2    *  MicroEmulator
3    *  Copyright (C) 2006-2007 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
5    *
6    *  It is licensed under the following two licenses as alternatives:
7    *    1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
8    *    2. Apache License (the "AL") Version 2.0
9    *
10   *  You may not use this file except in compliance with at least one of
11   *  the above two licenses.
12   *
13   *  You may obtain a copy of the LGPL at
14   *      http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
15   *
16   *  You may obtain a copy of the AL at
17   *      http://www.apache.org/licenses/LICENSE-2.0
18   *
19   *  Unless required by applicable law or agreed to in writing, software
20   *  distributed under the License is distributed on an "AS IS" BASIS,
21   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22   *  See the LGPL or the AL for the specific language governing permissions and
23   *  limitations.
24   *
25   *  @version $Id: MIDletOutputStreamRedirector.java 1760 2008-06-24 08:06:35Z barteo $
26   */
27  package org.microemu.app.util;
28  
29  import java.io.OutputStream;
30  import java.io.PrintStream;
31  
32  import org.microemu.log.Logger;
33  
34  /**
35   * @author vlads
36   * 
37   * This class allow redirection of stdout and stderr from MIDlet to
38   * MicroEmulator logger console
39   * 
40   */
41  public class MIDletOutputStreamRedirector extends PrintStream {
42  
43  	private final static boolean keepMultiLinePrint = true;
44  
45  	public final static PrintStream out = outPrintStream();
46  
47  	public final static PrintStream err = errPrintStream();
48  
49  	private boolean isErrorStream;
50  
51  	static {
52  		Logger.addLogOrigin(MIDletOutputStreamRedirector.class);
53  		Logger.addLogOrigin(OutputStream2Log.class);
54  	}
55  
56  	private static class OutputStream2Log extends OutputStream {
57  
58  		boolean isErrorStream;
59  
60  		StringBuffer buffer = new StringBuffer();
61  
62  		OutputStream2Log(boolean error) {
63  			this.isErrorStream = error;
64  		}
65  
66  		public void flush() {
67  			if (buffer.length() > 0) {
68  				write('\n');
69  			}
70  		}
71  
72  		public void write(int b) {
73  			if ((b == '\n') || (b == '\r')) {
74  				if (buffer.length() > 0) {
75  					if (isErrorStream) {
76  						Logger.error(buffer.toString());
77  					} else {
78  						Logger.info(buffer.toString());
79  					}
80  					buffer = new StringBuffer();
81  				}
82  			} else {
83  				buffer.append((char) b);
84  			}
85  		}
86  
87  	}
88  
89  	private MIDletOutputStreamRedirector(boolean error) {
90  		super(new OutputStream2Log(error));
91  
92  		this.isErrorStream = error;
93  	}
94  
95  	private static PrintStream outPrintStream() {
96  		return new MIDletOutputStreamRedirector(false);
97  	}
98  
99  	private static PrintStream errPrintStream() {
100 		return new MIDletOutputStreamRedirector(true);
101 	}
102 
103 	// Override methods to be able to get proper stack trace
104 
105 	public void print(boolean b) {
106 		super.print(b);
107 	}
108 
109 	public void print(char c) {
110 		super.print(c);
111 	}
112 
113 	public void print(char[] s) {
114 		super.print(s);
115 	}
116 
117 	public void print(double d) {
118 		super.print(d);
119 	}
120 
121 	public void print(float f) {
122 		super.print(f);
123 	}
124 
125 	public void print(int i) {
126 		super.print(i);
127 	}
128 
129 	public void print(long l) {
130 		super.print(l);
131 	}
132 
133 	public void print(Object obj) {
134 		super.print(obj);
135 	}
136 
137 	public void print(String s) {
138 		super.print(s);
139 	}
140 
141 	public void println() {
142 		super.println();
143 	}
144 
145 	public void println(boolean x) {
146 		super.println(x);
147 	}
148 
149 	public void println(char x) {
150 		super.println(x);
151 	}
152 
153 	public void println(char[] x) {
154 		super.println(x);
155 	}
156 
157 	public void println(double x) {
158 		super.println(x);
159 	}
160 
161 	public void println(float x) {
162 		super.println(x);
163 	}
164 
165 	public void println(int x) {
166 		super.println(x);
167 	}
168 
169 	public void println(long x) {
170 		super.println(x);
171 	}
172 
173 	public void println(Object x) {
174 		if (keepMultiLinePrint) {
175 			super.flush();
176 			if (isErrorStream) {
177 				Logger.error(x);
178 			} else {
179 				Logger.info(x);
180 			}
181 		} else {
182 			super.println(x);
183 		}
184 	}
185 
186 	public void println(String x) {
187 		if (keepMultiLinePrint) {
188 			super.flush();
189 			if (isErrorStream) {
190 				Logger.error(x);
191 			} else {
192 				Logger.info(x);
193 			}
194 		} else {
195 			super.println(x);
196 		}
197 	}
198 
199 	public void write(byte[] buf, int off, int len) {
200 		super.write(buf, off, len);
201 	}
202 
203 	public void write(int b) {
204 		super.write(b);
205 	}
206 
207 }