1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
36
37
38
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
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 }