public class AmigoOutputStream extends FileOutputStream {
public static String fileName = "D:/tmp/result.txt";
private FileOutputStream original = null;
public AmigoOutputStream(FileOutputStream fileOutputStream) throws Exception {
super(fileOutputStream.getFD());
this.original = fileOutputStream;
}
@Override
public void write(int b) throws IOException {
original.write(b);
}
@Override
public void write(byte[] b) throws IOException {
original.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
original.write(b, off, len);
}
@Override
public void close() throws IOException {
original.flush();
original.write("JavaRush © All rights reserved.".getBytes());
original.close();
}
@Override
public void flush() throws IOException {
original.flush();
}
public static void main(String[] args) throws Exception {
AmigoOutputStream amigoOutputStream = new AmigoOutputStream(new FileOutputStream(fileName));
amigoOutputStream.close();
}
Это содержание выходного файла:
JavaRush © All rights reserved.JavaRush © All rights reserved.
Похоже, метод close выполняется дважды, проверено отладчиком. Почему так?