1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.io.unsync;
16  
17  import com.liferay.portal.kernel.util.StringPool;
18  
19  import java.io.File;
20  import java.io.FileNotFoundException;
21  import java.io.FileOutputStream;
22  import java.io.FileWriter;
23  import java.io.IOException;
24  import java.io.InterruptedIOException;
25  import java.io.OutputStream;
26  import java.io.OutputStreamWriter;
27  import java.io.PrintWriter;
28  import java.io.UnsupportedEncodingException;
29  import java.io.Writer;
30  
31  import java.util.Formatter;
32  import java.util.Locale;
33  
34  /**
35   * <a href="UnsyncPrintWriter.java.html"><b><i>View Source</i></b></a>
36   *
37   * <p>
38   * See http://issues.liferay.com/browse/LPS-6648.
39   * </p>
40   *
41   * @author Shuyang Zhou
42   */
43  public class UnsyncPrintWriter extends PrintWriter {
44  
45      public UnsyncPrintWriter(File file) throws IOException {
46          this(new FileWriter(file), false);
47      }
48  
49      public UnsyncPrintWriter(File file, String csn)
50          throws FileNotFoundException, UnsupportedEncodingException {
51  
52          this(new OutputStreamWriter(new FileOutputStream(file), csn), false);
53      }
54  
55      public UnsyncPrintWriter(OutputStream outputStream) {
56          this(outputStream, false);
57      }
58  
59      public UnsyncPrintWriter(OutputStream outputStream, boolean autoFlush) {
60          this(new OutputStreamWriter(outputStream), autoFlush);
61      }
62  
63      public UnsyncPrintWriter(String fileName) throws IOException {
64          this(new FileWriter(fileName), false);
65      }
66  
67      public UnsyncPrintWriter(String fileName, String csn)
68          throws FileNotFoundException, UnsupportedEncodingException {
69  
70          this(
71              new OutputStreamWriter(new FileOutputStream(fileName), csn), false);
72      }
73  
74      public UnsyncPrintWriter(Writer writer) {
75          this(writer, false);
76      }
77  
78      public UnsyncPrintWriter(Writer writer, boolean autoFlush) {
79          super(writer);
80  
81          _writer = writer;
82          _autoFlush = autoFlush;
83      }
84  
85      public PrintWriter append(char c) {
86          write(c);
87  
88          return this;
89      }
90  
91      public PrintWriter append(CharSequence charSequence) {
92          if (charSequence == null) {
93              write(StringPool.NULL);
94          }
95          else {
96              write(charSequence.toString());
97          }
98  
99          return this;
100     }
101 
102     public PrintWriter append(CharSequence charSequence, int start, int end) {
103         if (charSequence == null) {
104             charSequence = StringPool.NULL;
105         }
106 
107         write(charSequence.subSequence(start, end).toString());
108 
109         return this;
110     }
111 
112     public boolean checkError() {
113         if (_writer != null) {
114             flush();
115         }
116 
117         return _hasError;
118     }
119 
120     public void close() {
121         try {
122             if (_writer == null) {
123                 return;
124             }
125 
126             _writer.close();
127 
128             _writer = null;
129         }
130         catch (IOException ioe) {
131             _hasError = true;
132         }
133     }
134 
135     public void flush() {
136         if (_writer == null) {
137             _hasError = true;
138         }
139         else {
140             try {
141                 _writer.flush();
142             }
143             catch (IOException ioe) {
144                 _hasError = true;
145             }
146         }
147     }
148 
149     public PrintWriter format(
150         Locale locale, String format, Object... arguments) {
151 
152         if (_writer == null) {
153             _hasError = true;
154         }
155         else {
156             try {
157                 if ((_formatter == null) || (_formatter.locale() != locale)) {
158                     _formatter = new Formatter(this, locale);
159                 }
160 
161                 _formatter.format(locale, format, arguments);
162 
163                 if (_autoFlush) {
164                     _writer.flush();
165                 }
166             }
167             catch (InterruptedIOException iioe) {
168                 Thread currentThread = Thread.currentThread();
169 
170                 currentThread.interrupt();
171             }
172             catch (IOException ioe) {
173                 _hasError = true;
174             }
175         }
176 
177         return this;
178     }
179 
180     public PrintWriter format(String format, Object... arguments) {
181         return format(Locale.getDefault(), format, arguments);
182     }
183 
184     public void print(boolean b) {
185         if (b) {
186             write(StringPool.TRUE);
187         }
188         else {
189             write(StringPool.FALSE);
190         }
191     }
192 
193     public void print(char c) {
194         write(c);
195     }
196 
197     public void print(char[] charArray) {
198         write(charArray);
199     }
200 
201     public void print(double d) {
202         write(String.valueOf(d));
203     }
204 
205     public void print(float f) {
206         write(String.valueOf(f));
207     }
208 
209     public void print(int i) {
210         write(String.valueOf(i));
211     }
212 
213     public void print(long l) {
214         write(String.valueOf(l));
215     }
216 
217     public void print(Object object) {
218         write(String.valueOf(object));
219     }
220 
221     public void print(String string) {
222         if (string == null) {
223             string = StringPool.NULL;
224         }
225 
226         write(string);
227     }
228 
229     public PrintWriter printf(
230         Locale locale, String format, Object... arguments) {
231 
232         return format(locale, format, arguments);
233     }
234 
235     public PrintWriter printf(String format, Object... arguments) {
236         return format(format, arguments);
237     }
238 
239     public void println() {
240         if (_writer == null) {
241             _hasError = true;
242         }
243         else {
244             try {
245                 _writer.write(_LINE_SEPARATOR);
246 
247                 if (_autoFlush) {
248                     _writer.flush();
249                 }
250             }
251             catch (InterruptedIOException iioe) {
252                 Thread currentThread = Thread.currentThread();
253 
254                 currentThread.interrupt();
255             }
256             catch (IOException ioe) {
257                 _hasError = true;
258             }
259         }
260     }
261 
262     public void println(boolean b) {
263         print(b);
264         println();
265     }
266 
267     public void println(char c) {
268         print(c);
269         println();
270     }
271 
272     public void println(char[] charArray) {
273         print(charArray);
274         println();
275     }
276 
277     public void println(double d) {
278         print(d);
279         println();
280     }
281 
282     public void println(float f) {
283         print(f);
284         println();
285     }
286 
287     public void println(int i) {
288         print(i);
289         println();
290     }
291 
292     public void println(long l) {
293         print(l);
294         println();
295     }
296 
297     public void println(Object object) {
298         print(object);
299         println();
300     }
301 
302     public void println(String string) {
303         print(string);
304         println();
305     }
306 
307     public void write(char[] charArray) {
308         write(charArray, 0, charArray.length);
309     }
310 
311     public void write(char[] charArray, int offset, int length) {
312         if (_writer == null) {
313             _hasError = true;
314         }
315         else {
316             try {
317                 _writer.write(charArray, offset, length);
318             }
319             catch (InterruptedIOException iioe) {
320                 Thread currentThread = Thread.currentThread();
321 
322                 currentThread.interrupt();
323             }
324             catch (IOException ioe) {
325                 _hasError = true;
326             }
327         }
328     }
329 
330     public void write(int c) {
331         if (_writer == null) {
332             _hasError = true;
333         }
334         else {
335             try {
336                 _writer.write(c);
337             }
338             catch (InterruptedIOException iioe) {
339                 Thread currentThread = Thread.currentThread();
340 
341                 currentThread.interrupt();
342             }
343             catch (IOException ioe) {
344                 _hasError = true;
345             }
346         }
347     }
348 
349     public void write(String string) {
350         if (_writer == null) {
351             _hasError = true;
352         }
353         else {
354             try {
355                 _writer.write(string);
356             }
357             catch (InterruptedIOException iioe) {
358                 Thread currentThread = Thread.currentThread();
359 
360                 currentThread.interrupt();
361             }
362             catch (IOException ioe) {
363                 _hasError = true;
364             }
365         }
366     }
367 
368     public void write(String string, int offset, int length) {
369         if (_writer == null) {
370             _hasError = true;
371         }
372         else {
373             try {
374                 _writer.write(string, offset, length);
375             }
376             catch (InterruptedIOException iioe) {
377                 Thread currentThread = Thread.currentThread();
378 
379                 currentThread.interrupt();
380             }
381             catch (IOException ioe) {
382                 _hasError = true;
383             }
384         }
385     }
386 
387     private static String _LINE_SEPARATOR = System.getProperty(
388         "line.separator");
389 
390     private boolean _autoFlush;
391     private Formatter _formatter;
392     private boolean _hasError;
393     private Writer _writer;
394 
395 }