001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.io.unsync;
016    
017    import com.liferay.portal.kernel.io.OutputStreamWriter;
018    import com.liferay.portal.kernel.util.StringPool;
019    
020    import java.io.File;
021    import java.io.FileNotFoundException;
022    import java.io.FileOutputStream;
023    import java.io.FileWriter;
024    import java.io.IOException;
025    import java.io.InterruptedIOException;
026    import java.io.OutputStream;
027    import java.io.PrintWriter;
028    import java.io.Writer;
029    
030    import java.util.Formatter;
031    import java.util.Locale;
032    
033    /**
034     * <p>
035     * See http://issues.liferay.com/browse/LPS-6648.
036     * </p>
037     *
038     * @author Shuyang Zhou
039     */
040    public class UnsyncPrintWriter extends PrintWriter {
041    
042            public UnsyncPrintWriter(File file) throws IOException {
043                    this(new FileWriter(file));
044            }
045    
046            public UnsyncPrintWriter(File file, String csn)
047                    throws FileNotFoundException {
048    
049                    this(new OutputStreamWriter(new FileOutputStream(file), csn));
050            }
051    
052            public UnsyncPrintWriter(OutputStream outputStream) {
053                    this(new OutputStreamWriter(outputStream));
054            }
055    
056            public UnsyncPrintWriter(String fileName) throws IOException {
057                    this(new FileWriter(fileName));
058            }
059    
060            public UnsyncPrintWriter(String fileName, String csn)
061                    throws FileNotFoundException {
062    
063                    this(new OutputStreamWriter(new FileOutputStream(fileName), csn));
064            }
065    
066            public UnsyncPrintWriter(Writer writer) {
067                    super(writer);
068    
069                    _writer = writer;
070            }
071    
072            @Override
073            public PrintWriter append(char c) {
074                    write(c);
075    
076                    return this;
077            }
078    
079            @Override
080            public PrintWriter append(CharSequence charSequence) {
081                    if (charSequence == null) {
082                            write(StringPool.NULL);
083                    }
084                    else {
085                            write(charSequence.toString());
086                    }
087    
088                    return this;
089            }
090    
091            @Override
092            public PrintWriter append(CharSequence charSequence, int start, int end) {
093                    if (charSequence == null) {
094                            charSequence = StringPool.NULL;
095                    }
096    
097                    write(charSequence.subSequence(start, end).toString());
098    
099                    return this;
100            }
101    
102            @Override
103            public boolean checkError() {
104                    if (_writer != null) {
105                            flush();
106                    }
107    
108                    return _hasError;
109            }
110    
111            @Override
112            public void close() {
113                    try {
114                            if (_writer == null) {
115                                    return;
116                            }
117    
118                            _writer.close();
119    
120                            _writer = null;
121                    }
122                    catch (IOException ioe) {
123                            _hasError = true;
124                    }
125            }
126    
127            @Override
128            public void flush() {
129                    if (_writer == null) {
130                            _hasError = true;
131                    }
132                    else {
133                            try {
134                                    _writer.flush();
135                            }
136                            catch (IOException ioe) {
137                                    _hasError = true;
138                            }
139                    }
140            }
141    
142            @Override
143            public PrintWriter format(
144                    Locale locale, String format, Object... arguments) {
145    
146                    if (_writer == null) {
147                            _hasError = true;
148                    }
149                    else {
150                            if ((_formatter == null) || (_formatter.locale() != locale)) {
151                                    _formatter = new Formatter(this, locale);
152                            }
153    
154                            _formatter.format(locale, format, arguments);
155                    }
156    
157                    return this;
158            }
159    
160            @Override
161            public PrintWriter format(String format, Object... arguments) {
162                    return format(Locale.getDefault(), format, arguments);
163            }
164    
165            @Override
166            public void print(boolean b) {
167                    if (b) {
168                            write(StringPool.TRUE);
169                    }
170                    else {
171                            write(StringPool.FALSE);
172                    }
173            }
174    
175            @Override
176            public void print(char c) {
177                    write(c);
178            }
179    
180            @Override
181            public void print(char[] chars) {
182                    write(chars);
183            }
184    
185            @Override
186            public void print(double d) {
187                    write(String.valueOf(d));
188            }
189    
190            @Override
191            public void print(float f) {
192                    write(String.valueOf(f));
193            }
194    
195            @Override
196            public void print(int i) {
197                    write(String.valueOf(i));
198            }
199    
200            @Override
201            public void print(long l) {
202                    write(String.valueOf(l));
203            }
204    
205            @Override
206            public void print(Object object) {
207                    write(String.valueOf(object));
208            }
209    
210            @Override
211            public void print(String string) {
212                    if (string == null) {
213                            string = StringPool.NULL;
214                    }
215    
216                    write(string);
217            }
218    
219            @Override
220            public PrintWriter printf(
221                    Locale locale, String format, Object... arguments) {
222    
223                    return format(locale, format, arguments);
224            }
225    
226            @Override
227            public PrintWriter printf(String format, Object... arguments) {
228                    return format(format, arguments);
229            }
230    
231            @Override
232            public void println() {
233                    if (_writer == null) {
234                            _hasError = true;
235                    }
236                    else {
237                            try {
238                                    _writer.write(_LINE_SEPARATOR);
239                            }
240                            catch (InterruptedIOException iioe) {
241                                    Thread currentThread = Thread.currentThread();
242    
243                                    currentThread.interrupt();
244                            }
245                            catch (IOException ioe) {
246                                    _hasError = true;
247                            }
248                    }
249            }
250    
251            @Override
252            public void println(boolean b) {
253                    print(b);
254                    println();
255            }
256    
257            @Override
258            public void println(char c) {
259                    print(c);
260                    println();
261            }
262    
263            @Override
264            public void println(char[] chars) {
265                    print(chars);
266                    println();
267            }
268    
269            @Override
270            public void println(double d) {
271                    print(d);
272                    println();
273            }
274    
275            @Override
276            public void println(float f) {
277                    print(f);
278                    println();
279            }
280    
281            @Override
282            public void println(int i) {
283                    print(i);
284                    println();
285            }
286    
287            @Override
288            public void println(long l) {
289                    print(l);
290                    println();
291            }
292    
293            @Override
294            public void println(Object object) {
295                    print(object);
296                    println();
297            }
298    
299            @Override
300            public void println(String string) {
301                    print(string);
302                    println();
303            }
304    
305            public void reset(Writer writer) {
306                    _formatter = null;
307                    _hasError = false;
308                    _writer = writer;
309    
310                    out = _writer;
311            }
312    
313            @Override
314            public void write(char[] chars) {
315                    write(chars, 0, chars.length);
316            }
317    
318            @Override
319            public void write(char[] chars, int offset, int length) {
320                    if (_writer == null) {
321                            _hasError = true;
322                    }
323                    else {
324                            try {
325                                    _writer.write(chars, offset, length);
326                            }
327                            catch (InterruptedIOException iioe) {
328                                    Thread currentThread = Thread.currentThread();
329    
330                                    currentThread.interrupt();
331                            }
332                            catch (IOException ioe) {
333                                    _hasError = true;
334                            }
335                    }
336            }
337    
338            @Override
339            public void write(int c) {
340                    if (_writer == null) {
341                            _hasError = true;
342                    }
343                    else {
344                            try {
345                                    _writer.write(c);
346                            }
347                            catch (InterruptedIOException iioe) {
348                                    Thread currentThread = Thread.currentThread();
349    
350                                    currentThread.interrupt();
351                            }
352                            catch (IOException ioe) {
353                                    _hasError = true;
354                            }
355                    }
356            }
357    
358            @Override
359            public void write(String string) {
360                    if (_writer == null) {
361                            _hasError = true;
362                    }
363                    else {
364                            try {
365                                    _writer.write(string);
366                            }
367                            catch (InterruptedIOException iioe) {
368                                    Thread currentThread = Thread.currentThread();
369    
370                                    currentThread.interrupt();
371                            }
372                            catch (IOException ioe) {
373                                    _hasError = true;
374                            }
375                    }
376            }
377    
378            @Override
379            public void write(String string, int offset, int length) {
380                    if (_writer == null) {
381                            _hasError = true;
382                    }
383                    else {
384                            try {
385                                    _writer.write(string, offset, length);
386                            }
387                            catch (InterruptedIOException iioe) {
388                                    Thread currentThread = Thread.currentThread();
389    
390                                    currentThread.interrupt();
391                            }
392                            catch (IOException ioe) {
393                                    _hasError = true;
394                            }
395                    }
396            }
397    
398            private static final String _LINE_SEPARATOR = System.getProperty(
399                    "line.separator");
400    
401            private Formatter _formatter;
402            private boolean _hasError;
403            private Writer _writer;
404    
405    }