StringWriterクラスは文字列に対してStreamのように書き込めるようにしたクラスである。内部では,StringBuffer型のインスタンス変数bufにデータを保持している。
- StringWriter()
- StringWriter(int initialSize)
write(int c)メソッドはバッファに対してint型で与えられたコードに対応する文字を追加して書き込む。write(char cbuf[], int off, int len)write(int c)メソッドはバッファに対してchar[]型で与えられた文字配列を指定のオフセットから長さ分だけ追加する。write(String str)メソッドはString型の文字列をバッファに追加する。write(String str, int off, int len)メソッドはString型の文字列を指定のオフセットから指定の長さだけバッファに追加する。
append(CharSequence csq)メソッドは引数のCharSequence(StringやStringBufferが実装しているインタフェースでcharのシーケンスを保持している)を追加し,自身のインスタンスを返す。append(CharSequence csq, int start, int end)メソッドは引数のCharSequenceにおける指定の開始位置から終了位置までの部分を取り出してバッファへ追加し,自身のオブジェクトを返す。append(char c)は文字をバッファに追加し,自身をオブジェクトを返す。getBuffer()メソッドはこのクラスのバッファとなっているStringBufferインスタンス変数への参照を返す。
flush()メソッドとclose()メソッドは何も実行しない。
* 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;
public class StringWriter extends Writer {
private StringBuffer buf;
public StringWriter() {
buf = new StringBuffer();
lock = buf;
}
public StringWriter(int initialSize) {
if (initialSize < 0) {
throw new IllegalArgumentException("Negative buffer size");
}
buf = new StringBuffer(initialSize);
lock = buf;
}
public void write(int c) {
buf.append((char) c);
}
public void write(char cbuf[], int off, int len) {
if ((off < 0) || (off > cbuf.length) || (len < 0) ||
((off + len) > cbuf.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return;
}
buf.append(cbuf, off, len);
}
public void write(String str) {
buf.append(str);
}
public void write(String str, int off, int len) {
buf.append(str, off, off + len);
}
public StringWriter append(CharSequence csq) {
write(String.valueOf(csq));
return this;
}
public StringWriter append(CharSequence csq, int start, int end) {
if (csq == null) csq = "null";
return append(csq.subSequence(start, end));
}
public StringWriter append(char c) {
write(c);
return this;
}
public String toString() {
return buf.toString();
}
public StringBuffer getBuffer() {
return buf;
}
public void flush() {
}
public void close() throws IOException {
}
}