View Javadoc

1   /**
2    *  MicroEmulator
3    *  Copyright (C) 2006-2007 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   *  @version $Id: MIDletOutputStreamRedirector.java 940 2007-02-16 20:59:37Z vlads $
21   */
22  package org.microemu.app.util;
23  
24  import java.io.IOException;
25  import java.io.OutputStream;
26  import java.io.PrintStream;
27  
28  import org.microemu.log.Logger;
29  
30  
31  /**
32   * @author vlads
33   * 
34   * This class allow redirection of stdout and stderr from MIDlet to MicroEmulator logger console
35   * 
36   */
37  public class MIDletOutputStreamRedirector extends PrintStream {
38  
39  	public final static PrintStream out = outPrintStream();
40  
41  	public final static PrintStream err = errPrintStream();
42  
43  	static {
44  		Logger.addLogOrigin(MIDletOutputStreamRedirector.class);
45  		Logger.addLogOrigin(OutputStream2Log.class);
46  	}
47  	
48  	private static class OutputStream2Log extends OutputStream {
49  
50  		boolean isErrorStream;
51  		
52  		StringBuffer buffer = new StringBuffer();
53  
54  		OutputStream2Log(boolean error) {
55  			this.isErrorStream = error;
56  		}
57  
58  		public void write(int b) throws IOException {
59  			if ((b == '\n') || (b == '\r')) {
60  				if (buffer.length() > 0) {
61  					if (isErrorStream) {
62  						Logger.error(buffer.toString());
63  					} else {
64  						Logger.info(buffer.toString());
65  					}
66  					buffer = new StringBuffer();
67  				}
68  			} else {
69  				buffer.append((char) b);
70  			}
71  		}
72  
73  	}
74  
75  	private MIDletOutputStreamRedirector(boolean error) {
76  		super(new OutputStream2Log(error));
77  	}
78  
79  	private static PrintStream outPrintStream() {
80  		return new MIDletOutputStreamRedirector(false);
81  	}
82  
83  	private static PrintStream errPrintStream() {
84  		return new MIDletOutputStreamRedirector(true);
85  	}
86  
87  	//Override methods to be able to get proper stack trace
88  
89  	public void print(boolean b) {
90  		super.print(b);
91  	}
92  
93  	public void print(char c) {
94  		super.print(c);
95  	}
96  
97  	public void print(char[] s) {
98  		super.print(s);
99  	}
100 
101 	public void print(double d) {
102 		super.print(d);
103 	}
104 
105 	public void print(float f) {
106 		super.print(f);
107 	}
108 
109 	public void print(int i) {
110 		super.print(i);
111 	}
112 
113 	public void print(long l) {
114 		super.print(l);
115 	}
116 
117 	public void print(Object obj) {
118 		super.print(obj);
119 	}
120 
121 	public void print(String s) {
122 		super.print(s);
123 	}
124 
125 	public void println() {
126 		super.println();
127 	}
128 
129 	public void println(boolean x) {
130 		super.println(x);
131 	}
132 
133 	public void println(char x) {
134 		super.println(x);
135 	}
136 
137 	public void println(char[] x) {
138 		super.println(x);
139 	}
140 
141 	public void println(double x) {
142 		super.println(x);
143 	}
144 
145 	public void println(float x) {
146 		super.println(x);
147 	}
148 
149 	public void println(int x) {
150 		super.println(x);
151 	}
152 
153 	public void println(long x) {
154 		super.println(x);
155 	}
156 
157 	public void println(Object x) {
158 		super.println(x);
159 	}
160 
161 	public void println(String x) {
162 		super.println(x);
163 	}
164 
165 	public void write(byte[] buf, int off, int len) {
166 		super.write(buf, off, len);
167 	}
168 
169 	public void write(int b) {
170 		super.write(b);
171 	}
172 	
173 }