1 // CharArrayWriter.java - Character array output stream.
3 /* Copyright (C) 1998, 1999 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
14 * @author Tom Tromey <tromey@cygnus.com>
15 * @date September 25, 1998
18 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
19 * "The Java Language Specification", ISBN 0-201-63451-1
20 * Status: Complete to 1.1.
23 public class CharArrayWriter extends Writer
25 public CharArrayWriter ()
30 public CharArrayWriter (int size)
38 // JCL says this does nothing. This seems to violate the Writer
39 // contract, in that other methods should still throw and
40 // IOException after a close. Still, we just follow JCL.
47 public synchronized void reset ()
57 public char[] toCharArray ()
59 char[] nc = new char[count];
60 System.arraycopy(buf, 0, nc, 0, count);
64 public String toString ()
66 return new String (buf, 0, count);
69 public void write (int oneChar)
74 buf[count++] = (char) oneChar;
78 public void write (char[] buffer, int offset, int len)
84 System.arraycopy(buffer, offset, buf, count, len);
89 public void write (String str, int offset, int len)
95 str.getChars(offset, offset + len, buf, count);
100 public void writeTo (Writer out) throws IOException
102 out.write(buf, 0, count);
105 private final void resize (int len)
107 if (count + len >= buf.length)
109 int newlen = buf.length * 2;
110 if (count + len > newlen)
111 newlen = count + len;
112 char[] newbuf = new char[newlen];
113 System.arraycopy(buf, 0, newbuf, 0, count);
118 // The character buffer.
119 protected char[] buf;
120 // Number of valid characters in buffer.