Skip to main content

JAI.create() method not closing file descriptors?

1 reply [Last post]
sam2k2002
Offline
Joined: 2010-04-23

I've been trying to use JAI to save a greyscale BufferedImage as a TIFF file so I can run a TIFF-only OCR program against it, and it seems like the JAI.create() method holds onto its file descriptor forever. This bug was first posed in 2004 and was supposed to be fixed in JAI 1.1.3, but I'm still encountering the problem.
Bug Link: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5029451

Here is part of my code:

<br />
BufferedImage newImage= new BufferedImage(oldImage.getWidth(),oldImage.getHeight(),BufferedImage.TYPE_BYTE_GRAY); //to convert to a greyscale img<br />
newImage.getGraphics().drawImage(oldImage,0,0,null);<br />
String filename = "s.tiff";<br />
String format = "TIFF";<br />
RenderedOp op = JAI.create("filestore", newImage, filename, format); //file descriptor is grabbed here and never let go</p>
<p>try {<br />
	Runtime rt = Runtime.getRuntime();<br />
	String[] commandArgs = {"cmd.exe", "/c", "del s.tiff"}; //this should delete the file that was just created<br />
	rt.exec(commandArgs);<br />
} catch (Exception e) {}<br />

The above code should create the tiff file (which it does) and then delete the file (which it does not). Does anyone know a workaround for this bug? I've heard forcing garbage collection helps, but it did not work for me.

-Sam

Message was edited by: sam2k2002

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Nidel, Mike

The way I see it, you have two choices:

1. Use the "encode" operator instead of "filestore".
2. Use ImageIO with the JAI ImageIO tools, then you can just call

ImageIO.write(newImage, "tiff", theFile);

I personally recommend the second because the ImageIO tools TIFF
plugin is more recent than the JAI encoder. but either way should work.

> -----Original Message-----
> From: jai-interest@javadesktop.org [mailto:jai-
> interest@javadesktop.org]
> Sent: Friday, April 23, 2010 4:44 AM
> To: interest@jai.dev.java.net
> Subject: [JAI] JAI.create() method not closing file descriptors?
>
> I've been trying to use JAI to save a greyscale BufferedImage as a TIFF
> file so I can run a TIFF-only OCR program against it, and it seems like
> the JAI.create() method holds onto its file descriptor forever. This
> bug was first posed in 2004 and was supposed to be fixed in JAI 1.1.3,
> but I'm still encountering the problem.
>
> Here is part of my code:
>
> [code]
> BufferedImage newImage= new
> BufferedImage(oldImage.getWidth(),oldImage.getHeight(),BufferedImage.TY
> PE_BYTE_GRAY); //to convert to a greyscale img
> newImage.getGraphics().drawImage(oldImage,0,0,null);
> String filename = "s.tiff";
> String format = "TIFF";
> RenderedOp op = JAI.create("filestore", newImage, filename, format);
> //file descriptor is grabbed here and never let go
>
> try {
> Runtime rt = Runtime.getRuntime();
> String[] commandArgs = {"cmd.exe", "/c", "del s.tiff"}; //this
> should delete the file that was just created
> rt.exec(commandArgs);
> } catch (Exception e) {}
> [/code]
>
> The above code should create the tiff file (which it does) and then
> delete the file (which it does not). Does anyone know a workaround for
> this bug? I've heard forcing garbage collection helps, but it did not
> work for me.
>
> -Sam
> [Message sent by forum member 'sam2k2002']
>
> http://forums.java.net/jive/thread.jspa?messageID=398633
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net