StringWriter.java in  » JDK-Core » io-nio » java » io » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. JDK Core
2. JDK Modules
3. JDK Modules com.sun
4. JDK Modules com.sun.java
5. JDK Modules Platform
6. JDK Modules sun
7. Open Source Graphic Library
8. Open Source IDE Eclipse
9. Open Source J2EE
10. Open Source JBOSS
11. Open Source JDBC Driver
12. Open Source Library
13. Open Source Library Database
14. Open Source Net
15. Science
16. Sevlet Container
17. SUN GlassFish
18. Swing Library
19. Web Services apache cxf 2.0.1
20. Web Services AXIS2
21. XML
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java Source Code / Java Documentation » JDK Core » io nio » java.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 1996-2004 Sun Microsystems, Inc.  All Rights Reserved.
003:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004:         *
005:         * This code is free software; you can redistribute it and/or modify it
006:         * under the terms of the GNU General Public License version 2 only, as
007:         * published by the Free Software Foundation.  Sun designates this
008:         * particular file as subject to the "Classpath" exception as provided
009:         * by Sun in the LICENSE file that accompanied this code.
010:         *
011:         * This code is distributed in the hope that it will be useful, but WITHOUT
012:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013:         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014:         * version 2 for more details (a copy is included in the LICENSE file that
015:         * accompanied this code).
016:         *
017:         * You should have received a copy of the GNU General Public License version
018:         * 2 along with this work; if not, write to the Free Software Foundation,
019:         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020:         *
021:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022:         * CA 95054 USA or visit www.sun.com if you need additional information or
023:         * have any questions.
024:         */
025:
026:        package java.io;
027:
028:        /**
029:         * A character stream that collects its output in a string buffer, which can
030:         * then be used to construct a string.
031:         * <p>
032:         * Closing a <tt>StringWriter</tt> has no effect. The methods in this class
033:         * can be called after the stream has been closed without generating an
034:         * <tt>IOException</tt>.
035:         *
036:         * @version 	1.32, 07/05/05
037:         * @author	Mark Reinhold
038:         * @since	JDK1.1
039:         */
040:
041:        public class StringWriter extends Writer {
042:
043:            private StringBuffer buf;
044:
045:            /**
046:             * Create a new string writer using the default initial string-buffer
047:             * size.
048:             */
049:            public StringWriter() {
050:                buf = new StringBuffer();
051:                lock = buf;
052:            }
053:
054:            /**
055:             * Create a new string writer using the specified initial string-buffer
056:             * size.
057:             *
058:             * @param initialSize
059:             *        The number of <tt>char</tt> values that will fit into this buffer
060:             *        before it is automatically expanded
061:             *
062:             * @throws IllegalArgumentException
063:             *         If <tt>initialSize</tt> is negative
064:             */
065:            public StringWriter(int initialSize) {
066:                if (initialSize < 0) {
067:                    throw new IllegalArgumentException("Negative buffer size");
068:                }
069:                buf = new StringBuffer(initialSize);
070:                lock = buf;
071:            }
072:
073:            /**
074:             * Write a single character.
075:             */
076:            public void write(int c) {
077:                buf.append((char) c);
078:            }
079:
080:            /**
081:             * Write a portion of an array of characters.
082:             *
083:             * @param  cbuf  Array of characters
084:             * @param  off   Offset from which to start writing characters
085:             * @param  len   Number of characters to write
086:             */
087:            public void write(char cbuf[], int off, int len) {
088:                if ((off < 0) || (off > cbuf.length) || (len < 0)
089:                        || ((off + len) > cbuf.length) || ((off + len) < 0)) {
090:                    throw new IndexOutOfBoundsException();
091:                } else if (len == 0) {
092:                    return;
093:                }
094:                buf.append(cbuf, off, len);
095:            }
096:
097:            /**
098:             * Write a string.
099:             */
100:            public void write(String str) {
101:                buf.append(str);
102:            }
103:
104:            /**
105:             * Write a portion of a string.
106:             *
107:             * @param  str  String to be written
108:             * @param  off  Offset from which to start writing characters
109:             * @param  len  Number of characters to write
110:             */
111:            public void write(String str, int off, int len) {
112:                buf.append(str.substring(off, off + len));
113:            }
114:
115:            /**
116:             * Appends the specified character sequence to this writer.
117:             *
118:             * <p> An invocation of this method of the form <tt>out.append(csq)</tt>
119:             * behaves in exactly the same way as the invocation
120:             *
121:             * <pre>
122:             *     out.write(csq.toString()) </pre>
123:             *
124:             * <p> Depending on the specification of <tt>toString</tt> for the
125:             * character sequence <tt>csq</tt>, the entire sequence may not be
126:             * appended. For instance, invoking the <tt>toString</tt> method of a
127:             * character buffer will return a subsequence whose content depends upon
128:             * the buffer's position and limit.
129:             *
130:             * @param  csq
131:             *         The character sequence to append.  If <tt>csq</tt> is
132:             *         <tt>null</tt>, then the four characters <tt>"null"</tt> are
133:             *         appended to this writer.
134:             *
135:             * @return  This writer
136:             *
137:             * @since  1.5
138:             */
139:            public StringWriter append(CharSequence csq) {
140:                if (csq == null)
141:                    write("null");
142:                else
143:                    write(csq.toString());
144:                return this ;
145:            }
146:
147:            /**
148:             * Appends a subsequence of the specified character sequence to this writer.
149:             *
150:             * <p> An invocation of this method of the form <tt>out.append(csq, start,
151:             * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in
152:             * exactly the same way as the invocation
153:             *
154:             * <pre>
155:             *     out.write(csq.subSequence(start, end).toString()) </pre>
156:             *
157:             * @param  csq
158:             *         The character sequence from which a subsequence will be
159:             *         appended.  If <tt>csq</tt> is <tt>null</tt>, then characters
160:             *         will be appended as if <tt>csq</tt> contained the four
161:             *         characters <tt>"null"</tt>.
162:             *
163:             * @param  start
164:             *         The index of the first character in the subsequence
165:             *
166:             * @param  end
167:             *         The index of the character following the last character in the
168:             *         subsequence
169:             *
170:             * @return  This writer
171:             *
172:             * @throws  IndexOutOfBoundsException
173:             *          If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
174:             *          is greater than <tt>end</tt>, or <tt>end</tt> is greater than
175:             *          <tt>csq.length()</tt>
176:             *
177:             * @since  1.5
178:             */
179:            public StringWriter append(CharSequence csq, int start, int end) {
180:                CharSequence cs = (csq == null ? "null" : csq);
181:                write(cs.subSequence(start, end).toString());
182:                return this ;
183:            }
184:
185:            /**
186:             * Appends the specified character to this writer. 
187:             *
188:             * <p> An invocation of this method of the form <tt>out.append(c)</tt>
189:             * behaves in exactly the same way as the invocation
190:             *
191:             * <pre>
192:             *     out.write(c) </pre>
193:             *
194:             * @param  c
195:             *         The 16-bit character to append
196:             *
197:             * @return  This writer
198:             *
199:             * @since 1.5
200:             */
201:            public StringWriter append(char c) {
202:                write(c);
203:                return this ;
204:            }
205:
206:            /**
207:             * Return the buffer's current value as a string.
208:             */
209:            public String toString() {
210:                return buf.toString();
211:            }
212:
213:            /**
214:             * Return the string buffer itself.
215:             *
216:             * @return StringBuffer holding the current buffer value.
217:             */
218:            public StringBuffer getBuffer() {
219:                return buf;
220:            }
221:
222:            /**
223:             * Flush the stream.
224:             */
225:            public void flush() {
226:            }
227:
228:            /**
229:             * Closing a <tt>StringWriter</tt> has no effect. The methods in this
230:             * class can be called after the stream has been closed without generating
231:             * an <tt>IOException</tt>.
232:             */
233:            public void close() throws IOException {
234:            }
235:
236:        }
w___w__w_._j___a__v__a_2___s__.c__o__m___
Home | Contact Us
Copyright 2003 - 07 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.