Source Code Cross Referenced for AppendableWriter.java in  » Development » Javolution » javolution » 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 » Development » Javolution » javolution.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
003:         * Copyright (C) 2006 - Javolution (http://javolution.org/)
004:         * All rights reserved.
005:         * 
006:         * Permission to use, copy, modify, and distribute this software is
007:         * freely granted, provided that this notice is preserved.
008:         */
009:        package javolution.io;
010:
011:        import j2me.lang.CharSequence;
012:        import j2me.lang.IllegalStateException;
013:        import j2me.lang.UnsupportedOperationException;
014:
015:        import java.io.IOException;
016:        import java.io.Writer;
017:        import javolution.lang.Reusable;
018:        import javolution.text.Appendable;
019:        import javolution.text.Text;
020:
021:        /**
022:         * <p> This class allows any <code>Appendable</code> to be used as 
023:         *     a writer.</p>
024:         *
025:         * @author  <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
026:         * @version 3.8, May 8, 2006
027:         */
028:        public final class AppendableWriter extends Writer implements  Reusable {
029:
030:            /**
031:             * Holds the current appendable output or <code>null</code> if closed.
032:             */
033:            private Appendable _output;
034:
035:            /**
036:             * Creates a new appendable writer for which the appendable output 
037:             * is not set.
038:             * 
039:             * @see #setOutput(Appendable)
040:             */
041:            public AppendableWriter() {
042:            }
043:
044:            /**
045:             * Sets the appendable output being written to.
046:             * For example:[code]
047:             *     Writer writer = new AppendableWriter().setOutput(new TextBuilder());
048:             * [/code]
049:             *
050:             * @param  output the appendable written to.
051:             * @return this writer.
052:             * @throws IllegalStateException if this writer is being reused and 
053:             *         it has not been {@link #close closed} or {@link #reset reset}.
054:             */
055:            public AppendableWriter setOutput(Appendable output) {
056:                if (_output != null)
057:                    throw new IllegalStateException(
058:                            "Writer not closed or reset");
059:                _output = output;
060:                return this ;
061:            }
062:
063:            /**
064:             * Writes a single character.
065:             *
066:             * @param  c <code>char</code> the character to be written.
067:             * @throws IOException if an I/O error occurs.
068:             */
069:            public void write(char c) throws IOException {
070:                if (_output == null)
071:                    throw new IOException("Writer closed");
072:                _output.append(c);
073:            }
074:
075:            /**
076:             * Writes the 16 low-order bits of the given integer value;
077:             * the 16 high-order bits are ignored.
078:             *
079:             * @param  c the value of the character to be written.
080:             * @throws IOException if an I/O error occurs.
081:             */
082:            public void write(int c) throws IOException {
083:                if (_output == null)
084:                    throw new IOException("Writer closed");
085:                _output.append((char) c);
086:            }
087:
088:            /**
089:             * Writes a portion of an array of characters.
090:             *
091:             * @param  cbuf the array of characters.
092:             * @param  off the offset from which to start writing characters.
093:             * @param  len the number of characters to write.
094:             * @throws IOException if an I/O error occurs.
095:             */
096:            public void write(char cbuf[], int off, int len) throws IOException {
097:                if (_output == null)
098:                    throw new IOException("Writer closed");
099:                _tmpBuffer = cbuf;
100:                _output.append(_tmpBufferAsCharSequence, off, off + len);
101:                _tmpBuffer = null; // Removes temporary references.
102:            }
103:
104:            private char[] _tmpBuffer;
105:
106:            private final CharSequence _tmpBufferAsCharSequence = new CharSequence() {
107:                public int length() {
108:                    return _tmpBuffer.length;
109:                }
110:
111:                public char charAt(int index) {
112:                    return _tmpBuffer[index];
113:                }
114:
115:                public CharSequence subSequence(int start, int end) {
116:                    throw new UnsupportedOperationException();
117:                }
118:            };
119:
120:            /**
121:             * Writes a portion of a string.
122:             *
123:             * @param  str a String.
124:             * @param  off the offset from which to start writing characters.
125:             * @param  len the number of characters to write.
126:             * @throws IOException if an I/O error occurs
127:             */
128:            public void write(String str, int off, int len) throws IOException {
129:                if (_output == null)
130:                    throw new IOException("Writer closed");
131:                Object obj = str;
132:                if (obj instanceof  CharSequence) {
133:                    _output.append((CharSequence) obj);
134:                } else {
135:                    _output.append(Text.valueOf(str));
136:                }
137:            }
138:
139:            /**
140:             * Writes the specified character sequence.
141:             *
142:             * @param  csq the character sequence.
143:             * @throws IOException if an I/O error occurs
144:             */
145:            public void write(CharSequence csq) throws IOException {
146:                if (_output == null)
147:                    throw new IOException("Writer closed");
148:                _output.append(csq);
149:            }
150:
151:            /**
152:             * Flushes the stream.
153:             */
154:            public void flush() {
155:                // Do nothing (no buffer).
156:            }
157:
158:            /**
159:             * Closes and {@link #reset resets} this writer for reuse.
160:             */
161:            public void close() {
162:                if (_output != null) {
163:                    reset();
164:                }
165:            }
166:
167:            // Implements Reusable.
168:            public void reset() {
169:                _output = null;
170:                _tmpBuffer = null;
171:            }
172:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.