001: package com.opensymphony.module.sitemesh.util;
002:
003: import java.io.Writer;
004: import java.io.IOException;
005:
006: /**
007: * Unsynced version of the JDK's CharArrayWriter
008: */
009: public class CharArrayWriter extends Writer {
010: /**
011: * The buffer where data is stored.
012: */
013: protected char buf[];
014:
015: /**
016: * The number of chars in the buffer.
017: */
018: protected int count;
019:
020: /**
021: * Creates a new CharArrayWriter.
022: */
023: public CharArrayWriter() {
024: this (32);
025: }
026:
027: /**
028: * Creates a new CharArrayWriter with the specified initial size.
029: *
030: * @param initialSize an int specifying the initial buffer size.
031: * @exception IllegalArgumentException if initialSize is negative
032: */
033: public CharArrayWriter(int initialSize) {
034: if (initialSize < 0) {
035: throw new IllegalArgumentException(
036: "Negative initial size: " + initialSize);
037: }
038: buf = new char[initialSize];
039: }
040:
041: /**
042: * Writes a character to the buffer.
043: */
044: public void write(int c) {
045: int newcount = count + 1;
046: if (newcount > buf.length) {
047: char newbuf[] = new char[Math
048: .max(buf.length << 1, newcount)];
049: System.arraycopy(buf, 0, newbuf, 0, count);
050: buf = newbuf;
051: }
052: buf[count] = (char) c;
053: count = newcount;
054: }
055:
056: /**
057: * Writes characters to the buffer.
058: * @param c the data to be written
059: * @param off the start offset in the data
060: * @param len the number of chars that are written
061: */
062: public void write(char c[], int off, int len) {
063: if ((off < 0) || (off > c.length) || (len < 0)
064: || ((off + len) > c.length) || ((off + len) < 0)) {
065: throw new IndexOutOfBoundsException();
066: } else if (len == 0) {
067: return;
068: }
069: int newcount = count + len;
070: if (newcount > buf.length) {
071: char newbuf[] = new char[Math
072: .max(buf.length << 1, newcount)];
073: System.arraycopy(buf, 0, newbuf, 0, count);
074: buf = newbuf;
075: }
076: System.arraycopy(c, off, buf, count, len);
077: count = newcount;
078: }
079:
080: /**
081: * Write a portion of a string to the buffer.
082: * @param str String to be written from
083: * @param off Offset from which to start reading characters
084: * @param len Number of characters to be written
085: */
086: public void write(String str, int off, int len) {
087: int newcount = count + len;
088: if (newcount > buf.length) {
089: char newbuf[] = new char[Math
090: .max(buf.length << 1, newcount)];
091: System.arraycopy(buf, 0, newbuf, 0, count);
092: buf = newbuf;
093: }
094: str.getChars(off, off + len, buf, count);
095: count = newcount;
096: }
097:
098: /**
099: * Writes the contents of the buffer to another character stream.
100: *
101: * @param out the output stream to write to
102: * @throws java.io.IOException If an I/O error occurs.
103: */
104: public void writeTo(Writer out) throws IOException {
105: out.write(buf, 0, count);
106: }
107:
108: /**
109: * Resets the buffer so that you can use it again without
110: * throwing away the already allocated buffer.
111: */
112: public void reset() {
113: count = 0;
114: }
115:
116: /**
117: * Returns a copy of the input data.
118: *
119: * @return an array of chars copied from the input data.
120: */
121: public char toCharArray()[] {
122: char newbuf[] = new char[count];
123: System.arraycopy(buf, 0, newbuf, 0, count);
124: return newbuf;
125: }
126:
127: /**
128: * Returns the current size of the buffer.
129: *
130: * @return an int representing the current size of the buffer.
131: */
132: public int size() {
133: return count;
134: }
135:
136: /**
137: * Converts input data to a string.
138: * @return the string.
139: */
140: public String toString() {
141: return new String(buf, 0, count);
142: }
143:
144: /**
145: * Flush the stream.
146: */
147: public void flush() {
148: }
149:
150: /**
151: * Close the stream. This method does not release the buffer, since its
152: * contents might still be required.
153: */
154: public void close() {
155: }
156:
157: }
|