FileDocCategorySizeDatePackage
CharArrayWriter.javaAPI DocAndroid 1.5 API11692Wed May 06 22:41:04 BST 2009java.io

CharArrayWriter

public class CharArrayWriter extends Writer
A specialized {@link Writer} for class for writing content to an (internal) char array. As bytes are written to this writer, the char array may be expanded to hold more characters. When the writing is considered to be finished, a copy of the char array can be requested from the class.
see
CharArrayReader
since
Android 1.0

Fields Summary
protected char[]
buf
The buffer for characters.
protected int
count
The ending index of the buffer.
Constructors Summary
public CharArrayWriter()
Constructs a new CharArrayWriter which has a buffer allocated with the default size of 32 characters. This buffer is also used as the {@code lock} to synchronize access to this writer.

since
Android 1.0

        super();
        buf = new char[32];
        lock = buf;
    
public CharArrayWriter(int initialSize)
Constructs a new CharArrayWriter which has a buffer allocated with the size of {@code initialSize} characters. The buffer is also used as the {@code lock} to synchronize access to this writer.

param
initialSize the initial size of this CharArrayWriters buffer.
throws
IllegalArgumentException if {@code initialSize < 0}.
since
Android 1.0

        super();
        if (initialSize < 0) {
            throw new IllegalArgumentException(Msg.getString("K005e")); //$NON-NLS-1$
        }
        buf = new char[initialSize];
        lock = buf;
    
Methods Summary
public java.io.CharArrayWriterappend(char c)
Appends a char {@code c} to the CharArrayWriter. The method works the same way as {@code write(c)}.

param
c the character appended to the CharArrayWriter.
return
this CharArrayWriter.
since
Android 1.0

        write(c);
        return this;
    
public java.io.CharArrayWriterappend(java.lang.CharSequence csq)
Appends a CharSequence {@code csq} to the CharArrayWriter. The method works the same way as {@code write(csq.toString())}. If {@code csq} is null, then it will be substituted with the string "null".

param
csq the CharSequence appended to the CharArrayWriter, may be null.
return
this CharArrayWriter.
since
Android 1.0

        if (null == csq) {
            append(TOKEN_NULL, 0, TOKEN_NULL.length());
        } else {
            append(csq, 0, csq.length());
        }
        return this;
    
public java.io.CharArrayWriterappend(java.lang.CharSequence csq, int start, int end)
Append a subsequence of a CharSequence {@code csq} to the CharArrayWriter. The first and last characters of the subsequence are specified by the parameters {@code start} and {@code end}. The CharArrayWriter.append({@code csq}) works the same way as {@code CharArrayWriter.write(csq.subSequence(start, end).toString)}. If {@code csq} is null, then it will be substituted with the string "null".

param
csq the CharSequence appended to the CharArrayWriter, may be null.
param
start the index of the first character in the CharSequence appended to the CharArrayWriter.
param
end the index of the character after the last one in the CharSequence appended to the CharArrayWriter.
return
this CharArrayWriter.
throws
IndexOutOfBoundsException if {@code start < 0}, {@code end < 0}, {@code start > end}, or if {@code end} is greater than the length of {@code csq}.
since
Android 1.0

        if (null == csq) {
            csq = TOKEN_NULL;
        }
        String output = csq.subSequence(start, end).toString();
        write(output, 0, output.length());
        return this;
    
public voidclose()
Closes this writer. The implementation in CharArrayWriter does nothing.

since
Android 1.0

        /* empty */
    
private voidexpand(int i)

        /* Can the buffer handle @i more chars, if not expand it */
        if (count + i <= buf.length) {
            return;
        }

        char[] newbuf = new char[buf.length + (2 * i)];
        System.arraycopy(buf, 0, newbuf, 0, count);
        buf = newbuf;
    
public voidflush()
Flushes this writer. The implementation in CharArrayWriter does nothing.

since
Android 1.0

        /* empty */
    
public voidreset()
Resets this writer. The current write position is reset to the beginning of the buffer. All written characters are lost and the size of this writer is set to 0.

since
Android 1.0

        synchronized (lock) {
            count = 0;
        }
    
public intsize()
Returns the size of this writer, that is the number of characters it stores. This number changes if this writer is reset or when more characters are written to it.

return
this CharArrayWriter's current size in characters.
since
Android 1.0

        synchronized (lock) {
            return count;
        }
    
public char[]toCharArray()
Returns the contents of the receiver as a char array. The array returned is a copy and any modifications made to this writer after calling this method are not reflected in the result.

return
this CharArrayWriter's contents as a new char array.
since
Android 1.0

        synchronized (lock) {
            char[] result = new char[count];
            System.arraycopy(buf, 0, result, 0, count);
            return result;
        }
    
public java.lang.StringtoString()
Returns the contents of this CharArrayWriter as a string. The string returned is a copy and any modifications made to this writer after calling this method are not reflected in the result.

return
this CharArrayWriters contents as a new string.
since
Android 1.0

        synchronized (lock) {
            return new String(buf, 0, count);
        }
    
public voidwrite(char[] c, int offset, int len)
Writes {@code count} characters starting at {@code offset} in {@code c} to this writer.

param
c the non-null array containing characters to write.
param
offset the index of the first character in {@code buf} to write.
param
len maximum number of characters to write.
throws
IndexOutOfBoundsException if {@code offset < 0} or {@code len < 0}, or if {@code offset + len} is bigger than the size of {@code c}.
since
Android 1.0

        // avoid int overflow
        // BEGIN android-changed
        // Exception priorities (in case of multiple errors) differ from
        // RI, but are spec-compliant.
        // made implicit null check explicit,
        // removed redundant check,
        // added null check, used (offset | len) < 0 instead of
        // (offset < 0) || (len < 0) to safe one operation
        if (c == null) {
            throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
        }
        if ((offset | len) < 0 || len > c.length - offset) {
            throw new IndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
        }
        // END android-changed
        synchronized (lock) {
            expand(len);
            System.arraycopy(c, offset, this.buf, this.count, len);
            this.count += len;
        }
    
public voidwrite(int oneChar)
Writes the specified character {@code oneChar} to this writer. This implementation writes the two low order bytes of the integer {@code oneChar} to the buffer.

param
oneChar the character to write.
since
Android 1.0

        synchronized (lock) {
            expand(1);
            buf[count++] = (char) oneChar;
        }
    
public voidwrite(java.lang.String str, int offset, int len)
Writes {@code count} number of characters starting at {@code offset} from the string {@code str} to this CharArrayWriter.

param
str the non-null string containing the characters to write.
param
offset the index of the first character in {@code str} to write.
param
len the number of characters to retrieve and write.
throws
NullPointerException if {@code str} is null.
throws
StringIndexOutOfBoundsException if {@code offset < 0} or {@code len < 0}, or if {@code offset + len} is bigger than the length of {@code str}.
since
Android 1.0

        if (str == null) {
            throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
        }
        // avoid int overflow
        // BEGIN android-changed
        // Exception priorities (in case of multiple errors) differ from
        // RI, but are spec-compliant.
        // removed redundant check, used (offset | len) < 0
        // instead of (offset < 0) || (len < 0) to safe one operation
        if ((offset | len) < 0 || len > str.length() - offset) {
            throw new StringIndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
        }
        // END android-changed
        synchronized (lock) {
            expand(len);
            str.getChars(offset, offset + len, buf, this.count);
            this.count += len;
        }
    
public voidwriteTo(java.io.Writer out)
Writes the contents of this CharArrayWriter to another Writer. The output is all the characters that have been written to the receiver since the last reset or since it was created.

param
out the non-null Writer on which to write the contents.
throws
NullPointerException if {@code out} is null.
throws
IOException if an error occurs attempting to write out the contents.
since
Android 1.0

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