A decoration class can be implemented by inheriting the abstract decorator class (FilterInputStream), and this function can be realized by calling some methods provided by the InputStream class or its subclasses and adding logical judgments.
import java.io.*;
/**
* Created by Administrator on 2018/4/25 0025.
*/
public class MyOwnInputStream extends FilterInputStream{
public static void main(String[] args) {
int c;
try {
InputStream is = new MyOwnInputStream(new FileInputStream("D:/test.txt"));
while ((c = is.read()) >= 0) {
System.out.println((char) c);
}
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public MyOwnInputStream(InputStream in) {
super(in);
}
@Override
public int read() throws IOException {
int c = super.read();
if (c != -1) {
if (Character.isLowerCase((char)c)) {
return Character.toUpperCase((char) c);
}
else if (Character.isUpperCase((char)c)) {
return Character.toLowerCase((char) c);
}
else {
return c;
}
} else {
return -1;
}
}
}
References Java Programmer Interview Written Test Collection P117