001
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
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 lock = _writer;
311 out = _writer;
312 }
313
314 @Override
315 public void write(char[] chars) {
316 write(chars, 0, chars.length);
317 }
318
319 @Override
320 public void write(char[] chars, int offset, int length) {
321 if (_writer == null) {
322 _hasError = true;
323 }
324 else {
325 try {
326 _writer.write(chars, offset, length);
327 }
328 catch (InterruptedIOException iioe) {
329 Thread currentThread = Thread.currentThread();
330
331 currentThread.interrupt();
332 }
333 catch (IOException ioe) {
334 _hasError = true;
335 }
336 }
337 }
338
339 @Override
340 public void write(int c) {
341 if (_writer == null) {
342 _hasError = true;
343 }
344 else {
345 try {
346 _writer.write(c);
347 }
348 catch (InterruptedIOException iioe) {
349 Thread currentThread = Thread.currentThread();
350
351 currentThread.interrupt();
352 }
353 catch (IOException ioe) {
354 _hasError = true;
355 }
356 }
357 }
358
359 @Override
360 public void write(String string) {
361 if (_writer == null) {
362 _hasError = true;
363 }
364 else {
365 try {
366 _writer.write(string);
367 }
368 catch (InterruptedIOException iioe) {
369 Thread currentThread = Thread.currentThread();
370
371 currentThread.interrupt();
372 }
373 catch (IOException ioe) {
374 _hasError = true;
375 }
376 }
377 }
378
379 @Override
380 public void write(String string, int offset, int length) {
381 if (_writer == null) {
382 _hasError = true;
383 }
384 else {
385 try {
386 _writer.write(string, offset, length);
387 }
388 catch (InterruptedIOException iioe) {
389 Thread currentThread = Thread.currentThread();
390
391 currentThread.interrupt();
392 }
393 catch (IOException ioe) {
394 _hasError = true;
395 }
396 }
397 }
398
399 private static final String _LINE_SEPARATOR = System.getProperty(
400 "line.separator");
401
402 private Formatter _formatter;
403 private boolean _hasError;
404 private Writer _writer;
405
406 }