001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010:
011: package org.mmbase.util;
012:
013: import java.io.*;
014:
015: /**
016: * Oddly enough, Java does not provide this itself. Code is nearly identical to java.io.StringWriter.
017: * @see java.io.StringWriter
018: *
019: * @author Michiel Meeuwissen
020: * @since MMBase-1.7
021: * @version $Id: StringBufferWriter.java,v 1.4 2005/10/05 10:09:05 michiel Exp $
022: */
023: public class StringBufferWriter extends Writer {
024:
025: protected StringBuffer buffer;
026:
027: /**
028: * Create a new StringBufferWriter
029: * @param buffer The StringBuffer to use
030: * @throws java.lang.NullPointerException if <code>buffer</code> is null.
031: */
032: public StringBufferWriter(StringBuffer buffer) {
033: if (buffer == null)
034: throw new NullPointerException("Buffer may not be null");
035: this .buffer = buffer;
036: lock = buffer;
037: }
038:
039: /**
040: * Write a single character.
041: */
042: public void write(int c) {
043: buffer.append((char) c);
044: }
045:
046: /**
047: * Write a portion of an array of characters.
048: *
049: * @param charArray Array of characters
050: * @param offset Offset from which to start writing characters
051: * @param length Number of characters to write
052: */
053: public void write(char charArray[], int offset, int length) {
054: if ((offset < 0) || (offset > charArray.length) || (length < 0)
055: || ((offset + length) > charArray.length)
056: || ((offset + length) < 0)) {
057: throw new IndexOutOfBoundsException();
058: } else if (length == 0) {
059: return;
060: }
061: buffer.append(charArray, offset, length);
062: }
063:
064: /**
065: * Write a string.
066: */
067: public void write(String str) {
068: buffer.append(str);
069: }
070:
071: /**
072: * Write a portion of a string.
073: *
074: * @param str String to be written
075: * @param offset Offset from which to start writing characters
076: * @param length Number of characters to write
077: */
078: public void write(String str, int offset, int length) {
079: buffer.append(str.substring(offset, offset + length));
080: }
081:
082: /**
083: * Return the buffer's current value as a string.
084: */
085: public String toString() {
086: return buffer.toString();
087: }
088:
089: /**
090: * Return the string buffer itself.
091: *
092: * @return StringBuffer holding the current buffer value.
093: */
094: public StringBuffer getBuffer() {
095: return buffer;
096: }
097:
098: /**
099: * Flush the stream.
100: */
101: public void flush() {
102: }
103:
104: /**
105: * Closing a <tt>StringBufferWriter</tt> has no effect. The methods in this
106: * class can be called after the stream has been closed without generating
107: * an <tt>IOException</tt>.
108: */
109: public void close() throws IOException {
110: }
111:
112: }
|