Overwriting Native Methods
I want to make my own TempFileInputStream class that extends FileInputStream and overwrites close() so that it deletes the file as well. It's not working yet, and I wonder if the problem has something to do with that I'm trying to overwrite a native method (the subtleties of which I'm not clear on). Please share any insights.
Hi bschauwe, here are the code snippets.
Using the following class produces a FileNotFoundException in the init.
java.io.FileNotFoundException: /home/hopper/id0/www/organizer/tmp/foobar997121798292
at java.lang.Throwable.<init>(Compiled Code)
at java.lang.Exception.<init>(Compiled Code)
at java.io.IOException.<init>(Compiled Code)
at java.io.FileNotFoundException.<init>(Compiled Code)
at java.io.FileInputStream.<init>(Compiled Code)
at java.io.FileInputStream.<init>(Compiled Code)
at organizer.multipart.TempFileInputStream.<init>(Compiled Code)
Using FileInputStream in the same place and it works fine (without deleting the file on close of course).
--
import java.io.FileInputStream;
import java.io.IOException;
import java.io.File;
public class TempFileInputStream extends FileInputStream {
private File m_file;
TempFileInputStream(File file)
throws IOException {
super(file);
m_file = file;
}
public void close() throws IOException {
super.close();
if (m_file != null)
m_file.delete();
}
}
-
It's initiated like this:
public InputStream getInputStream() {
try {
return new TempFileInputStream(m_file);
I suggest you put in a few print statements to be sure which statement is failing.
One thing I notice is that you are passing in a File object, then using it to
o do file processing,
o be closed when you close the stream.
o do the delete.
try instantiating a separate File object (with the same path) and doing the delete.