JDK

java.io.CharArrayWriterクラス

CharArrayWriterクラスはchar[]型の配列へ書き込むWriterクラスである。java.io.Writerクラスを継承している。書込先となるchar[]型のインスタンス変数としてbuf,書き込み済の文字数を示すint型のインスタンス変数としてcountが宣言されている。

コンストラクタは,次の2種類がオーバーロードされている。1つめのコンストラクタはバッファサイズとしてディフォルト値32を用いてバッファを作成する。一方2つめのコンストラクタは,引数でバッファサイズを受取り,そのサイズのバッファを確保する。
  • CharArrayWriter()
  • CharArrayWriter(int initialSize)

write(int c)メソッドは,count変数をインクリメントした上で,バッファサイズを超えないかチェックした上でint型で受け取った引数をchar[]配列へ書き込む。countがバッファサイズを超える場合は,バッファサイズを1増やしたバッファを確保した上で,配列の内容をコピーし,引数として受け取った文字を書き込む。バッファサイズを頻繁に拡張するとパフォーマンスの劣化が見込まれそうだ。

write(char c[], int off, int len)メソッドはchar[]型の配列で受け取ったデータを指定のオフセットから指定の長さだけ書き込む。バッファサイズが第3引数で受け取った長さと書き込み済の文字数countの合計より小さくなる場合は,新たにバッファを作成して,内容をSystem.arraycopy()メソッドでコピーしている。

write(String str, int off, int len)メソッドは,char[]配列の代わりに,Stringを書き込むもので,処理内容は同様である。

writeTo(Writer out)メソッドはバッファの内容を引数として受け取ったWriterオブジェクトへ書き込む。

append(CharSequence csq)メソッドはStringなどのCharSequenceインタフェースを実装したオブジェクトを追加して,自身への参照を返す。append(CharSequence csq, int start, int end)append(CharSequence csq)メソッドはStringなどのCharSequenceインタフェースを実装したオブジェクトの引数startからendまでの部分を追加して,自身への参照を返す。

reset()メソッドは,書き込み済のカウントをゼロに戻す。

/*
 * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package java.io;
import java.util.Arrays;
public
class CharArrayWriter extends Writer {
    protected char buf[];
    protected int count;
    public CharArrayWriter() {
        this(32);
    }
    public CharArrayWriter(int initialSize) {
        if (initialSize < 0) {
            throw new IllegalArgumentException("Negative initial size: "
                                               + initialSize);
        }
        buf = new char[initialSize];
    }
    public void write(int c) {
        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 void write(char c[], int off, int len) {
        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 void write(String str, int off, int len) {
        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 void writeTo(Writer out) throws IOException {
        synchronized (lock) {
            out.write(buf, 0, count);
        }
    }
    public CharArrayWriter append(CharSequence csq) {
        String s = String.valueOf(csq);
        write(s, 0, s.length());
        return this;
    }
    public CharArrayWriter append(CharSequence csq, int start, int end) {
        if (csq == null) csq = "null";
        return append(csq.subSequence(start, end));
    }
    public CharArrayWriter append(char c) {
        write(c);
        return this;
    }
    public void reset() {
        count = 0;
    }
    public char[] toCharArray() {
        synchronized (lock) {
            return Arrays.copyOf(buf, count);
        }
    }
    public int size() {
        return count;
    }
    public String toString() {
        synchronized (lock) {
            return new String(buf, 0, count);
        }
    }
    public void flush() { }
    public void close() { }
}