import java.io.*;


public class ReentrantClose extends FileOutputStream {

    public ReentrantClose(String filename) throws FileNotFoundException {
        super(filename);
    }

    @Override
    public void close() throws IOException {
        new Throwable("close() invoked here").printStackTrace(System.out);
        super.close();
    }

    public static void main(String[] args) throws Exception {
        try (ReentrantClose out = new ReentrantClose("testfile")) {
            out.getChannel().force(false); // this line triggers re-entrant invocation of close()
        }
    }
} 