001    /*
002     * Copyright (c) Andrey Kuznetsov. All Rights Reserved.
003     *
004     * http://uio.imagero.com
005     *
006     * Redistribution and use in source and binary forms, with or without
007     * modification, are permitted provided that the following conditions are met:
008     *
009     *  o Redistributions of source code must retain the above copyright notice,
010     *    this list of conditions and the following disclaimer.
011     *
012     *  o Redistributions in binary form must reproduce the above copyright notice,
013     *    this list of conditions and the following disclaimer in the documentation
014     *    and/or other materials provided with the distribution.
015     *
016     *  o Neither the name of Andrey Kuznetsov nor the names of
017     *    its contributors may be used to endorse or promote products derived
018     *    from this software without specific prior written permission.
019     *
020     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
022     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
023     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
027     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
028     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
029     * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
030     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031     */
032    
033    package com.imagero.uio;
034    
035    import java.io.IOException;
036    import java.io.PrintStream;
037    
038    /**
039     * @author Andrei Kouznetsov
040     *         Date: 05.07.2004
041     *         Time: 15:02:01
042     */
043    public class Sys {
044        public static final PrintStreamFilter out = new PrintStreamFilter(java.lang.System.out);
045        public static final PrintStreamFilter err = new PrintStreamFilter(java.lang.System.err);
046    
047        public static PrintStreamFilter getErr() {
048            return err;
049        }
050    
051        /**
052         * Set PrintStream used for output.
053         * Set to null to disable output.
054         * @param err PrintStream or null
055         */
056        public static void setErr(PrintStream err) {
057            Sys.err.setPs(err);
058        }
059    
060        public static PrintStreamFilter getOut() {
061            return out;
062        }
063    
064        /**
065         * Set PrintStream used for output.
066         * Set to null to disable output.
067         * @param out PrintStream or null
068         */
069        public static void setOut(PrintStream out) {
070            Sys.out.setPs(out);
071        }
072    
073        public static class PrintStreamFilter {
074            PrintStream ps;
075    
076            public PrintStreamFilter(PrintStream ps) {
077                this.ps = ps;
078            }
079    
080            public PrintStream getPrintStream() {
081                return ps;
082            }
083    
084            void setPs(PrintStream ps) {
085                this.ps = ps;
086            }
087    
088            public void print(String x) {
089                if (ps == null) {
090                    return;
091                }
092                if (x == null) {
093                    ps.print(x);
094                    return;
095                }
096                char[] chars = new char[x.length()];
097                x.getChars(0, x.length(), chars, 0);
098                int p = chars.length;
099                for (int j = 0; j < p; j++) {
100                    char c = chars[j];
101                    if (Character.isLetterOrDigit(c)) {
102                        chars[j] = c;
103                    } else if (Character.isWhitespace(c)) {
104                        chars[j] = c;
105                    } else if (Character.isISOControl(c)) {
106                        chars[j] = '.';
107                    } else {
108                        chars[j] = c;
109                    }
110                }
111                ps.print(chars);
112            }
113    
114            public void println(String x) {
115                print(x);
116                println();
117            }
118    
119            public void println() {
120                if (ps == null) {
121                    return;
122                }
123                ps.println();
124            }
125    
126            public void flush() {
127                if (ps == null) {
128                    return;
129                }
130                ps.flush();
131            }
132    
133            public void close() {
134                if (ps == null) {
135                    return;
136                }
137                ps.close();
138            }
139    
140            public boolean checkError() {
141                if (ps == null) {
142                    return false;
143                }
144                return ps.checkError();
145            }
146    
147            public void write(int b) {
148                if (ps == null) {
149                    return;
150                }
151                ps.write(b);
152            }
153    
154            public void write(byte buf[], int off, int len) {
155                if (ps == null) {
156                    return;
157                }
158                ps.write(buf, off, len);
159            }
160    
161            public void print(boolean b) {
162                if (ps == null) {
163                    return;
164                }
165                ps.print(b);
166            }
167    
168            public void print(char c) {
169                if (ps == null) {
170                    return;
171                }
172                ps.print(c);
173            }
174    
175            public void print(int i) {
176                if (ps == null) {
177                    return;
178                }
179                ps.print(i);
180            }
181    
182            public void print(long l) {
183                if (ps == null) {
184                    return;
185                }
186                ps.print(l);
187            }
188    
189            public void print(float f) {
190                if (ps == null) {
191                    return;
192                }
193                ps.print(f);
194            }
195    
196            public void print(double d) {
197                if (ps == null) {
198                    return;
199                }
200                ps.print(d);
201            }
202    
203            public void print(char s[]) {
204                if (ps == null) {
205                    return;
206                }
207                ps.print(s);
208            }
209    
210            public void print(Object obj) {
211                if (ps == null) {
212                    return;
213                }
214                ps.print(obj);
215            }
216    
217            public void println(boolean x) {
218                if (ps == null) {
219                    return;
220                }
221                ps.println(x);
222            }
223    
224            public void println(char x) {
225                if (ps == null) {
226                    return;
227                }
228                ps.println(x);
229            }
230    
231            public void println(int x) {
232                if (ps == null) {
233                    return;
234                }
235                ps.println(x);
236            }
237    
238            public void println(long x) {
239                if (ps == null) {
240                    return;
241                }
242                ps.println(x);
243            }
244    
245            public void println(float x) {
246                if (ps == null) {
247                    return;
248                }
249                ps.println(x);
250            }
251    
252            public void println(double x) {
253                if (ps == null) {
254                    return;
255                }
256                ps.println(x);
257            }
258    
259            public void println(char x[]) {
260                if (ps == null) {
261                    return;
262                }
263                ps.println(x);
264            }
265    
266            public void println(Object x) {
267                if (ps == null) {
268                    return;
269                }
270                ps.println(x);
271            }
272    
273            public void write(byte b[]) throws IOException {
274                if (ps == null) {
275                    return;
276                }
277                ps.write(b);
278            }
279    
280            public void println(Object[] objects) {
281                for (int i = 0; i < objects.length; i++) {
282                    print(objects[i]);
283                }
284                println();
285            }
286    
287            public void println(Object[] objects, String delimiter) {
288                for (int i = 0; i < objects.length; i++) {
289                    print(objects[i]);
290                    print(delimiter);
291                }
292                println();
293            }
294    
295            public void println(long[] longs, String delimiter) {
296                for (int i = 0; i < longs.length; i++) {
297                    print(longs[i]);
298                    print(delimiter);
299                }
300                println();
301            }
302    
303            public void println(int[] numbers, String delimiter) {
304                for (int i = 0; i < numbers.length; i++) {
305                    print(numbers[i]);
306                    print(delimiter);
307                }
308                println();
309            }
310    
311            public void println(char[] chars, String delimiter) {
312                for (int i = 0; i < chars.length; i++) {
313                    print(chars[i]);
314                    print(delimiter);
315                }
316                println();
317            }
318    
319            public void println(short[] shorts, String delimiter) {
320                for (int i = 0; i < shorts.length; i++) {
321                    print(shorts[i]);
322                    print(delimiter);
323                }
324                println();
325            }
326    
327            public void println(byte[] bytes, String delimiter) {
328                for (int i = 0; i < bytes.length; i++) {
329                    print(bytes[i]);
330                    print(delimiter);
331                }
332                println();
333            }
334        }
335    }