coobird / thumbnailator

Thumbnailator - a thumbnail generation library for Java
MIT License
5.14k stars 784 forks source link

Is it a bug for 'Thumbnails.of(...files)' that the file reference not being released? #149

Closed CLovinr closed 4 years ago

CLovinr commented 4 years ago

Expected behavior

The reference of the file should be released when Thumbnails.of(tempImgFile) used.

Actual behavior

After Thumbnails.of(tempImgFile)...toFile(targetFile),the tempImgFile could not be deleted.

Steps to reproduce the behavior

This is a simple test to reproduce the behavior:

    @Test
    public void test1ThumbnailsFileBug() throws IOException
    {
        File temp1ImgFile = new File("E:/MyFiles/Win10Moved/Pictures/Saved Pictures/temp1.jpg");
        File target1ImgFile = new File("E:/MyFiles/Win10Moved/Pictures/Saved Pictures/target1.jpg");

        Thumbnails.Builder fileBuilder = Thumbnails.of(temp1ImgFile);
        fileBuilder.width(256);
        fileBuilder.height(256);
        fileBuilder.outputQuality(0.8f);
        fileBuilder.outputFormat("jpg");
        fileBuilder.toFile(target1ImgFile);
        Assert.assertTrue(temp1ImgFile.delete());//delete fail for '0.4.9' and '0.4.10',sucess for '0.4.8'

    }

    @Test
    public void test2ThumbnailsFileBug() throws IOException
    {
        File temp2ImgFile = new File("E:/MyFiles/Win10Moved/Pictures/Saved Pictures/temp2.jpg");
        File target2ImgFile = new File("E:/MyFiles/Win10Moved/Pictures/Saved Pictures/target2.jpg");
        try (InputStream in = new FileInputStream(temp2ImgFile))//close the input file myself
        {
            Thumbnails.Builder fileBuilder = Thumbnails.of(in);
            fileBuilder.width(256);
            fileBuilder.height(256);
            fileBuilder.outputQuality(0.8f);
            fileBuilder.outputFormat("jpg");
            fileBuilder.toFile(target2ImgFile);
        }
        Assert.assertTrue(temp2ImgFile.delete());//delete success

    }

Environment

coobird commented 4 years ago

Thanks for the bug report. I found a place that's likely causing the issue!

coobird commented 4 years ago

This should be fixed in 0.4.11. :)