01: /*
02: * This file is part of "SnipSnap Radeox Rendering Engine".
03: *
04: * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
05: * All Rights Reserved.
06: *
07: * Please visit http://radeox.org/ for updates and contact.
08: *
09: * --LICENSE NOTICE--
10: * Licensed under the Apache License, Version 2.0 (the "License");
11: * you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software
17: * distributed under the License is distributed on an "AS IS" BASIS,
18: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19: * See the License for the specific language governing permissions and
20: * limitations under the License.
21: * --LICENSE NOTICE--
22: */
23:
24: package org.radeox.util;
25:
26: import java.io.IOException;
27: import java.io.Writer;
28:
29: /**
30: * The same as StringWriter, but takes an existing StringBuffer in its
31: * constructor.
32: *
33: * @author Stephan J. Schmidt
34: * @version $Id: StringBufferWriter.java 7707 2006-04-12 17:30:19Z
35: * ian@caret.cam.ac.uk $
36: */
37:
38: public class StringBufferWriter extends Writer {
39:
40: private StringBuffer buffer;
41:
42: private boolean closed = false;
43:
44: public StringBufferWriter(StringBuffer buffer) {
45: this .buffer = buffer;
46: this .lock = buffer;
47: }
48:
49: public StringBufferWriter() {
50: this .buffer = new StringBuffer();
51: this .lock = buffer;
52: }
53:
54: public StringBufferWriter(int initialSize) {
55: if (initialSize < 0) {
56: throw new IllegalArgumentException("Negative buffer size");
57: }
58: buffer = new StringBuffer(initialSize);
59: lock = buffer;
60: }
61:
62: public void write(int c) {
63: buffer.append((char) c);
64: }
65:
66: public void write(char cbuf[], int off, int len) {
67: if ((off < 0) || (off > cbuf.length) || (len < 0)
68: || ((off + len) > cbuf.length) || ((off + len) < 0)) {
69: throw new IndexOutOfBoundsException();
70: } else if (len == 0) {
71: return;
72: }
73: buffer.append(cbuf, off, len);
74: }
75:
76: public void write(String str) {
77: buffer.append(str);
78: }
79:
80: public void write(String str, int off, int len) {
81: buffer.append(str.substring(off, off + len));
82: }
83:
84: public String toString() {
85: return buffer.toString();
86: }
87:
88: public StringBuffer getBuffer() {
89: return buffer;
90: }
91:
92: public void flush() {
93: }
94:
95: public void close() throws IOException {
96: closed = true;
97: }
98:
99: }
|