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 package org.metricshub.wbem.sblim.cimclient.internal.http.io;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 import java.io.BufferedWriter;
48 import java.io.FilterOutputStream;
49 import java.io.IOException;
50 import java.io.InterruptedIOException;
51 import java.io.OutputStream;
52 import java.io.OutputStreamWriter;
53
54
55
56
57
58 public class ASCIIPrintStream extends FilterOutputStream {
59 private boolean iAutoFlush = false;
60
61 private Exception iTrouble = null;
62
63 private BufferedWriter iTextOut;
64
65 private ASCIIPrintStream(boolean autoFlush, OutputStream pOut) {
66 super(pOut);
67 if (pOut == null) throw new NullPointerException("Null output stream");
68 this.iAutoFlush = autoFlush;
69 }
70
71
72
73
74
75
76
77 public ASCIIPrintStream(OutputStream pStream) {
78 this(pStream, false);
79 init(new OutputStreamWriter(this));
80 }
81
82
83
84
85
86
87
88
89
90
91 public ASCIIPrintStream(OutputStream pStream, boolean pAutoFlush) {
92 this(pAutoFlush, pStream);
93 init(new OutputStreamWriter(this.out));
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107 public ASCIIPrintStream(OutputStream pStream, boolean pAutoFlush, String pEncoding) {
108 this(pStream, pAutoFlush);
109 }
110
111 private void init(OutputStreamWriter osw) {
112 this.iTextOut = new BufferedWriter(osw);
113 }
114
115 private void write(String str) {
116 int stringLength = str.length();
117 char charArray[] = new char[stringLength];
118 byte asciiArray[] = new byte[stringLength];
119 str.getChars(0, stringLength, charArray, 0);
120 for (int i = 0; i < stringLength; i++) asciiArray[i] = charArray[i] >= '\u0100' ? 63 : (byte) charArray[i];
121
122 write(asciiArray, 0, stringLength);
123 }
124
125
126
127
128
129
130
131 public void print(boolean pValue) {
132 write(pValue ? "true" : "false");
133 }
134
135
136
137
138
139
140
141 public void print(char c) {
142 write(String.valueOf(c));
143 }
144
145
146
147
148
149
150
151 public void print(int i) {
152 write(String.valueOf(i));
153 }
154
155
156
157
158
159
160
161 public void print(long l) {
162 write(String.valueOf(l));
163 }
164
165
166
167
168
169
170
171 public void print(float f) {
172 write(String.valueOf(f));
173 }
174
175
176
177
178
179
180
181 public void print(double d) {
182 write(String.valueOf(d));
183 }
184
185
186
187
188
189
190
191 public void print(char pArray[]) {
192 write(String.valueOf(pArray));
193 }
194
195
196
197
198
199
200
201 public void print(String s) {
202 write(s == null ? "null" : s);
203 }
204
205
206
207
208
209
210
211 public void print(Object pObj) {
212 write(String.valueOf(pObj));
213 }
214
215
216
217
218 public void println() {
219 newLine();
220 }
221
222
223
224
225
226
227 public void println(boolean flag) {
228 synchronized (this) {
229 print(flag);
230 newLine();
231 }
232 }
233
234
235
236
237
238
239 public void println(char c) {
240 synchronized (this) {
241 print(c);
242 newLine();
243 }
244 }
245
246
247
248
249
250
251 public void println(int i) {
252 synchronized (this) {
253 print(i);
254 newLine();
255 }
256 }
257
258
259
260
261
262
263 public void println(long l) {
264 synchronized (this) {
265 print(l);
266 newLine();
267 }
268 }
269
270
271
272
273
274
275 public void println(float f) {
276 synchronized (this) {
277 print(f);
278 newLine();
279 }
280 }
281
282
283
284
285
286
287 public void println(double d) {
288 synchronized (this) {
289 print(d);
290 newLine();
291 }
292 }
293
294
295
296
297
298
299 public void println(char ac[]) {
300 synchronized (this) {
301 print(ac);
302 newLine();
303 }
304 }
305
306
307
308
309
310
311 public void println(String s) {
312 synchronized (this) {
313 print(s);
314 newLine();
315 }
316 }
317
318
319
320
321
322
323 public void println(Object obj) {
324 synchronized (this) {
325 print(obj);
326 newLine();
327 }
328 }
329
330 private void newLine() {
331 try {
332 synchronized (this) {
333 ensureOpen();
334 this.iTextOut.newLine();
335 if (this.iAutoFlush) this.out.flush();
336 }
337 } catch (InterruptedIOException x) {
338 Thread.currentThread().interrupt();
339 } catch (IOException x) {
340 this.iTrouble = x;
341 }
342 }
343
344 @Override
345 public void write(byte buf[], int off, int len) {
346 try {
347 synchronized (this) {
348 ensureOpen();
349 this.out.write(buf, off, len);
350
351 if (this.iAutoFlush) this.out.flush();
352 }
353
354 } catch (InterruptedIOException x) {
355 Thread.currentThread().interrupt();
356 } catch (IOException x) {
357 this.iTrouble = x;
358 }
359 }
360
361 @Override
362 public void write(int b) {
363 try {
364 synchronized (this) {
365 ensureOpen();
366 this.out.write(b);
367 if ((b == '\n') && this.iAutoFlush) this.out.flush();
368 }
369 } catch (InterruptedIOException x) {
370 Thread.currentThread().interrupt();
371 } catch (IOException x) {
372 this.iTrouble = x;
373 }
374 }
375
376 protected void setError() {
377
378 }
379
380
381
382
383
384
385 public Exception checkError() {
386 if (this.out != null) flush();
387 return this.iTrouble;
388 }
389
390 @Override
391 public void close() {
392 synchronized (this) {
393 if (!this.closing) {
394 this.closing = true;
395 try {
396 this.iTextOut.close();
397 this.out.close();
398 } catch (IOException x) {
399 this.iTrouble = x;
400 }
401 this.iTextOut = null;
402
403 this.out = null;
404 }
405 }
406 }
407
408 @Override
409 public void flush() {
410 synchronized (this) {
411 try {
412 ensureOpen();
413 this.out.flush();
414 } catch (IOException x) {
415 this.iTrouble = x;
416 }
417 }
418 }
419
420 private boolean closing = false;
421
422 private void ensureOpen() throws IOException {
423 if (this.out == null) throw new IOException("Stream closed");
424 }
425 }