CharArrayWriterpublic class CharArrayWriter extends Writer This class implements a character buffer that can be used as an Writer.
The buffer automatically grows when data is written to the stream. The data
can be retrieved using toCharArray() and toString().
Note: Invoking close() on this class has no effect, and methods
of this class can be called after the stream has closed
without generating an IOException. |
Fields Summary |
---|
protected char[] | bufThe buffer where data is stored. | protected int | countThe number of chars in the buffer. |
Constructors Summary |
---|
public CharArrayWriter()Creates a new CharArrayWriter.
this(32);
| public CharArrayWriter(int initialSize)Creates a new CharArrayWriter with the specified initial size.
if (initialSize < 0) {
throw new IllegalArgumentException("Negative initial size: "
+ initialSize);
}
buf = new char[initialSize];
|
Methods Summary |
---|
public java.io.CharArrayWriter | append(java.lang.CharSequence csq)Appends the specified character sequence to this writer.
An invocation of this method of the form out.append(csq)
behaves in exactly the same way as the invocation
out.write(csq.toString())
Depending on the specification of toString for the
character sequence csq, the entire sequence may not be
appended. For instance, invoking the toString method of a
character buffer will return a subsequence whose content depends upon
the buffer's position and limit.
String s = (csq == null ? "null" : csq.toString());
write(s, 0, s.length());
return this;
| public java.io.CharArrayWriter | append(java.lang.CharSequence csq, int start, int end)Appends a subsequence of the specified character sequence to this writer.
An invocation of this method of the form out.append(csq, start,
end) when csq is not null, behaves in
exactly the same way as the invocation
out.write(csq.subSequence(start, end).toString())
String s = (csq == null ? "null" : csq).subSequence(start, end).toString();
write(s, 0, s.length());
return this;
| public java.io.CharArrayWriter | append(char c)Appends the specified character to this writer.
An invocation of this method of the form out.append(c)
behaves in exactly the same way as the invocation
out.write(c)
write(c);
return this;
| public void | close()Close the stream. This method does not release the buffer, since its
contents might still be required. Note: Invoking this method in this class
will have no effect.
| public void | flush()Flush the stream.
| public void | reset()Resets the buffer so that you can use it again without
throwing away the already allocated buffer.
count = 0;
| public int | size()Returns the current size of the buffer.
return count;
| public char[] | toCharArray()Returns a copy of the input data.
synchronized (lock) {
char newbuf[] = new char[count];
System.arraycopy(buf, 0, newbuf, 0, count);
return newbuf;
}
| public java.lang.String | toString()Converts input data to a string.
synchronized (lock) {
return new String(buf, 0, count);
}
| public void | write(int c)Writes a character to the buffer.
synchronized (lock) {
int newcount = count + 1;
if (newcount > buf.length) {
char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
System.arraycopy(buf, 0, newbuf, 0, count);
buf = newbuf;
}
buf[count] = (char)c;
count = newcount;
}
| public void | write(char[] c, int off, int len)Writes characters to the buffer.
if ((off < 0) || (off > c.length) || (len < 0) ||
((off + len) > c.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return;
}
synchronized (lock) {
int newcount = count + len;
if (newcount > buf.length) {
char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
System.arraycopy(buf, 0, newbuf, 0, count);
buf = newbuf;
}
System.arraycopy(c, off, buf, count, len);
count = newcount;
}
| public void | write(java.lang.String str, int off, int len)Write a portion of a string to the buffer.
synchronized (lock) {
int newcount = count + len;
if (newcount > buf.length) {
char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
System.arraycopy(buf, 0, newbuf, 0, count);
buf = newbuf;
}
str.getChars(off, off + len, buf, count);
count = newcount;
}
| public void | writeTo(java.io.Writer out)Writes the contents of the buffer to another character stream.
synchronized (lock) {
out.write(buf, 0, count);
}
|
|