Source Code Cross Referenced for PrintWriter.java in  » 6.0-JDK-Modules » j2me » java » io » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » j2me » java.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)PrintWriter.java	1.32 06/10/13
003:         *
004:         * Copyright  1990-2006 Sun Microsystems, Inc. All Rights Reserved.  
005:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER  
006:         *   
007:         * This program is free software; you can redistribute it and/or  
008:         * modify it under the terms of the GNU General Public License version  
009:         * 2 only, as published by the Free Software Foundation.   
010:         *   
011:         * This program is distributed in the hope that it will be useful, but  
012:         * WITHOUT ANY WARRANTY; without even the implied warranty of  
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  
014:         * General Public License version 2 for more details (a copy is  
015:         * included at /legal/license.txt).   
016:         *   
017:         * You should have received a copy of the GNU General Public License  
018:         * version 2 along with this work; if not, write to the Free Software  
019:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  
020:         * 02110-1301 USA   
021:         *   
022:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa  
023:         * Clara, CA 95054 or visit www.sun.com if you need additional  
024:         * information or have any questions. 
025:         *
026:         */
027:
028:        package java.io;
029:
030:        /**
031:         * Print formatted representations of objects to a text-output stream.  This
032:         * class implements all of the print methods found in PrintStream.  It does not
033:         * contain methods for writing raw bytes, for which a program should use
034:         * unencoded byte streams.
035:         *
036:         * <p> Unlike the PrintStream class, if automatic flushing is enabled it will
037:         * be done only when one of the println() methods is invoked, rather than
038:         * whenever a newline character happens to be output.  The println() methods
039:         * use the platform's own notion of line separator rather than the newline
040:         * character.
041:         *
042:         * <p> Methods in this class never throw I/O exceptions.  The client may
043:         * inquire as to whether any errors have occurred by invoking checkError().
044:         *
045:         * @version 	1.24, 02/02/00
046:         * @author	Mark Reinhold
047:         * @since	JDK1.1
048:         */
049:
050:        public class PrintWriter extends Writer {
051:
052:            /**
053:             * The underlying character-output stream of this
054:             * <code>PrintWriter</code>.
055:             *
056:             * @since 1.2
057:             */
058:            protected Writer out;
059:
060:            private boolean autoFlush = false;
061:            private boolean trouble = false;
062:
063:            /**
064:             * Line separator string.  This is the value of the line.separator
065:             * property at the moment that the stream was created.
066:             */
067:            private String lineSeparator;
068:
069:            /**
070:             * Create a new PrintWriter, without automatic line flushing.
071:             *
072:             * @param  out        A character-output stream
073:             */
074:            public PrintWriter(Writer out) {
075:                this (out, false);
076:            }
077:
078:            /**
079:             * Create a new PrintWriter.
080:             *
081:             * @param  out        A character-output stream
082:             * @param  autoFlush  A boolean; if true, the println() methods will flush
083:             *                    the output buffer
084:             */
085:            public PrintWriter(Writer out, boolean autoFlush) {
086:                super (out);
087:                this .out = out;
088:                this .autoFlush = autoFlush;
089:                lineSeparator = (String) java.security.AccessController
090:                        .doPrivileged(new sun.security.action.GetPropertyAction(
091:                                "line.separator"));
092:            }
093:
094:            /**
095:             * Create a new PrintWriter, without automatic line flushing, from an
096:             * existing OutputStream.  This convenience constructor creates the
097:             * necessary intermediate OutputStreamWriter, which will convert characters
098:             * into bytes using the default character encoding.
099:             *
100:             * @param  out        An output stream
101:             *
102:             * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
103:             */
104:            public PrintWriter(OutputStream out) {
105:                this (out, false);
106:            }
107:
108:            /**
109:             * Create a new PrintWriter from an existing OutputStream.  This
110:             * convenience constructor creates the necessary intermediate
111:             * OutputStreamWriter, which will convert characters into bytes using the
112:             * default character encoding.
113:             *
114:             * @param  out        An output stream
115:             * @param  autoFlush  A boolean; if true, the println() methods will flush
116:             *                    the output buffer
117:             *
118:             * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
119:             */
120:            public PrintWriter(OutputStream out, boolean autoFlush) {
121:                this (new BufferedWriter(new OutputStreamWriter(out)), autoFlush);
122:            }
123:
124:            /** Check to make sure that the stream has not been closed */
125:            private void ensureOpen() throws IOException {
126:                if (out == null)
127:                    throw new IOException("Stream closed");
128:            }
129:
130:            /** 
131:             * Flush the stream. 
132:             * @see #checkError()
133:             */
134:            public void flush() {
135:                try {
136:                    synchronized (lock) {
137:                        ensureOpen();
138:                        out.flush();
139:                    }
140:                } catch (IOException x) {
141:                    trouble = true;
142:                }
143:            }
144:
145:            /** 
146:             * Close the stream. 
147:             * @see #checkError()
148:             */
149:            public void close() {
150:                try {
151:                    synchronized (lock) {
152:                        if (out == null)
153:                            return;
154:                        out.close();
155:                        out = null;
156:                    }
157:                } catch (IOException x) {
158:                    trouble = true;
159:                }
160:            }
161:
162:            /**
163:             * Flush the stream if it's not closed and check its error state.  
164:             * Errors are cumulative; once the stream encounters an error, this 
165:             * routine will return true on all successive calls.
166:             *
167:             * @return True if the print stream has encountered an error, either on the
168:             * underlying output stream or during a format conversion.
169:             */
170:            public boolean checkError() {
171:                if (out != null)
172:                    flush();
173:                return trouble;
174:            }
175:
176:            /** Indicate that an error has occurred. */
177:            protected void setError() {
178:                trouble = true;
179:            }
180:
181:            /*
182:             * Exception-catching, synchronized output operations,
183:             * which also implement the write() methods of Writer
184:             */
185:
186:            /** 
187:             * Write a single character.
188:             * @param c int specifying a character to be written.
189:             */
190:            public void write(int c) {
191:                try {
192:                    synchronized (lock) {
193:                        ensureOpen();
194:                        out.write(c);
195:                    }
196:                } catch (InterruptedIOException x) {
197:                    Thread.currentThread().interrupt();
198:                } catch (IOException x) {
199:                    trouble = true;
200:                }
201:            }
202:
203:            /** 
204:             * Write a portion of an array of characters. 
205:             * @param buf Array of characters
206:             * @param off Offset from which to start writing characters
207:             * @param len Number of characters to write
208:             */
209:            public void write(char buf[], int off, int len) {
210:                try {
211:                    synchronized (lock) {
212:                        ensureOpen();
213:                        out.write(buf, off, len);
214:                    }
215:                } catch (InterruptedIOException x) {
216:                    Thread.currentThread().interrupt();
217:                } catch (IOException x) {
218:                    trouble = true;
219:                }
220:            }
221:
222:            /**
223:             * Write an array of characters.  This method cannot be inherited from the
224:             * Writer class because it must suppress I/O exceptions.
225:             * @param buf Array of characters to be written
226:             */
227:            public void write(char buf[]) {
228:                write(buf, 0, buf.length);
229:            }
230:
231:            /** 
232:             * Write a portion of a string. 
233:             * @param s A String
234:             * @param off Offset from which to start writing characters
235:             * @param len Number of characters to write
236:             */
237:            public void write(String s, int off, int len) {
238:                try {
239:                    synchronized (lock) {
240:                        ensureOpen();
241:                        out.write(s, off, len);
242:                    }
243:                } catch (InterruptedIOException x) {
244:                    Thread.currentThread().interrupt();
245:                } catch (IOException x) {
246:                    trouble = true;
247:                }
248:            }
249:
250:            /**
251:             * Write a string.  This method cannot be inherited from the Writer class
252:             * because it must suppress I/O exceptions.
253:             * @param s String to be written
254:             */
255:            public void write(String s) {
256:                write(s, 0, s.length());
257:            }
258:
259:            private void newLine() {
260:                try {
261:                    synchronized (lock) {
262:                        ensureOpen();
263:                        out.write(lineSeparator);
264:                        if (autoFlush)
265:                            out.flush();
266:                    }
267:                } catch (InterruptedIOException x) {
268:                    Thread.currentThread().interrupt();
269:                } catch (IOException x) {
270:                    trouble = true;
271:                }
272:            }
273:
274:            /* Methods that do not terminate lines */
275:
276:            /**
277:             * Print a boolean value.  The string produced by <code>{@link
278:             * java.lang.String#valueOf(boolean)}</code> is translated into bytes
279:             * according to the platform's default character encoding, and these bytes
280:             * are written in exactly the manner of the <code>{@link
281:             * #write(int)}</code> method.
282:             *
283:             * @param      b   The <code>boolean</code> to be printed
284:             */
285:            public void print(boolean b) {
286:                write(b ? "true" : "false");
287:            }
288:
289:            /**
290:             * Print a character.  The character is translated into one or more bytes
291:             * according to the platform's default character encoding, and these bytes
292:             * are written in exactly the manner of the <code>{@link
293:             * #write(int)}</code> method.
294:             *
295:             * @param      c   The <code>char</code> to be printed
296:             */
297:            public void print(char c) {
298:                write(c);
299:            }
300:
301:            /**
302:             * Print an integer.  The string produced by <code>{@link
303:             * java.lang.String#valueOf(int)}</code> is translated into bytes according
304:             * to the platform's default character encoding, and these bytes are
305:             * written in exactly the manner of the <code>{@link #write(int)}</code>
306:             * method.
307:             *
308:             * @param      i   The <code>int</code> to be printed
309:             * @see        java.lang.Integer#toString(int)
310:             */
311:            public void print(int i) {
312:                write(String.valueOf(i));
313:            }
314:
315:            /**
316:             * Print a long integer.  The string produced by <code>{@link
317:             * java.lang.String#valueOf(long)}</code> is translated into bytes
318:             * according to the platform's default character encoding, and these bytes
319:             * are written in exactly the manner of the <code>{@link #write(int)}</code>
320:             * method.
321:             *
322:             * @param      l   The <code>long</code> to be printed
323:             * @see        java.lang.Long#toString(long)
324:             */
325:            public void print(long l) {
326:                write(String.valueOf(l));
327:            }
328:
329:            /**
330:             * Print a floating-point number.  The string produced by <code>{@link
331:             * java.lang.String#valueOf(float)}</code> is translated into bytes
332:             * according to the platform's default character encoding, and these bytes
333:             * are written in exactly the manner of the <code>{@link #write(int)}</code>
334:             * method.
335:             *
336:             * @param      f   The <code>float</code> to be printed
337:             * @see        java.lang.Float#toString(float)
338:             */
339:            public void print(float f) {
340:                write(String.valueOf(f));
341:            }
342:
343:            /**
344:             * Print a double-precision floating-point number.  The string produced by
345:             * <code>{@link java.lang.String#valueOf(double)}</code> is translated into
346:             * bytes according to the platform's default character encoding, and these
347:             * bytes are written in exactly the manner of the <code>{@link
348:             * #write(int)}</code> method.
349:             *
350:             * @param      d   The <code>double</code> to be printed
351:             * @see        java.lang.Double#toString(double)
352:             */
353:            public void print(double d) {
354:                write(String.valueOf(d));
355:            }
356:
357:            /**
358:             * Print an array of characters.  The characters are converted into bytes
359:             * according to the platform's default character encoding, and these bytes
360:             * are written in exactly the manner of the <code>{@link #write(int)}</code>
361:             * method.
362:             *
363:             * @param      s   The array of chars to be printed
364:             *
365:             * @throws  NullPointerException  If <code>s</code> is <code>null</code>
366:             */
367:            public void print(char s[]) {
368:                write(s);
369:            }
370:
371:            /**
372:             * Print a string.  If the argument is <code>null</code> then the string
373:             * <code>"null"</code> is printed.  Otherwise, the string's characters are
374:             * converted into bytes according to the platform's default character
375:             * encoding, and these bytes are written in exactly the manner of the
376:             * <code>{@link #write(int)}</code> method.
377:             *
378:             * @param      s   The <code>String</code> to be printed
379:             */
380:            public void print(String s) {
381:                if (s == null) {
382:                    s = "null";
383:                }
384:                write(s);
385:            }
386:
387:            /**
388:             * Print an object.  The string produced by the <code>{@link
389:             * java.lang.String#valueOf(Object)}</code> method is translated into bytes
390:             * according to the platform's default character encoding, and these bytes
391:             * are written in exactly the manner of the <code>{@link #write(int)}</code>
392:             * method.
393:             *
394:             * @param      obj   The <code>Object</code> to be printed
395:             * @see        java.lang.Object#toString()
396:             */
397:            public void print(Object obj) {
398:                write(String.valueOf(obj));
399:            }
400:
401:            /* Methods that do terminate lines */
402:
403:            /**
404:             * Terminate the current line by writing the line separator string.  The
405:             * line separator string is defined by the system property
406:             * <code>line.separator</code>, and is not necessarily a single newline
407:             * character (<code>'\n'</code>).
408:             */
409:            public void println() {
410:                newLine();
411:            }
412:
413:            /**
414:             * Print a boolean value and then terminate the line.  This method behaves
415:             * as though it invokes <code>{@link #print(boolean)}</code> and then
416:             * <code>{@link #println()}</code>.
417:             *
418:             * @param x the <code>boolean</code> value to be printed
419:             */
420:            public void println(boolean x) {
421:                synchronized (lock) {
422:                    print(x);
423:                    println();
424:                }
425:            }
426:
427:            /**
428:             * Print a character and then terminate the line.  This method behaves as
429:             * though it invokes <code>{@link #print(char)}</code> and then <code>{@link
430:             * #println()}</code>.
431:             *
432:             * @param x the <code>char</code> value to be printed
433:             */
434:            public void println(char x) {
435:                synchronized (lock) {
436:                    print(x);
437:                    println();
438:                }
439:            }
440:
441:            /**
442:             * Print an integer and then terminate the line.  This method behaves as
443:             * though it invokes <code>{@link #print(int)}</code> and then <code>{@link
444:             * #println()}</code>.
445:             *
446:             * @param x the <code>int</code> value to be printed
447:             */
448:            public void println(int x) {
449:                synchronized (lock) {
450:                    print(x);
451:                    println();
452:                }
453:            }
454:
455:            /**
456:             * Print a long integer and then terminate the line.  This method behaves
457:             * as though it invokes <code>{@link #print(long)}</code> and then
458:             * <code>{@link #println()}</code>.
459:             *
460:             * @param x the <code>long</code> value to be printed
461:             */
462:            public void println(long x) {
463:                synchronized (lock) {
464:                    print(x);
465:                    println();
466:                }
467:            }
468:
469:            /**
470:             * Print a floating-point number and then terminate the line.  This method
471:             * behaves as though it invokes <code>{@link #print(float)}</code> and then
472:             * <code>{@link #println()}</code>.
473:             *
474:             * @param x the <code>float</code> value to be printed
475:             */
476:            public void println(float x) {
477:                synchronized (lock) {
478:                    print(x);
479:                    println();
480:                }
481:            }
482:
483:            /**
484:             * Print a double-precision floating-point number and then terminate the
485:             * line.  This method behaves as though it invokes <code>{@link
486:             * #print(double)}</code> and then <code>{@link #println()}</code>.
487:             *
488:             * @param x the <code>double</code> value to be printed
489:             */
490:            public void println(double x) {
491:                synchronized (lock) {
492:                    print(x);
493:                    println();
494:                }
495:            }
496:
497:            /**
498:             * Print an array of characters and then terminate the line.  This method
499:             * behaves as though it invokes <code>{@link #print(char[])}</code> and then
500:             * <code>{@link #println()}</code>.
501:             *
502:             * @param x the array of <code>char</code> values to be printed
503:             */
504:            public void println(char x[]) {
505:                synchronized (lock) {
506:                    print(x);
507:                    println();
508:                }
509:            }
510:
511:            /**
512:             * Print a String and then terminate the line.  This method behaves as
513:             * though it invokes <code>{@link #print(String)}</code> and then
514:             * <code>{@link #println()}</code>.
515:             *
516:             * @param x the <code>String</code> value to be printed
517:             */
518:            public void println(String x) {
519:                synchronized (lock) {
520:                    print(x);
521:                    println();
522:                }
523:            }
524:
525:            /**
526:             * Print an Object and then terminate the line.  This method behaves as
527:             * though it invokes <code>{@link #print(Object)}</code> and then
528:             * <code>{@link #println()}</code>.
529:             *
530:             * @param x the <code>Object</code> value to be printed
531:             */
532:            public void println(Object x) {
533:                synchronized (lock) {
534:                    print(x);
535:                    println();
536:                }
537:            }
538:
539:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.