AutoDeleteFileInputStream extends FileInputStream { @Override public void close() throws IOException { try { super.close(); } finally { if (file.exists()) { file.delete(); } } } AutoDeleteFileInputStream(File file);  AutoDeleteFileInputStream(InputStream copyFromStream); @Override void close(); long getFileLength();  }
@Test public void existingFileShouldBeDeletedWhenStreamIsClosed() throws IOException { file = File.createTempFile(getClass().getSimpleName(), null); file.deleteOnExit(); AutoDeleteFileInputStream adFileInputStream = new AutoDeleteFileInputStream(file); assertTrue(file.exists()); adFileInputStream.close(); assertFalse(file.exists()); }