FileDocCategorySizeDatePackage
CharArrayWriter.javaAPI DocJava SE 6 API7350Tue Jun 10 00:25:32 BST 2008java.io

CharArrayWriter

public 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.

author
Herb Jellinek
version
1.25, 11/17/05
since
JDK1.1

Fields Summary
protected char[]
buf
The buffer where data is stored.
protected int
count
The 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.

param
initialSize an int specifying the initial buffer size.
exception
IllegalArgumentException if initialSize is negative

        if (initialSize < 0) {
            throw new IllegalArgumentException("Negative initial size: "
					       + initialSize);
        }
	buf = new char[initialSize];
    
Methods Summary
public java.io.CharArrayWriterappend(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.

param
csq The character sequence to append. If csq is null, then the four characters "null" are appended to this writer.
return
This writer
since
1.5

	String s = (csq == null ? "null" : csq.toString());
	write(s, 0, s.length());
	return this;
    
public java.io.CharArrayWriterappend(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()) 

param
csq The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null".
param
start The index of the first character in the subsequence
param
end The index of the character following the last character in the subsequence
return
This writer
throws
IndexOutOfBoundsException If start or end are negative, start is greater than end, or end is greater than csq.length()
since
1.5

	String s = (csq == null ? "null" : csq).subSequence(start, end).toString();
	write(s, 0, s.length());
	return this;
    
public java.io.CharArrayWriterappend(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) 

param
c The 16-bit character to append
return
This writer
since
1.5

	write(c);
	return this;
    
public voidclose()
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 voidflush()
Flush the stream.

 
public voidreset()
Resets the buffer so that you can use it again without throwing away the already allocated buffer.

	count = 0;
    
public intsize()
Returns the current size of the buffer.

return
an int representing the current size of the buffer.

	return count;
    
public char[]toCharArray()
Returns a copy of the input data.

return
an array of chars copied from the input data.

	synchronized (lock) {
            return Arrays.copyOf(buf, count);
	}
    
public java.lang.StringtoString()
Converts input data to a string.

return
the string.

	synchronized (lock) {
	    return new String(buf, 0, count);
	}
    
public voidwrite(int c)
Writes a character to the buffer.

	synchronized (lock) {
	    int newcount = count + 1;
	    if (newcount > buf.length) {
                buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
	    }
	    buf[count] = (char)c;
	    count = newcount;
	}
    
public voidwrite(char[] c, int off, int len)
Writes characters to the buffer.

param
c the data to be written
param
off the start offset in the data
param
len the number of chars that are written

	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) {
                buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
	    }
	    System.arraycopy(c, off, buf, count, len);
	    count = newcount;
	}
    
public voidwrite(java.lang.String str, int off, int len)
Write a portion of a string to the buffer.

param
str String to be written from
param
off Offset from which to start reading characters
param
len Number of characters to be written

	synchronized (lock) {
	    int newcount = count + len;
	    if (newcount > buf.length) {
                buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
	    }
	    str.getChars(off, off + len, buf, count);
	    count = newcount;
	}
    
public voidwriteTo(java.io.Writer out)
Writes the contents of the buffer to another character stream.

param
out the output stream to write to
throws
IOException If an I/O error occurs.

	synchronized (lock) {
	    out.write(buf, 0, count);
	}