KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > StringPrintWriter


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang;
17
18 import java.io.PrintWriter JavaDoc;
19 import java.io.StringWriter JavaDoc;
20
21 /**
22  * <p>A PrintWriter that maintains a String as its backing store.</p>
23  *
24  * <p>Usage:
25  * <pre>
26  * StringPrintWriter out = new StringPrintWriter();
27  * printTo(out);
28  * System.out.println( out.getString() );
29  * </pre>
30  * </p>
31  *
32  * @author Alex Chaffee
33  * @author Scott Stanchfield
34  * @author Gary D. Gregory
35  * @since 2.0
36  */

37 class StringPrintWriter extends PrintWriter JavaDoc {
38     
39     /**
40      * Constructs a new instance.
41      */

42     public StringPrintWriter() {
43         super(new StringWriter JavaDoc());
44     }
45
46     /**
47      * Constructs a new instance using the specified initial string-buffer
48      * size.
49      *
50      * @param initialSize an int specifying the initial size of the buffer.
51      */

52     public StringPrintWriter(int initialSize) {
53         super(new StringWriter JavaDoc(initialSize));
54     }
55
56     /**
57      * <p>Since toString() returns information *about* this object, we
58      * want a separate method to extract just the contents of the
59      * internal buffer as a String.</p>
60      *
61      * @return the contents of the internal string buffer
62      */

63     public String JavaDoc getString() {
64         flush();
65         return ((StringWriter JavaDoc) this.out).toString();
66     }
67     
68 }
69
70
Popular Tags