View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2005, 2009
3   
4     THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5     ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6     CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7   
8     You can obtain a current copy of the Eclipse Public License from
9     http://www.opensource.org/licenses/eclipse-1.0.php
10  
11    @author : Roberto Pineiro, IBM, roberto.pineiro@us.ibm.com
12   * @author : Chung-hao Tan, IBM, chungtan@us.ibm.com
13   * 
14   * 
15   * Change History
16   * Flag       Date        Prog         Description
17   *------------------------------------------------------------------------------- 
18   * 18045      2005-08-10  pineiro5     Some code clean up in multiple points
19   * 1535756    2006-08-07  lupusalex    Make code warning free
20   * 1565892    2006-11-28  lupusalex    Make SBLIM client JSR48 compliant
21   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
22   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
23   * 2714989    2009-03-26  blaschke-oss Code cleanup from redundant null check et al
24   */
25  package org.metricshub.wbem.sblim.cimclient.internal.http.io;
26  
27  /*-
28   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
29   * WBEM Java Client
30   * ჻჻჻჻჻჻
31   * Copyright 2023 - 2025 MetricsHub
32   * ჻჻჻჻჻჻
33   * Licensed under the Apache License, Version 2.0 (the "License");
34   * you may not use this file except in compliance with the License.
35   * You may obtain a copy of the License at
36   *
37   *      http://www.apache.org/licenses/LICENSE-2.0
38   *
39   * Unless required by applicable law or agreed to in writing, software
40   * distributed under the License is distributed on an "AS IS" BASIS,
41   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42   * See the License for the specific language governing permissions and
43   * limitations under the License.
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   * Class ASCIIPrintStream implement a stream with ASCII charset
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  	 * Ctor.
73  	 *
74  	 * @param pStream
75  	 *            The underlying stream
76  	 */
77  	public ASCIIPrintStream(OutputStream pStream) {
78  		this(pStream, false);
79  		init(new OutputStreamWriter(this));
80  	}
81  
82  	/**
83  	 * Ctor.
84  	 *
85  	 * @param pStream
86  	 *            The underlying stream
87  	 * @param pAutoFlush
88  	 *            If <code>true</code> the stream is automatically flushed after
89  	 *            write.
90  	 */
91  	public ASCIIPrintStream(OutputStream pStream, boolean pAutoFlush) {
92  		this(pAutoFlush, pStream);
93  		init(new OutputStreamWriter(this.out));
94  	}
95  
96  	/**
97  	 * Ctor.
98  	 *
99  	 * @param pStream
100 	 *            The underlying stream
101 	 * @param pAutoFlush
102 	 *            If <code>true</code> the stream is automatically flushed after
103 	 *            write.
104 	 * @param pEncoding
105 	 *            Ignored
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 	 * Prints a boolean value
127 	 *
128 	 * @param pValue
129 	 *            The value
130 	 */
131 	public void print(boolean pValue) {
132 		write(pValue ? "true" : "false");
133 	}
134 
135 	/**
136 	 * Prints a single character
137 	 *
138 	 * @param c
139 	 *            The character
140 	 */
141 	public void print(char c) {
142 		write(String.valueOf(c));
143 	}
144 
145 	/**
146 	 * Prints an integer value
147 	 *
148 	 * @param i
149 	 *            The value
150 	 */
151 	public void print(int i) {
152 		write(String.valueOf(i));
153 	}
154 
155 	/**
156 	 * Prints a long value
157 	 *
158 	 * @param l
159 	 *            The value
160 	 */
161 	public void print(long l) {
162 		write(String.valueOf(l));
163 	}
164 
165 	/**
166 	 * Prints a float value
167 	 *
168 	 * @param f
169 	 *            The value
170 	 */
171 	public void print(float f) {
172 		write(String.valueOf(f));
173 	}
174 
175 	/**
176 	 * Prints a double value
177 	 *
178 	 * @param d
179 	 *            The value
180 	 */
181 	public void print(double d) {
182 		write(String.valueOf(d));
183 	}
184 
185 	/**
186 	 * Prints a character array
187 	 *
188 	 * @param pArray
189 	 *            The array
190 	 */
191 	public void print(char pArray[]) {
192 		write(String.valueOf(pArray));
193 	}
194 
195 	/**
196 	 * Prints a string
197 	 *
198 	 * @param s
199 	 *            The string
200 	 */
201 	public void print(String s) {
202 		write(s == null ? "null" : s);
203 	}
204 
205 	/**
206 	 * Prints an object
207 	 *
208 	 * @param pObj
209 	 *            The object
210 	 */
211 	public void print(Object pObj) {
212 		write(String.valueOf(pObj));
213 	}
214 
215 	/**
216 	 * Prints a newline
217 	 */
218 	public void println() {
219 		newLine();
220 	}
221 
222 	/**
223 	 * println
224 	 *
225 	 * @param flag
226 	 */
227 	public void println(boolean flag) {
228 		synchronized (this) {
229 			print(flag);
230 			newLine();
231 		}
232 	}
233 
234 	/**
235 	 * println
236 	 *
237 	 * @param c
238 	 */
239 	public void println(char c) {
240 		synchronized (this) {
241 			print(c);
242 			newLine();
243 		}
244 	}
245 
246 	/**
247 	 * println
248 	 *
249 	 * @param i
250 	 */
251 	public void println(int i) {
252 		synchronized (this) {
253 			print(i);
254 			newLine();
255 		}
256 	}
257 
258 	/**
259 	 * println
260 	 *
261 	 * @param l
262 	 */
263 	public void println(long l) {
264 		synchronized (this) {
265 			print(l);
266 			newLine();
267 		}
268 	}
269 
270 	/**
271 	 * println
272 	 *
273 	 * @param f
274 	 */
275 	public void println(float f) {
276 		synchronized (this) {
277 			print(f);
278 			newLine();
279 		}
280 	}
281 
282 	/**
283 	 * println
284 	 *
285 	 * @param d
286 	 */
287 	public void println(double d) {
288 		synchronized (this) {
289 			print(d);
290 			newLine();
291 		}
292 	}
293 
294 	/**
295 	 * println
296 	 *
297 	 * @param ac
298 	 */
299 	public void println(char ac[]) {
300 		synchronized (this) {
301 			print(ac);
302 			newLine();
303 		}
304 	}
305 
306 	/**
307 	 * println
308 	 *
309 	 * @param s
310 	 */
311 	public void println(String s) {
312 		synchronized (this) {
313 			print(s);
314 			newLine();
315 		}
316 	}
317 
318 	/**
319 	 * println
320 	 *
321 	 * @param obj
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 				// i ++;
351 				if (this.iAutoFlush) this.out.flush();
352 			}
353 			// System.out.println("TOTAL:"+i);
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 		// trouble = x;
378 	}
379 
380 	/**
381 	 * Returns the last exception caught
382 	 *
383 	 * @return The exception
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 				// iCharOut = null;
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 }