001: /*
002: * @(#)CharArrayWriter.java 1.22 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.io;
029:
030: /**
031: * This class implements a character buffer that can be used as an Writer.
032: * The buffer automatically grows when data is written to the stream. The data
033: * can be retrieved using toCharArray() and toString().
034: *
035: * @author Herb Jellinek
036: * @version 1.15, 02/02/00
037: * @since JDK1.1
038: */
039: public class CharArrayWriter extends Writer {
040: /**
041: * The buffer where data is stored.
042: */
043: protected char buf[];
044:
045: /**
046: * The number of chars in the buffer.
047: */
048: protected int count;
049:
050: /**
051: * Creates a new CharArrayWriter.
052: */
053: public CharArrayWriter() {
054: this (32);
055: }
056:
057: /**
058: * Creates a new CharArrayWriter with the specified initial size.
059: *
060: * @param initialSize an int specifying the initial buffer size.
061: * @exception IllegalArgumentException if initialSize is negative
062: */
063: public CharArrayWriter(int initialSize) {
064: if (initialSize < 0) {
065: throw new IllegalArgumentException(
066: "Negative initial size: " + initialSize);
067: }
068: buf = new char[initialSize];
069: }
070:
071: /**
072: * Writes a character to the buffer.
073: */
074: public void write(int c) {
075: synchronized (lock) {
076: int newcount = count + 1;
077: if (newcount > buf.length) {
078: char newbuf[] = new char[Math.max(buf.length << 1,
079: newcount)];
080: System.arraycopy(buf, 0, newbuf, 0, count);
081: buf = newbuf;
082: }
083: buf[count] = (char) c;
084: count = newcount;
085: }
086: }
087:
088: /**
089: * Writes characters to the buffer.
090: * @param c the data to be written
091: * @param off the start offset in the data
092: * @param len the number of chars that are written
093: */
094: public void write(char c[], int off, int len) {
095: if ((off < 0) || (off > c.length) || (len < 0)
096: || ((off + len) > c.length) || ((off + len) < 0)) {
097: throw new IndexOutOfBoundsException();
098: } else if (len == 0) {
099: return;
100: }
101: synchronized (lock) {
102: int newcount = count + len;
103: if (newcount > buf.length) {
104: char newbuf[] = new char[Math.max(buf.length << 1,
105: newcount)];
106: System.arraycopy(buf, 0, newbuf, 0, count);
107: buf = newbuf;
108: }
109: System.arraycopy(c, off, buf, count, len);
110: count = newcount;
111: }
112: }
113:
114: /**
115: * Write a portion of a string to the buffer.
116: * @param str String to be written from
117: * @param off Offset from which to start reading characters
118: * @param len Number of characters to be written
119: */
120: public void write(String str, int off, int len) {
121: synchronized (lock) {
122: int newcount = count + len;
123: if (newcount > buf.length) {
124: char newbuf[] = new char[Math.max(buf.length << 1,
125: newcount)];
126: System.arraycopy(buf, 0, newbuf, 0, count);
127: buf = newbuf;
128: }
129: str.getChars(off, off + len, buf, count);
130: count = newcount;
131: }
132: }
133:
134: /**
135: * Writes the contents of the buffer to another character stream.
136: *
137: * @param out the output stream to write to
138: * @throws IOException If an I/O error occurs.
139: */
140: public void writeTo(Writer out) throws IOException {
141: synchronized (lock) {
142: out.write(buf, 0, count);
143: }
144: }
145:
146: /**
147: * Resets the buffer so that you can use it again without
148: * throwing away the already allocated buffer.
149: */
150: public void reset() {
151: count = 0;
152: }
153:
154: /**
155: * Returns a copy of the input data.
156: *
157: * @return an array of chars copied from the input data.
158: */
159: public char toCharArray()[] {
160: synchronized (lock) {
161: char newbuf[] = new char[count];
162: System.arraycopy(buf, 0, newbuf, 0, count);
163: return newbuf;
164: }
165: }
166:
167: /**
168: * Returns the current size of the buffer.
169: *
170: * @return an int representing the current size of the buffer.
171: */
172: public int size() {
173: return count;
174: }
175:
176: /**
177: * Converts input data to a string.
178: * @return the string.
179: */
180: public String toString() {
181: synchronized (lock) {
182: return new String(buf, 0, count);
183: }
184: }
185:
186: /**
187: * Flush the stream.
188: */
189: public void flush() {
190: }
191:
192: /**
193: * Close the stream. This method does not release the buffer, since its
194: * contents might still be required.
195: */
196: public void close() {
197: }
198:
199: }
|