1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
33
34
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
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 }