CharArrayReaderクラスはchar[]型の配列から読み込むためのReaderクラスであり,java.io.Readerクラスを継承している。
インスタンス変数として,char[]型のbufが宣言されており,ここに読込対象のデータが保持される。posというint型のインスタンス変数はバッファにおける現在の読込位置を保持する。インスタンス変数はmarkedPosはバッファ内のマーカーの位置であり,reset()メソッドで戻る位置を示す。バッファのcount変数は文字数を示している。
- CharArrayReader(char buf[])
- CharArrayReader(char buf[], int offset, int length)
read()メソッドは,バッファから1文字を読み込むものである。きわめてわかりやすい実装である。バッファであるbuf配列から,現在の読込位置のインデックスの値を返し,読込位置をインクリメントする。読込位置を表すposがcount以上になると,EOFを表す-1を返している。
read(char b[], int off, int len)メソッドは,バッファの指定位置から指定の長さだけを引数のb[]配列にコピーして返すものである。読み込んだ長さを返す。EOFの場合は-1を返している。
mark(int readAheadLimit)メソッドは,現在の読込位置であるposをmarkedPos変数に代入して,マーク位置を保存する。reset()メソッドにより,逆にposにmarkedPos変数が代入され,読込位置がマーク位置に戻る仕組みになっている。
close()メソッドでは,バッファであるbuf変数にnullを代入している。
* Copyright (c) 1996, 2015, 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 CharArrayReader extends Reader {
protected char buf[];
protected int pos;
protected int markedPos = 0;
protected int count;
public CharArrayReader(char buf[]) {
this.buf = buf;
this.pos = 0;
this.count = buf.length;
}
public CharArrayReader(char buf[], int offset, int length) {
if ((offset < 0) || (offset > buf.length) || (length < 0) ||
((offset + length) < 0)) {
throw new IllegalArgumentException();
}
this.buf = buf;
this.pos = offset;
this.count = Math.min(offset + length, buf.length);
this.markedPos = offset;
}
private void ensureOpen() throws IOException {
if (buf == null)
throw new IOException("Stream closed");
}
public int read() throws IOException {
synchronized (lock) {
ensureOpen();
if (pos >= count)
return -1;
else
return buf[pos++];
}
}
public int read(char b[], int off, int len) throws IOException {
synchronized (lock) {
ensureOpen();
if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) > b.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
if (pos >= count) {
return -1;
}
int avail = count – pos;
if (len > avail) {
len = avail;
}
if (len <= 0) {
return 0;
}
System.arraycopy(buf, pos, b, off, len);
pos += len;
return len;
}
}
public long skip(long n) throws IOException {
synchronized (lock) {
ensureOpen();
long avail = count – pos;
if (n > avail) {
n = avail;
}
if (n < 0) {
return 0;
}
pos += n;
return n;
}
}
public boolean ready() throws IOException {
synchronized (lock) {
ensureOpen();
return (count – pos) > 0;
}
}
public boolean markSupported() {
return true;
}
public void mark(int readAheadLimit) throws IOException {
synchronized (lock) {
ensureOpen();
markedPos = pos;
}
}
public void reset() throws IOException {
synchronized (lock) {
ensureOpen();
pos = markedPos;
}
}
public void close() {
synchronized (lock) {
buf = null;
}
}
}