Hi,

The GeoTiffReader will handle the resource management of the file. You can have 
several calls for read with params referring to different parts of the file. 
You wouldn't clutter that with calls for opening / closing the file. The code 
should look more like:

GeoTiffReader reader = new GeoTiffReader(file);
try {
    reader.read(null);
} finally {
    reader.dispose();
}
// Here you could check if the file is closed

Regards,
Roar Brænden

> 11. des. 2025 kl. 12:49 skrev Viatcheslav Sysoltsev 
> <[email protected]>:
> 
> Hi Ian,
> 
> Been debugging, the stream (of type 
> it.geosolutions.imageio.stream.eraf.EnhancedRandomAccessFile) remains not 
> closed after call at org.geotools.gce.geotiff.GeoTiffReader#read():737
> > pbjRead.add(maskOvrProvider.getSourceSpiProvider().getStream());
> ... where it is passed to JaI/ImageN
> 
> With best regards, Slava
> 
> Von: Ian Turton <[email protected] <mailto:[email protected]>>
> Gesendet: Donnerstag, 11. Dezember 2025 12:16
> An: Viatcheslav Sysoltsev <[email protected] 
> <mailto:[email protected]>>
> Cc: [email protected] 
> <mailto:[email protected]> 
> <[email protected] 
> <mailto:[email protected]>>
> Betreff: Re: [Geotools-gt2-users] GeoTiffReader locks the file indefinitely?
>  
> Sie erhalten nicht häufig E-Mails von [email protected] 
> <mailto:[email protected]>. Erfahren Sie, warum dies wichtig ist 
> <https://aka.ms/LearnAboutSenderIdentification>    
> I think that's the expected behaviour on a windows machine for any file 
> opened in a program. The code clearly closes the input stream created from 
> the file that is passed in so I don't think there is any reason GeoTools 
> would be locking the file.
> 
> Ian
> 
> On Wed, 10 Dec 2025 at 17:31, Viatcheslav Sysoltsev 
> <[email protected] 
> <mailto:[email protected]>> wrote:
> Hi everyone,
> 
> I'd like to ask if it is an intended behavior, that GeoTiffReader locks the 
> file indefinitely? If yes, is there a way to read the file completely and 
> "dispose" the reader and the lock?
> 
> A simple program to demonstrate:
> 
> import org.geotools.coverage.grid.GridCoverage2D;
> import org.geotools.gce.geotiff.GeoTiffReader;
> import org.junit.jupiter.api.Test;
> 
> import java.io.File;
> import java.time.Duration;
> 
> class GeotiffReaderTest {
>    @Test
>    public void read() throws Exception {
>        File file = new File("C:\\Projects\\tmp\\tmp2\\qwf 2_TH.tif");
>        GeoTiffReader reader = new GeoTiffReader(file);
>        GridCoverage2D grid = reader.read(null);
>        Thread.sleep(Duration.ofSeconds(60));
>    }
> }
> when run, it reads the file and sleeps for a minute. Until JVM exits, the tif 
> file cannot be moved or deleted:
> 
> C:\Projects\tmp\tmp2>mv "qwf 2_TH.tif" "qwf 2_TH.tif2"
> /usr/bin/mv: cannot move 'qwf 2_TH.tif' to 'qwf 2_TH.tif2': Device or 
> resource busy
> 
> // the problem is reproducible with geotools 34.1 and 33.0
> 
> With best regards,
> Viatcheslav Sysoltsev / Slava
> 
> _______________________________________________
> GeoTools-GT2-Users mailing list
> [email protected] 
> <mailto:[email protected]>
> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users 
> <https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users>
> 
> 
> -- 
> Ian Turton
> _______________________________________________
> GeoTools-GT2-Users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

_______________________________________________
GeoTools-GT2-Users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to