观察者模式:
public class FileAlterationObserver implements Serializable {
private static final long serialVersionUID = 1185122225658782848L;
private final List<FileAlterationListener> listeners = new CopyOnWriteArrayList<>();
Observer中有多个listener,listener中有各种事件响应方法:
public interface FileAlterationListener {
/**
* File system observer started checking event.
*
* @param observer The file system observer
*/
void onStart(final FileAlterationObserver observer);
/**
* Directory created Event.
*
* @param directory The directory created
*/
void onDirectoryCreate(final File directory);
事件发生时,调用observer的方法:
public void checkAndNotify() {
/* fire onStart() */
for (final FileAlterationListener listener : listeners) {
listener.onStart(this);
}
来让listener得到事件发生的通知。
判断数组越界的位与判断(只判断符号位即可):
FilterOutputStream:
public void write(byte b[], int off, int len) throws IOException {
if ((off | len | (b.length - (len + off)) | (off + len)) < 0)
throw new IndexOutOfBoundsException();
FilterOutputStream继承了OutputStream,但是如果不继承OutputStream,对于FilterOutputStream自身的功能没有任何影响,继承OutputStream的原因可能是为了配合FilterOutputStream的名字。
代理模式:
ProxyOutputStream继承了FilterOutputStream,然后对FilterOutputStream的方法进行了包装:
public ProxyOutputStream(final OutputStream proxy) {
super(proxy);
// the proxy is stored in a protected superclass variable named 'out'
}
@Override
public void write(final byte[] bts, final int st, final int end) throws IOException {
try {
beforeWrite(end);
out.write(bts, st, end);
afterWrite(end);
} catch (final IOException e) {
handleIOException(e);
}
}