1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package org.codehaus.groovy.runtime;
35
36 import java.io.IOException;
37 import java.io.Writer;
38
39 /***
40 * This class codes around a silly limiation of StringWriter which doesn't allow a StringBuffer
41 * to be passed in as a constructor for some bizzare reason.
42 * So we replicate the behaviour of StringWriter here but allow a StringBuffer to be passed in.
43 *
44 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
45 * @version $Revision: 1.1 $
46 */
47 public class StringBufferWriter extends Writer {
48
49 private StringBuffer buffer;
50
51 /***
52 * Create a new string writer which will append the text to the given StringBuffer
53 */
54 public StringBufferWriter(StringBuffer buffer) {
55 this.buffer = buffer;
56 }
57
58 /***
59 * Write a single character.
60 */
61 public void write(int c) {
62 buffer.append((char) c);
63 }
64
65 /***
66 * Write a portion of an array of characters.
67 *
68 * @param text Array of characters
69 * @param offset Offset from which to start writing characters
70 * @param length Number of characters to write
71 */
72 public void write(char text[], int offset, int length) {
73 if ((offset < 0) || (offset > text.length) || (length < 0) || ((offset + length) > text.length) || ((offset + length) < 0)) {
74 throw new IndexOutOfBoundsException();
75 }
76 else if (length == 0) {
77 return;
78 }
79 buffer.append(text, offset, length);
80 }
81
82 /***
83 * Write a string.
84 */
85 public void write(String text) {
86 buffer.append(text);
87 }
88
89 /***
90 * Write a portion of a string.
91 *
92 * @param text the text to be written
93 * @param offset offset from which to start writing characters
94 * @param length Number of characters to write
95 */
96 public void write(String text, int offset, int length) {
97 buffer.append(text.substring(offset, offset + length));
98 }
99
100 /***
101 * Return the buffer's current value as a string.
102 */
103 public String toString() {
104 return buffer.toString();
105 }
106
107 /***
108 * Flush the stream.
109 */
110 public void flush() {
111 }
112
113 /***
114 * Closing a <tt>StringWriter</tt> has no effect. The methods in this
115 * class can be called after the stream has been closed without generating
116 * an <tt>IOException</tt>.
117 */
118 public void close() throws IOException {
119 }
120 }