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
017: * java.io.StringWriter. Code is also a near copy of {@link StringBufferWriter}.
018: * @see java.io.StringWriter
019: *
020: * @author Michiel Meeuwissen
021: * @since MMBase-1.9
022: * @version $Id: StringBuilderWriter.java,v 1.1 2006/10/14 21:57:20 michiel Exp $
023: */
024: public class StringBuilderWriter extends Writer {
025:
026: protected StringBuilder buffer;
027:
028: /**
029: * Create a new StringBufferWriter
030: * @param buffer The StringBuffer to use
031: * @throws java.lang.NullPointerException if <code>buffer</code> is null.
032: */
033: public StringBuilderWriter(StringBuilder buffer) {
034: if (buffer == null)
035: throw new NullPointerException("Buffer may not be null");
036: this .buffer = buffer;
037: lock = buffer;
038: }
039:
040: /**
041: * Write a single character.
042: */
043: public void write(int c) {
044: buffer.append((char) c);
045: }
046:
047: /**
048: * Write a portion of an array of characters.
049: *
050: * @param charArray Array of characters
051: * @param offset Offset from which to start writing characters
052: * @param length Number of characters to write
053: */
054: public void write(char charArray[], int offset, int length) {
055: if ((offset < 0) || (offset > charArray.length) || (length < 0)
056: || ((offset + length) > charArray.length)
057: || ((offset + length) < 0)) {
058: throw new IndexOutOfBoundsException();
059: } else if (length == 0) {
060: return;
061: }
062: buffer.append(charArray, offset, length);
063: }
064:
065: /**
066: * Write a string.
067: */
068: public void write(String str) {
069: buffer.append(str);
070: }
071:
072: /**
073: * Write a portion of a string.
074: *
075: * @param str String to be written
076: * @param offset Offset from which to start writing characters
077: * @param length Number of characters to write
078: */
079: public void write(String str, int offset, int length) {
080: buffer.append(str.substring(offset, offset + length));
081: }
082:
083: /**
084: * Return the buffer's current value as a string.
085: */
086: public String toString() {
087: return buffer.toString();
088: }
089:
090: /**
091: * Return the string buffer itself.
092: *
093: * @return StringBuilder holding the current buffer value.
094: */
095: public StringBuilder getBuffer() {
096: return buffer;
097: }
098:
099: /**
100: * Flush the stream.
101: */
102: public void flush() {
103: }
104:
105: /**
106: * Closing a <tt>StringBuilderWriter</tt> has no effect. The methods in this
107: * class can be called after the stream has been closed without generating
108: * an <tt>IOException</tt>.
109: */
110: public void close() throws IOException {
111: }
112:
113: }
|