Source Code Cross Referenced for StringWriter.java in  » Apache-Harmony-Java-SE » java-package » 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 » Apache Harmony Java SE » java package » java.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package java.io;
019:
020:        /**
021:         * StringWriter is an class for writing Character Streams to a StringBuffer. The
022:         * characters written can then be returned as a String. This is used for
023:         * capturing output sent to a Writer by substituting a StringWriter.
024:         * 
025:         * @see StringReader
026:         */
027:        public class StringWriter extends Writer {
028:
029:            private StringBuffer buf;
030:
031:            /**
032:             * Constructs a new StringWriter which has a StringBuffer allocated with the
033:             * default size of 16 characters. The StringBuffer is also the
034:             * <code>lock</code> used to synchronize access to this Writer.
035:             */
036:            public StringWriter() {
037:                super ();
038:                buf = new StringBuffer(16);
039:                lock = buf;
040:            }
041:
042:            /**
043:             * Constructs a new StringWriter which has a StringBuffer allocated with the
044:             * size of <code>initialSize</code> characters. The StringBuffer is also
045:             * the <code>lock</code> used to synchronize access to this Writer.
046:             * 
047:             * @param initialSize
048:             *            the intial number of characters
049:             */
050:            public StringWriter(int initialSize) {
051:                if (initialSize < 0) {
052:                    throw new IllegalArgumentException();
053:                }
054:                buf = new StringBuffer(initialSize);
055:                lock = buf;
056:            }
057:
058:            /**
059:             * Close this Writer. This is the concrete implementation required. This
060:             * particular implementation does nothing.
061:             * 
062:             * @throws IOException
063:             *             If an IO error occurs closing this StringWriter.
064:             */
065:            @Override
066:            public void close() throws IOException {
067:                /* empty */
068:            }
069:
070:            /**
071:             * Flush this Writer. This is the concrete implementation required. This
072:             * particular implementation does nothing.
073:             */
074:            @Override
075:            public void flush() {
076:                /* empty */
077:            }
078:
079:            /**
080:             * Answer the contents of this StringWriter as a StringBuffer. Any changes
081:             * made to the StringBuffer by the receiver or the caller are reflected in
082:             * this StringWriter.
083:             * 
084:             * @return this StringWriters local StringBuffer.
085:             */
086:            public StringBuffer getBuffer() {
087:                return buf;
088:            }
089:
090:            /**
091:             * Answer the contents of this StringWriter as a String. Any changes made to
092:             * the StringBuffer by the receiver after returning will not be reflected in
093:             * the String returned to the caller.
094:             * 
095:             * @return this StringWriters current contents as a String.
096:             */
097:            @Override
098:            public String toString() {
099:                return buf.toString();
100:            }
101:
102:            /**
103:             * Writes <code>count</code> characters starting at <code>offset</code>
104:             * in <code>cbuf</code> to this StringWriter.
105:             * 
106:             * @param cbuf
107:             *            the non-null array containing characters to write.
108:             * @param offset
109:             *            offset in buf to retrieve characters
110:             * @param count
111:             *            maximum number of characters to write
112:             * 
113:             * @throws ArrayIndexOutOfBoundsException
114:             *             If offset or count are outside of bounds.
115:             */
116:            @Override
117:            public void write(char[] cbuf, int offset, int count) {
118:                // avoid int overflow
119:                if (offset < 0 || offset > cbuf.length || count < 0
120:                        || count > cbuf.length - offset) {
121:                    throw new IndexOutOfBoundsException();
122:                }
123:                if (count == 0) {
124:                    return;
125:                }
126:                buf.append(cbuf, offset, count);
127:            }
128:
129:            /**
130:             * Writes the specified character <code>oneChar</code> to this
131:             * StringWriter. This implementation writes the low order two bytes to the
132:             * Stream.
133:             * 
134:             * @param oneChar
135:             *            The character to write
136:             */
137:            @Override
138:            public void write(int oneChar) {
139:                buf.append((char) oneChar);
140:            }
141:
142:            /**
143:             * Writes the characters from the String <code>str</code> to this
144:             * StringWriter.
145:             * 
146:             * @param str
147:             *            the non-null String containing the characters to write.
148:             */
149:            @Override
150:            public void write(String str) {
151:                buf.append(str);
152:            }
153:
154:            /**
155:             * Writes <code>count</code> number of characters starting at
156:             * <code>offset</code> from the String <code>str</code> to this
157:             * StringWriter.
158:             * 
159:             * @param str
160:             *            the non-null String containing the characters to write.
161:             * @param offset
162:             *            the starting point to retrieve characters.
163:             * @param count
164:             *            the number of characters to retrieve and write.
165:             * 
166:             * @throws ArrayIndexOutOfBoundsException
167:             *             If offset or count are outside of bounds.
168:             */
169:            @Override
170:            public void write(String str, int offset, int count) {
171:                String sub = str.substring(offset, offset + count);
172:                buf.append(sub);
173:            }
174:
175:            /**
176:             * Append a char <code>c</code>to the StringWriter. The
177:             * StringWriter.append(<code>c</code>) works the same way as
178:             * StringWriter.write(<code>c</code>).
179:             * 
180:             * @param c
181:             *            The character appended to the StringWriter.
182:             * @return The StringWriter.
183:             */
184:            @Override
185:            public StringWriter append(char c) {
186:                write(c);
187:                return this ;
188:            }
189:
190:            /**
191:             * Append a CharSequence <code>csq</code> to the StringWriter. The
192:             * StringWriter.append(<code>csq</code>) works the same way as
193:             * StringWriter.write(<code>csq</code>.toString()). If <code>csq</code>
194:             * is null, then "null" will be substituted for <code>csq</code>.
195:             * 
196:             * @param csq
197:             *            The CharSequence appended to the StringWriter.
198:             * @return The StringWriter
199:             */
200:            @Override
201:            public StringWriter append(CharSequence csq) {
202:                if (null == csq) {
203:                    write(TOKEN_NULL);
204:                } else {
205:                    write(csq.toString());
206:                }
207:                return this ;
208:            }
209:
210:            /**
211:             * Append a subsequence of a CharSequence <code>csq</code> to the
212:             * StringWriter. The first char and the last char of the subsequnce is
213:             * specified by the parameter <code>start</code> and <code>end</code>.
214:             * The StringWriter.append(<code>csq</code>) works the same way as
215:             * StringWriter.write(<code>csq</code>.subSequence(<code>start</code>,<code>end</code>).toString).If
216:             * <code>csq</code> is null, then "null" will be substituted for
217:             * <code>csq</code>. s
218:             * 
219:             * @param csq
220:             *            The CharSequence appended to the StringWriter.
221:             * @param start
222:             *            The index of the first char in the CharSequence appended to
223:             *            the StringWriter.
224:             * @param end
225:             *            The index of the char after the last one in the CharSequence
226:             *            appended to the StringWriter.
227:             * @return The StringWriter.
228:             * @throws IndexOutOfBoundsException
229:             *             If start is less than end, end is greater than the length of
230:             *             the CharSequence, or start or end is negative.
231:             */
232:            @Override
233:            public StringWriter append(CharSequence csq, int start, int end) {
234:                if (null == csq) {
235:                    csq = TOKEN_NULL;
236:                }
237:                String output = csq.subSequence(start, end).toString();
238:                write(output, 0, output.length());
239:                return this;
240:            }
241:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.