public abstract class FilterReader extends Reader {
/**
* The underlying character-input stream.
*/
protected Reader in;
/**
* Create a new filtered reader.
*
* @param in a Reader object providing the underlying stream.
* @throws NullPointerException if <code>in</code> is <code>null</code>
*/
protected FilterReader(Reader in) {
super(in);
this.in = in;
}
/**
* Read a single character.
*
* @exception IOException If an I/O error occurs
*/
public int read() throws IOException {
return in.read();
}
/**
* Read characters into a portion of an array.
*
* @exception IOException If an I/O error occurs
*/
public int read(char cbuf[], int off, int len) throws IOException {
return in.read(cbuf, off, len);
}
/**
* Skip characters.
*
* @exception IOException If an I/O error occurs
*/
public long skip(long n) throws IOException {
return in.skip(n);
}
/**
* Tell whether this stream is ready to be read.
*
* @exception IOException If an I/O error occurs
*/
public boolean ready() throws IOException {
return in.ready();
}
/**
* Tell whether this stream supports the mark() operation.
*/
public boolean markSupported() {
return in.markSupported();
}
/**
* Mark the present position in the stream.
*
* @exception IOException If an I/O error occurs
*/
public void mark(int readAheadLimit) throws IOException {
in.mark(readAheadLimit);
}
/**
* Reset the stream.
*
* @exception IOException If an I/O error occurs
*/
public void reset() throws IOException {
in.reset();
}
/**
* Close the stream.
*
* @exception IOException If an I/O error occurs
*/
public void close() throws IOException {
in.close();
}
/**
* FilterReader是一个抽象类,不能实例化该类,FilterReader类的每个方法都是实现的,
* 里面调用的都是FilterReader(Reader in)的传入参数in的方法.
*/
public class UppercaseConvertor extends FilterReader{
// 构造方法由FilterReader的protected上升到public的
public UppercaseConvertor(Reader in) {
super(in);
}
@Override
public int read() throws IOException{
int c = super.read();
return (-1==c?c:Character.toUpperCase((char)c));
}
@Override
public int read(char[] buf,int offset, int count) throws IOException{
int nread = super.read(buf, offset, count);
int last = offset + nread;
for(int i=0;i<last;i++)
buf[i] = Character.toUpperCase(buf[i]);
return nread;
}