About Me

My photo
PhD Candidate at Purdue University, Computer Science.

Sunday, December 16, 2007

JAI handling files

I am using JAI to display images. First I run my development environment was MAC. Everything was going smooth with me.
The problems started when I moved to Windows. Somehow, when I try to delete the image file from the hard disk, it didn't work.
Seems that JAI keeps a reference over the file indefinitely.
Example:
PlanarImage image = JAI.create("fileload",path)

The file load operation doesn't release the handler.
I tried to nullify the source handling the image, then call the System.gc(). (which is a really bad solution)

another solution is as following:
//replace fileLoad operation
File image = new File(path);
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[(int)file.length()];
fis.read(buffer);
fis.close();
PlanarImage image = JAI.create("stream",new ByteArraySeekableStream(buffer));

//replace file store operation
JAI.create("filestore",image,file.getAbsolutePath(),"jpg",null);

If this doesn't work, it maybe because file descriptor is not released. you may need to call the garbage collector.

No comments: