Page 1 of 1

Closing of the IFormatReader object.

PostPosted: Fri Apr 02, 2010 7:27 am
by n_biocoder
The following code opens a file using IFormatReader, closes all the objects and tries to delete the file.

Code: Select all
/* open image reader */
ImageReader imgReader = new ImageReader();
IFormatReader formatReader = imgReader.getReader("file.tif");
formatReader.setId("file.tif");
BufferedImageReader bufferedImageReader = new BufferedImageReader(formatReader);
BufferedImage buffImage = bufferedImageReader.openThumbImage(0);

/* close image reader */
imgReader.close();
imgReader = null;
formatReader.close();
formatReader = null;

/* delete file */
File filenameObj = new File("file.tif");
boolean boolFile = filenameObj.delete();


The delete file will not work. I think its because of line 135 (Location.getHandle(id);) in ImageReader in the public IFormatReader getReader(String id) method. It returns an IRandomAccess which needs to be closed. When i replace that code with the following it seems to work fine.

Code: Select all
IRandomAccess canReadID = Location.getHandle(id);
canReadID.close();


I dont know if this change has any side effects furthur on. Could someone comment on the same? If its a valid issue, maybe it could be fixed next release.

~Thanks

Re: Closing of the IFormatReader object.

PostPosted: Mon Apr 05, 2010 9:01 pm
by mlinkert
Hi,

This problem was fixed in SVN revision 6024, which you can view here:

https://skyking.microscopy.wisc.edu/tra ... geset/6024

You can test the fix by updating to a recent trunk or daily build of Bio-Formats.

Regards,
-Melissa

Re: Closing of the IFormatReader object.

PostPosted: Thu Apr 08, 2010 7:25 pm
by n_biocoder
Hi,

There is another change i would suggest to solve a similar problem when we open and try to delete jpg images. I made this change in the loci.formats.in.ImageIOReader. The following code on Line 92 locks the file if we dont call close functions on DataInputStream and RandomAccessInputStream objects.
Code: Select all
BufferedImage img =
      ImageIO.read(new DataInputStream(new RandomAccessInputStream(currentId)));


After closing those objects it seemed to work ok.
Code: Select all
RandomAccessInputStream rIn = new RandomAccessInputStream(currentId);
DataInputStream dIn = new DataInputStream(rIn);
BufferedImage img = ImageIO.read(dIn);
rIn.close();
dIn.close();


~Thanks

Re: Closing of the IFormatReader object.

PostPosted: Thu Apr 08, 2010 9:30 pm
by mlinkert
Hi,

Thank you for pointing out this problem. SVN revision 6107 (https://skyking.microscopy.wisc.edu/tra ... geset/6107) fixes this and a few similar problems.

Regards,
-Melissa