KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > StringBufferWriter


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.util;
12
13 import java.io.*;
14
15 /**
16  * Oddly enough, Java does not provide this itself. Code is nearly identical to java.io.StringWriter.
17  * @see java.io.StringWriter
18  *
19  * @author Michiel Meeuwissen
20  * @since MMBase-1.7
21  * @version $Id: StringBufferWriter.java,v 1.4 2005/10/05 10:09:05 michiel Exp $
22  */

23 public class StringBufferWriter extends Writer {
24
25     protected StringBuffer JavaDoc buffer;
26
27     /**
28      * Create a new StringBufferWriter
29      * @param buffer The StringBuffer to use
30      * @throws java.lang.NullPointerException if <code>buffer</code> is null.
31      */

32     public StringBufferWriter(StringBuffer JavaDoc buffer) {
33         if (buffer == null) throw new NullPointerException JavaDoc("Buffer may not be null");
34         this.buffer = buffer;
35         lock = buffer;
36     }
37
38     /**
39      * Write a single character.
40      */

41     public void write(int c) {
42         buffer.append((char) c);
43     }
44
45     /**
46      * Write a portion of an array of characters.
47      *
48      * @param charArray Array of characters
49      * @param offset Offset from which to start writing characters
50      * @param length Number of characters to write
51      */

52     public void write(char charArray[], int offset, int length) {
53         if ((offset < 0) || (offset > charArray.length) || (length < 0) ||
54             ((offset + length) > charArray.length) || ((offset + length) < 0)) {
55             throw new IndexOutOfBoundsException JavaDoc();
56         } else if (length == 0) {
57             return;
58         }
59         buffer.append(charArray, offset, length);
60     }
61
62     /**
63      * Write a string.
64      */

65     public void write(String JavaDoc str) {
66         buffer.append(str);
67     }
68
69     /**
70      * Write a portion of a string.
71      *
72      * @param str String to be written
73      * @param offset Offset from which to start writing characters
74      * @param length Number of characters to write
75      */

76     public void write(String JavaDoc str, int offset, int length) {
77         buffer.append(str.substring(offset, offset + length));
78     }
79
80     /**
81      * Return the buffer's current value as a string.
82      */

83     public String JavaDoc toString() {
84         return buffer.toString();
85     }
86
87     /**
88      * Return the string buffer itself.
89      *
90      * @return StringBuffer holding the current buffer value.
91      */

92     public StringBuffer JavaDoc getBuffer() {
93         return buffer;
94     }
95
96     /**
97      * Flush the stream.
98      */

99     public void flush() {
100     }
101
102     /**
103      * Closing a <tt>StringBufferWriter</tt> has no effect. The methods in this
104      * class can be called after the stream has been closed without generating
105      * an <tt>IOException</tt>.
106      */

107     public void close() throws IOException {
108     }
109
110 }
111
Popular Tags