001
014
015 package com.liferay.portal.kernel.io.unsync;
016
017 import com.liferay.portal.kernel.util.StringPool;
018
019 import java.io.File;
020 import java.io.FileNotFoundException;
021 import java.io.FileOutputStream;
022 import java.io.FileWriter;
023 import java.io.IOException;
024 import java.io.InterruptedIOException;
025 import java.io.OutputStream;
026 import java.io.OutputStreamWriter;
027 import java.io.PrintWriter;
028 import java.io.UnsupportedEncodingException;
029 import java.io.Writer;
030
031 import java.util.Formatter;
032 import java.util.Locale;
033
034
041 public class UnsyncPrintWriter extends PrintWriter {
042
043 public UnsyncPrintWriter(File file) throws IOException {
044 this(new FileWriter(file), false);
045 }
046
047 public UnsyncPrintWriter(File file, String csn)
048 throws FileNotFoundException, UnsupportedEncodingException {
049
050 this(new OutputStreamWriter(new FileOutputStream(file), csn), false);
051 }
052
053 public UnsyncPrintWriter(OutputStream outputStream) {
054 this(outputStream, false);
055 }
056
057 public UnsyncPrintWriter(OutputStream outputStream, boolean autoFlush) {
058 this(new OutputStreamWriter(outputStream), autoFlush);
059 }
060
061 public UnsyncPrintWriter(String fileName) throws IOException {
062 this(new FileWriter(fileName), false);
063 }
064
065 public UnsyncPrintWriter(String fileName, String csn)
066 throws FileNotFoundException, UnsupportedEncodingException {
067
068 this(
069 new OutputStreamWriter(new FileOutputStream(fileName), csn), false);
070 }
071
072 public UnsyncPrintWriter(Writer writer) {
073 this(writer, false);
074 }
075
076 public UnsyncPrintWriter(Writer writer, boolean autoFlush) {
077 super(writer);
078
079 _writer = writer;
080 _autoFlush = autoFlush;
081 }
082
083 public PrintWriter append(char c) {
084 write(c);
085
086 return this;
087 }
088
089 public PrintWriter append(CharSequence charSequence) {
090 if (charSequence == null) {
091 write(StringPool.NULL);
092 }
093 else {
094 write(charSequence.toString());
095 }
096
097 return this;
098 }
099
100 public PrintWriter append(CharSequence charSequence, int start, int end) {
101 if (charSequence == null) {
102 charSequence = StringPool.NULL;
103 }
104
105 write(charSequence.subSequence(start, end).toString());
106
107 return this;
108 }
109
110 public boolean checkError() {
111 if (_writer != null) {
112 flush();
113 }
114
115 return _hasError;
116 }
117
118 public void close() {
119 try {
120 if (_writer == null) {
121 return;
122 }
123
124 _writer.close();
125
126 _writer = null;
127 }
128 catch (IOException ioe) {
129 _hasError = true;
130 }
131 }
132
133 public void flush() {
134 if (_writer == null) {
135 _hasError = true;
136 }
137 else {
138 try {
139 _writer.flush();
140 }
141 catch (IOException ioe) {
142 _hasError = true;
143 }
144 }
145 }
146
147 public PrintWriter format(
148 Locale locale, String format, Object... arguments) {
149
150 if (_writer == null) {
151 _hasError = true;
152 }
153 else {
154 try {
155 if ((_formatter == null) || (_formatter.locale() != locale)) {
156 _formatter = new Formatter(this, locale);
157 }
158
159 _formatter.format(locale, format, arguments);
160
161 if (_autoFlush) {
162 _writer.flush();
163 }
164 }
165 catch (InterruptedIOException iioe) {
166 Thread currentThread = Thread.currentThread();
167
168 currentThread.interrupt();
169 }
170 catch (IOException ioe) {
171 _hasError = true;
172 }
173 }
174
175 return this;
176 }
177
178 public PrintWriter format(String format, Object... arguments) {
179 return format(Locale.getDefault(), format, arguments);
180 }
181
182 public void print(boolean b) {
183 if (b) {
184 write(StringPool.TRUE);
185 }
186 else {
187 write(StringPool.FALSE);
188 }
189 }
190
191 public void print(char c) {
192 write(c);
193 }
194
195 public void print(char[] charArray) {
196 write(charArray);
197 }
198
199 public void print(double d) {
200 write(String.valueOf(d));
201 }
202
203 public void print(float f) {
204 write(String.valueOf(f));
205 }
206
207 public void print(int i) {
208 write(String.valueOf(i));
209 }
210
211 public void print(long l) {
212 write(String.valueOf(l));
213 }
214
215 public void print(Object object) {
216 write(String.valueOf(object));
217 }
218
219 public void print(String string) {
220 if (string == null) {
221 string = StringPool.NULL;
222 }
223
224 write(string);
225 }
226
227 public PrintWriter printf(
228 Locale locale, String format, Object... arguments) {
229
230 return format(locale, format, arguments);
231 }
232
233 public PrintWriter printf(String format, Object... arguments) {
234 return format(format, arguments);
235 }
236
237 public void println() {
238 if (_writer == null) {
239 _hasError = true;
240 }
241 else {
242 try {
243 _writer.write(_LINE_SEPARATOR);
244
245 if (_autoFlush) {
246 _writer.flush();
247 }
248 }
249 catch (InterruptedIOException iioe) {
250 Thread currentThread = Thread.currentThread();
251
252 currentThread.interrupt();
253 }
254 catch (IOException ioe) {
255 _hasError = true;
256 }
257 }
258 }
259
260 public void println(boolean b) {
261 print(b);
262 println();
263 }
264
265 public void println(char c) {
266 print(c);
267 println();
268 }
269
270 public void println(char[] charArray) {
271 print(charArray);
272 println();
273 }
274
275 public void println(double d) {
276 print(d);
277 println();
278 }
279
280 public void println(float f) {
281 print(f);
282 println();
283 }
284
285 public void println(int i) {
286 print(i);
287 println();
288 }
289
290 public void println(long l) {
291 print(l);
292 println();
293 }
294
295 public void println(Object object) {
296 print(object);
297 println();
298 }
299
300 public void println(String string) {
301 print(string);
302 println();
303 }
304
305 public void write(char[] charArray) {
306 write(charArray, 0, charArray.length);
307 }
308
309 public void write(char[] charArray, int offset, int length) {
310 if (_writer == null) {
311 _hasError = true;
312 }
313 else {
314 try {
315 _writer.write(charArray, offset, length);
316 }
317 catch (InterruptedIOException iioe) {
318 Thread currentThread = Thread.currentThread();
319
320 currentThread.interrupt();
321 }
322 catch (IOException ioe) {
323 _hasError = true;
324 }
325 }
326 }
327
328 public void write(int c) {
329 if (_writer == null) {
330 _hasError = true;
331 }
332 else {
333 try {
334 _writer.write(c);
335 }
336 catch (InterruptedIOException iioe) {
337 Thread currentThread = Thread.currentThread();
338
339 currentThread.interrupt();
340 }
341 catch (IOException ioe) {
342 _hasError = true;
343 }
344 }
345 }
346
347 public void write(String string) {
348 if (_writer == null) {
349 _hasError = true;
350 }
351 else {
352 try {
353 _writer.write(string);
354 }
355 catch (InterruptedIOException iioe) {
356 Thread currentThread = Thread.currentThread();
357
358 currentThread.interrupt();
359 }
360 catch (IOException ioe) {
361 _hasError = true;
362 }
363 }
364 }
365
366 public void write(String string, int offset, int length) {
367 if (_writer == null) {
368 _hasError = true;
369 }
370 else {
371 try {
372 _writer.write(string, offset, length);
373 }
374 catch (InterruptedIOException iioe) {
375 Thread currentThread = Thread.currentThread();
376
377 currentThread.interrupt();
378 }
379 catch (IOException ioe) {
380 _hasError = true;
381 }
382 }
383 }
384
385 private static String _LINE_SEPARATOR = System.getProperty(
386 "line.separator");
387
388 private boolean _autoFlush;
389 private Formatter _formatter;
390 private boolean _hasError;
391 private Writer _writer;
392
393 }